Hi,
i'm trying to create a custom Button user control which will be derived from System.Web.UI.W ebControls.Butt on. 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_Cl ientValidate) == 'function') Page_ClientVali date(); " language="javas cript" 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.W ebControls.Butt on
{
protected override void Render(HtmlText Writer tw){
this.Attributes["onclick"]="alert('ZZZ'); ";
base.Render(tw) ;
}
}
this button control creates the following html code:
<input type="submit" name="CustomBut ton1" value="click me" onclick="alert( 'ZZZ');if (typeof(Page_Cl ientValidate) == 'function') Page_ClientVali date(); " language="javas cript" id="CustomButto n1" />
as you see, the onclick attribute of the button has this Page_ClientVali date() 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="CustomBut ton1" value="ccc" onclick="if (typeof(Page_Cl ientValidate) == 'function') Page_ClientVali date(); " language="javas cript" id="CustomButto n1" />
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_ClientVali date() 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 :)