button.attributes.Remove

James

Well-known member
Joined
Oct 3, 2002
Messages
78
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 its 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") Heres my problem
saveIt.Attributes.Add("onClick", "ValConfirm();")

Thanks,

James
 
Try adding the remove on the preRender of page. I dont think the onClick event attribute is added during the load stage.
 
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 dont need it since the javascript below calls the same function that would be called if the causesvalidation=true. DONT 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.

Heres 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);")
 
Thanks a lot James ! That helped me too. It just seems like the first "<script>" tag was missing, but it works perfectly and it was just what I needed.
 


Write your reply...
Back
Top