473,765 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting an HTML button's onclick property client-side

Hi,

VS.NET 2003 on WinXPPro, both with all the latest patches and updates,
etc...

I've got a very simple WebForm which is used either to add a new record to a
SQL Server database or edit a record. Depending on whether I'm adding or
editing, I need to do slightly different validation, so I'm trying to set
the form's Save button's onclick property dynamically at run-time
client-side with JavaScript, as follows:

document.frmCal endar.cmdSave.o nClick = "javascript:val idateForm('Add' );";

or

document.frmCal endar.cmdSave.o nClick = "javascript:val idateForm('Edit ');";

function validateForm(ps trMode)
{
switch (pstrMode)
{
case "Add" :
{
//validate the form and add the record
break;
}
case "Edit" :
{
//validate the form and edit the record
}
}
}

Problem is that the button does not respond to the onClick event. If I
create another button and set its onClick to
"javascript:ale rt(document.frm Calendar.cmdSav e.onClick);" it shows the
correct string in the first button's onClick property. It's almost like the
first button doesn't know to respond to the click event.

I have found at least five ways to achieve the same effect using different
functionality. I'm just interested to know if the above is possible, more
from an intellectual exercise and for my own interest.

Thanks,

Mark Rae
Nov 18 '05 #1
4 2578
If the button is an object in a control that implements INamingContaine r
then you need to retreive the control Id by using the ClientId property.

The best way to do this is in your code.

C#:
btnBtnName.Attr ibutes.Add("onC lick", "validateForm(' Edit');");

If you needed to make a reference to that button then you would do:
btnBtnName.Attr ibutes.Add("onC lick", "doSomethin g(" + btnBtnName.Clie ntId +
");");

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ua******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

VS.NET 2003 on WinXPPro, both with all the latest patches and updates,
etc...

I've got a very simple WebForm which is used either to add a new record to
a
SQL Server database or edit a record. Depending on whether I'm adding or
editing, I need to do slightly different validation, so I'm trying to set
the form's Save button's onclick property dynamically at run-time
client-side with JavaScript, as follows:

document.frmCal endar.cmdSave.o nClick = "javascript:val idateForm('Add' );";

or

document.frmCal endar.cmdSave.o nClick = "javascript:val idateForm('Edit ');";

function validateForm(ps trMode)
{
switch (pstrMode)
{
case "Add" :
{
//validate the form and add the record
break;
}
case "Edit" :
{
//validate the form and edit the record
}
}
}

Problem is that the button does not respond to the onClick event. If I
create another button and set its onClick to
"javascript:ale rt(document.frm Calendar.cmdSav e.onClick);" it shows the
correct string in the first button's onClick property. It's almost like
the
first button doesn't know to respond to the click event.

I have found at least five ways to achieve the same effect using different
functionality. I'm just interested to know if the above is possible, more
from an intellectual exercise and for my own interest.

Thanks,

Mark Rae

Nov 18 '05 #2
"Steven" <ms******@berko vitz.org> wrote in message
news:eF******** ******@TK2MSFTN GP10.phx.gbl...
If the button is an object in a control that implements INamingContaine r
then you need to retreive the control Id by using the ClientId property.

The best way to do this is in your code.

C#:
btnBtnName.Attr ibutes.Add("onC lick", "validateForm(' Edit');");

If you needed to make a reference to that button then you would do:
btnBtnName.Attr ibutes.Add("onC lick", "doSomethin g(" + btnBtnName.Clie ntId + ");");


Like I said, "dynamicall y at run-time client-side with JavaScript" ...
Nov 18 '05 #3
javascript and the dom are case sensitive (while html isn't), also the
onclick property of an html element wants a reference to a function, not a
string.

document.frmCal endar.cmdSave.o nClick =
"javascript:val idateForm('Add' );";

while valid javascript, creates a new property named onClick, whose value is
the string "javascript:val idateForm('Add' );" instead try:

document.frmCal endar.cmdSave.o nclick = function() { return
validateForm('A dd');};

which set the builtin onclick property to a function reference. note: a new
wrapper function is required to pass the 'Add' arg to validateForm, if
validateForm needed no arguments, then it could have been referenced
directly.

-- bruce (sqlwork.com)

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ua******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

VS.NET 2003 on WinXPPro, both with all the latest patches and updates,
etc...

I've got a very simple WebForm which is used either to add a new record to a SQL Server database or edit a record. Depending on whether I'm adding or
editing, I need to do slightly different validation, so I'm trying to set
the form's Save button's onclick property dynamically at run-time
client-side with JavaScript, as follows:

document.frmCal endar.cmdSave.o nClick = "javascript:val idateForm('Add' );";

or

document.frmCal endar.cmdSave.o nClick = "javascript:val idateForm('Edit ');";

function validateForm(ps trMode)
{
switch (pstrMode)
{
case "Add" :
{
//validate the form and add the record
break;
}
case "Edit" :
{
//validate the form and edit the record
}
}
}

Problem is that the button does not respond to the onClick event. If I
create another button and set its onClick to
"javascript:ale rt(document.frm Calendar.cmdSav e.onClick);" it shows the
correct string in the first button's onClick property. It's almost like the first button doesn't know to respond to the click event.

I have found at least five ways to achieve the same effect using different
functionality. I'm just interested to know if the above is possible, more
from an intellectual exercise and for my own interest.

Thanks,

Mark Rae

Nov 18 '05 #4
"bruce barker" <no***********@ safeco.com> wrote in message
news:eb******** ******@tk2msftn gp13.phx.gbl...
document.frmCal endar.cmdSave.o nclick = function() { return
validateForm('A dd');};


Excellent! Works perfectly :-)

Thanks very much.
Nov 18 '05 #5

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

Similar topics

18
18417
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
2
2525
by: Richard Morse | last post by:
Hi! I have an aspx that I've created which has an asp:Button in it. I would like to be able to change the OnClick handler at runtime (basically, I want this form to either edit or create a record, and I want to change the procedure it calls depending on what it should be doing). However, apparently the .OnClick member is read only? (Sample:) <script language="jscript"> function do_edit() {
3
1632
by: Janaka | last post by:
I've seen and used some samples where you can set the onclick attrubute to a Button control to get it to do some javascript a la : btnUse.Attributes = "DoSomeJS()"; However when i try and get this to work on certain controls such as the ListItem in a RadioButtonList it just doesn't do anything? Does this attributes property only render for certain controls?
3
6760
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the following default properties: Height = 32px, Width = 144px. I declare the Width property in my control as... \\\
4
2005
by: Sathyaish | last post by:
I am no JavaScript progammer, and unfortunately am having to babysit an old code base that has a lot of JavaScript in it. I have two questions: (1) Can two HTML controls have the same name? It looks like the obvious answer is NO. (2) What if? What if the developer has given two HTML controls the same name, i.e has created the same button more than once with exactly the
3
2006
by: Alex | last post by:
Hello. First, with AJAX I will get a remote web page into a string. Thus, a string will contain HTML tags and such. I will need to extract text from one <span> for which I know the ID the inner text. Is it possible to access in this way "string variable".getElementByID() somehow? Thank you.
3
1888
by: lcjohnso | last post by:
Hi all, Does anyone know if there is an easy way to create the html representation of an HTMLElement object in javascript? I'm attempting to update the innerHTML property of a div element to allow user entries to presist when the the input display pane is changed. Basically I have several different div panes that users can rotate through when entering values of a simulated form whose values are then parsed and used to dynamically...
1
19329
by: ykong1214 | last post by:
In my project, there is a single select tag, <html:select property="userName" size="7"> <html:options collection="UserList" property="value" labelProperty="label" /> </html:select> <br><br> <input type="button" value="Select" onClick="go()">&nbsp;&nbsp;&nbsp; <input type="button" value="Cancel" onClick="cancel()"> when user choose any one and click "select" button, the value will be transfered to original window:
2
2327
by: Bob | last post by:
Hi, in aspx file, i defined this: <input id="Button2" type="button" value="button" runat="server" onclick="klik()"/> This 'onclick' event is a clientclick (starting the Javascript function "klik()" ). I want to do a 'server onclick', just like the <asp:Buttoncontrol, but it
3
3010
by: Sunny | last post by:
Hi, Can someone tell me, How to load the Body Html from a text file that contains javascript. to Manage my files I am creating an Index Page. <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title></title>
0
9566
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
9393
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
8830
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
7371
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
6646
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
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
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.