overriding client-side onclick of Button

G

Guest

Hi,
i'm trying to create a custom Button user control which will be derived from System.Web.UI.WebControls.Button. the normal server side Button class creates some client side javascript code for its onclick event. it's something like:

<input type="submit" name="Button1" value="Button" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="Button1" />

what i want to do is to be able to change the onclick attribute of the button. i tried some different methods but i ended up with a button whose onclick event has the custom javascript code i call followed by the javascript code created by the standard Button control. the code for my custom button control is like this:

public class CustomButton : System.Web.UI.WebControls.Button
{
protected override void Render(HtmlTextWriter tw){
this.Attributes["onclick"]="alert('ZZZ');";
base.Render(tw);
}
}

this button control creates the following html code:
<input type="submit" name="CustomButton1" value="click me" onclick="alert('ZZZ');if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="CustomButton1" />

as you see, the onclick attribute of the button has this Page_ClientValidate() code which i don't want. i want only the alert('ZZZ') function to be rendered.

i tried to override OnPreRender method instead of the Render method in the CustomButton class but the result was the same.
i also tried
this.Attributes.Add("onclick", "alert('ZZZ');");
instead of
this.Attributes["onclick"]="alert('ZZZ');";
the result was the same.

Then i tried
tw.AddAttribute("onclick", "alert('ZZZ');"); // tw is the HtmlTextWriter parameter of the Render method.

but this time what i got was a button with two onclick attributes. here is the html code:
<input onclick="alert('ZZZ');" type="submit" name="CustomButton1" value="ccc" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="CustomButton1" />

What i simply would like to do is to create the onclick attribute of the button freely. I do not want the code created by the standard Button control which calls the Page_ClientValidate() function. I must be able to do this, right?
Does anyone have an idea what i'm doing wrong? Any help is appreciated. I really can't figure out what i should do...
Thanks in advance :)
 
S

Stuart Hemming (via DFN-CIS NetNews Service)

S> What i simply would like to do is to create the onclick
S> attribute of the button freely. I do not want the code created by
S> the standard Button control which calls the Page_ClientValidate()
S> function. I must be able to do this, right?
S> Does anyone have an idea what i'm doing wrong? Any help is
S> appreciated. I really can't figure out what i should do...
S> Thanks in advance :)

I can't offer a 'fix' for this, but I can suggest a workaround. Try
this...

public class CustomButton : System.Web.UI.WebControls.Button
{
protected override void Render(HtmlTextWriter tw){
this.Attributes["onclick"]="return;";
this.Attributes["onclick"]="alert('ZZZ');";
base.Render(tw);
}
}

You'll still have the standard code but it'll be separated from yours
by a 'return' statement so that it will never execute.

I haven't tested this but give it a try.
 
P

Peter Blum

In this case, you effectively want to turn off the code for client-side
validation. Simply set the Button.CausesValidation property to false and
assign your onclick code in the traditional way in the Page_Load method:
Button1.Attributes.Add("onclick", "code");

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Stuart Hemming (via DFN-CIS NetNews Service)"

S> What i simply would like to do is to create the onclick
S> attribute of the button freely. I do not want the code created by
S> the standard Button control which calls the Page_ClientValidate()
S> function. I must be able to do this, right?
S> Does anyone have an idea what i'm doing wrong? Any help is
S> appreciated. I really can't figure out what i should do...
S> Thanks in advance :)

I can't offer a 'fix' for this, but I can suggest a workaround. Try
this...

public class CustomButton : System.Web.UI.WebControls.Button
{
protected override void Render(HtmlTextWriter tw){
this.Attributes["onclick"]="return;";
this.Attributes["onclick"]="alert('ZZZ');";
base.Render(tw);
}
}

You'll still have the standard code but it'll be separated from yours
by a 'return' statement so that it will never execute.

I haven't tested this but give it a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top