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
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