After looking around the internet I could not find anybody adding an
onClick event or an onSubmit event to a asp.net Wizard control ( asp:Wizard ). After looking into it further I found that you can not do an Wizard.Finishbutton.Attributes.Add().
You can edit the wizard control template and create your own custom template but this was overkill for what I wanted to do.
So I decided to use a JavaScript Event Listener to capture my FinishButtonClick.
The code for this is actually pretty simple: (
please see the Zip File for complete code sample)
First on the server side code I added a call to a new function I created called AddJsEventListeners
in the wizards NextButtonClick event like so:
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
AddJsEventListeners();
}
Next I created a function to build my JavaScript event listener and register it with page.
Note: This
attachEvent javascript works on IE only
for a cross browser listener you will need to find additional javascript on the web.
private void AddJsEventListeners()
{
StringBuilder sb = new StringBuilder();
sb.Length = 0; //wipes all information in stringbuilder
if (!Page.ClientScript.
IsStartupScriptRegistered(Page.GetType(), "FinishButton"))
{
sb.Append("<script language='javascript'> \r\n");
sb.Append("function finishButtonClicked(evt) { \r\n");
sb.Append(" alert('finishbutton was clicked.'); \r\n");
sb.Append("} \r\n");
sb.Append("</script> \r\n");
sb.Append("<script language='javascript'> \r\n");
sb.Append(" var finishButton = document.getElementById('Wizard1_FinishNavigationTemplateContainerID_FinishButton'); \r\n");
sb.Append(" // IE Code Only \r\n");
sb.Append(" finishButton.attachEvent('onclick',finishButtonClicked); \r\n");
sb.Append(" \r\n");
sb.Append("</script> \r\n");
this.Page.ClientScript.
RegisterStartupScript(Page.GetType(), "FinishButton", sb.ToString());
}
}
posted @ Monday, June 18, 2007 11:15 PM