473,795 Members | 3,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_ClientVali date()
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.W ebControls.Butt on
{
protected override void Render(HtmlText Writer 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.CausesVa lidation property to false and
assign your onclick code in the traditional way in the Page_Load method:
Button1.Attribu tes.Add("onclic k", "code");

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlu m.com
Creator of "Profession al Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Stuart Hemming (via DFN-CIS NetNews Service)"
<sh******@estat ecomputers.com> wrote in message
news:14******** *************** @estatecomputer s.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_ClientVali date()
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.W ebControls.Butt on
{
protected override void Render(HtmlText Writer 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
3794
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 most operators, they are conspicuously absent for the logical "or" and "and". I'm guessing that the reason for this is that these operators short-circuit if their first operand answers the whole question? Would it be possible to allow...
3
4200
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 the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
9
6362
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 didn't work. I discovered that this seemed to work: HTMLDocument.prototype.write= my_doc_write ; Why does HTMLDocument work here but not Document? Will this second
3
10987
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 library. I want to override this by having another function which is exactly similar in signature including name. When somebody call this function, control should automatically come to my overriddent function. Inside this function, based on...
4
2544
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 information from a drop down box. The second datagrid is populated by viewing details from a row of the first datagrid. When editing individual rows (in either datagrid), clicking the enter key in any editable textbox calls my inital search...
3
1557
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
5302
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 web service server methods and the client proxy methods. The tool created the server methods as abstract member methods of an abstract class that derived from System.Web.Services.WebService. I *thought* what I needed to do was create another...
4
1788
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 into the browser equivalent like &lt; &gt; etc.( for example by using the cgi module) Case 2: Whenever I wanted to present the output to some client other than a browser, I wanted to present the data as it is stored in the database.
10
105202
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
12
1559
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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.