|
-
Apr 10th, 2003, 12:43 PM
#1
Thread Starter
Registered User
button.attributes.Remove
Does anyone know how to use button1.attributes.Remove method. I can not get it to remove the onclick event in my button.
This button has it's causevalidation=True and I want to add a confirmation box when the button is clicked. .Net adds an onclick event to this button so it can validate. If I use an attributes.add method it will add another onclick event which will be ignored. I want to remove it then use the button1.attributes.add method to add the onclick method that calls a javascript function I wrote. This function will display the confirmation and still validate using the validation control.
Part of my code.
<asp:button id="saveIt" runat="server" Text="Insert New"></asp:button><FONT face="Arial Black">
sScript = "function ValConfirm()" & vbCrLf
sScript += "{" & vbCrLf
sScript += vbTab & "var valid = true;" & vbCrLf
sScript += vbTab & "if(typeof(Page_ClientValidate) == 'function')" & vbCrLf
sScript += vbTab & "{" & vbCrLf
sScript += vbTab & vbTab & "valid = Page_ClientValidate();" & vbCrLf
sScript += vbTab & "}" & vbCrLf
sScript += vbTab & "if(valid)" & vbCrLf
sScript += vbTab & "{" & vbCrLf
sScript += vbTab & vbTab & "return confirm('Are you sure you want to insert the new record?');" & vbCrLf
sScript += vbTab & "}" & vbCrLf
sScript += vbTab & "else" & vbCrLf
sScript += vbTab & "{" & vbCrLf
sScript += vbTab & vbTab & "return false;" & vbCrLf
sScript += vbTab & "}" & vbCrLf
sScript += "}" & vbCrLf
If (Not IsClientScriptBlockRegistered("MyScript")) Then
RegisterClientScriptBlock("MyScript", sScript)
End If
saveIt.Attributes.Remove("onclick") 'Here's my problem
saveIt.Attributes.Add("onClick", "ValConfirm();")
Thanks,
James
-
Apr 11th, 2003, 01:20 PM
#2
Thread Starter
Registered User
Continue to read if you want to know how to validate your form and display a confirmation box after a button is clicked (ie before submitting data to a db).
First set the causesvalidation=false property to your button. You don't need it since the javascript below calls the same function that would be called if the causesvalidation=true. DON'T remove your validation controls.
I have a Save and Update button in my form. The code below checks to see which button was clicked and then show the correct confirmation box. You will need to make minor modification to this code to make it work for you.
Here's the code
sScript = "function ConfirmButton(name)" & vbCrLf
sScript += "{" & vbCrLf
sScript += vbTab & "var Valid = true;" & vbCrLf
sScript += vbTab & "if(typeof(Page_ClientValidate) == 'function')" & vbCrLf
sScript += vbTab & "{" & vbCrLf
sScript += vbTab & vbTab & "Valid = Page_ClientValidate(); " & vbCrLf
sScript += vbTab & "}" & vbCrLf
sScript += vbTab & "if(Valid)" & vbCrLf
sScript += vbTab & "{" & vbCrLf
sScript += vbTab & vbTab & "var Status= true;" & vbCrLf
sScript += vbTab & vbTab & "if (name=='save'){" & vbCrLf
sScript += vbTab & vbTab & vbTab & "Status = confirm('Are you sure you want to save a new record?');}" & vbCrLf
sScript += vbTab & vbTab & "else {" & vbCrLf
sScript += vbTab & vbTab & vbTab & "Status = confirm('Are you sure you want to update the record?');}" & vbCrLf & vbCrLf
sScript += vbTab & vbTab & vbTab & "return Status;" & vbCrLf
sScript += vbTab & vbTab & "}" & vbCrLf
sScript += vbTab & "else" & vbCrLf
sScript += vbTab & "{" & vbCrLf
sScript += vbTab & vbTab & vbTab & "return false;" & vbCrLf
sScript += vbTab & "}" & vbCrLf
sScript += "}" & vbCrLf
sScript += "// -->" & vbCrLf
sScript += "</script>" & vbCrLf
'Register javascript to client
If (Not IsClientScriptBlockRegistered("MyScript")) Then
RegisterClientScriptBlock("MyScript", sScript)
End If
saveIt.Attributes.Add("onClick", "return ConfirmButton('save');")
update.Attributes.Add("onClick", "return ConfirmButton('update');")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|