473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overriding client-side onclick of Button

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 :)
Nov 18 '05 #1
2 3269

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.

--
Stuart
See headers for PGP Key.
Politicians are like nappies: they should be changed regularly, and
for the same reason

Nov 18 '05 #2
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Stuart Hemming (via DFN-CIS NetNews Service)"
<sh******@estatecomputers.com> wrote in message
news:14***********************@estatecomputers.com ...

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.

--
Stuart
See headers for PGP Key.
Politicians are like nappies: they should be changed regularly, and
for the same reason
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
3
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static...
4
by: jibran | last post by:
Hello. I have wrapped the DataGrid control with my own class (SmartDataGrid) adding some necessary functionality. My current webform has 2 SmartDataGrids. The first is populated by selected...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
4
by: drb | last post by:
Hi, I'm hoping this is a stupid question, so I apologize in advance. After creating WSDL files from scratch for a new web service, I used the WSDL.exe tool to generate C# stubs for both the...
4
by: Raja Raman Sundararajan | last post by:
Hello guys, I have data stored in the database which has special characters like <, etc. Case 1: Whenever I wanted to present the output to a browser I need to escape these special characters...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.