How to execute javascript before execute click event?

goodmorningsky

Centurion
Joined
Aug 18, 2003
Messages
172
Hi, all.
my page contains [Delete] button control runat=server.
If it's clicked
Show message box asking confirmation and delete the page item from database.

How to do this?

thanks..
 
goodmorningsky said:
Hi, all.
my page contains [Delete] button control runat=server.
If it's clicked
Show message box asking confirmation and delete the page item from database.

How to do this?

thanks..
Ok, lets step back from asp.net for a second.

you know that each HTML Element <input type=SUBMIT> has an onsubmit event. If the onsubmit is attached to a function that returns a boolean, the submit cancels out when returning false and does not post - when returning true it does post. . .

so we can attach to the onsubmit event for an input-button element "window.confirm('Delete Current Item?');"

further more, you can write a script block to attach the code without putting the code within the <INPUT> tag by doing this:

<SCRIPT FOR="document.all.myDeleteButton" language="JAVASCRIPT" event="onsubmit">
"window.confirm('Delete Current Item?');"
</SCRIPT>

so. . . how do we do it inside the code behind the .aspx page programatically???


well, what I do in my .net page class definition is this:

1. I create a function that returns the script block that registers the event. . .
(no promise that syntax is correct. the approach is sound while the code might contain errors)
PHP:
private void RegisterPromptScriptBlock(System.Web.UI.Control ctrl, string aMsg)
{
string scriptKey = ctrl.UniqueID +"_OnSubmit";
string scriptBlock = String.Format("<SCRIPT ID=\"{0}\" FOR=\"document.all.{1}\" " +
								 " language=\"JAVASCRIPT\" event=\"onsubmit\"> "+
								 "\n\twindow.confirm('{2}');\"\n</SCRIPT>",
								 scriptKey, ctrl.UniqueID, aMsg);
 
if (!this.IsClientScriptBlockRegistered(scriptKey)
	 this.RegisterClientScriptBlock(scriptBlock);
}

2. in OnLoad, I register whatever buttons must be prompted:

PHP:
RegisterPromptScriptBlock(deleteButton, "Delete Current Item?");
RegisterPromptScriptBlock(postButton, "Post Changes?");

Make sense?
 
Hi,

You may like to try the following:

1. Add a button to your form
<asp:button id="btnDelete" runat="server" CssClass="button" Text="Delete"></asp:button>

2. Add a client side function to the button
Private Sub Page_Load()
BtnDelete.Attributes.Add("onClick", "return FrmSubmit();")
end sub

3. Add a javascript function in your html code

<script language="javascript">

function FrmSubmit()
{

if (confirm("do you wish to delete ?"))
{
return true;
}else {
return false;
}

}
</script>

simple, isn't it ? or am i missing something ?


HTH
Jitesh Sinha
 
Back
Top