473,474 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Create 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.frmCalendar.cmdSave.onClick = "javascript:validateForm('Add');";

or

document.frmCalendar.cmdSave.onClick = "javascript:validateForm('Edit');";

function validateForm(pstrMode)
{
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:alert(document.frmCalendar.cmdSave.onC lick);" 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 2562
If the button is an object in a control that implements INamingContainer
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.Attributes.Add("onClick", "validateForm('Edit');");

If you needed to make a reference to that button then you would do:
btnBtnName.Attributes.Add("onClick", "doSomething(" + btnBtnName.ClientId +
");");

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ua*************@TK2MSFTNGP11.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.frmCalendar.cmdSave.onClick = "javascript:validateForm('Add');";

or

document.frmCalendar.cmdSave.onClick = "javascript:validateForm('Edit');";

function validateForm(pstrMode)
{
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:alert(document.frmCalendar.cmdSave.onC lick);" 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******@berkovitz.org> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
If the button is an object in a control that implements INamingContainer
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.Attributes.Add("onClick", "validateForm('Edit');");

If you needed to make a reference to that button then you would do:
btnBtnName.Attributes.Add("onClick", "doSomething(" + btnBtnName.ClientId + ");");


Like I said, "dynamically 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.frmCalendar.cmdSave.onClick =
"javascript:validateForm('Add');";

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

document.frmCalendar.cmdSave.onclick = function() { return
validateForm('Add');};

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*************@TK2MSFTNGP11.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.frmCalendar.cmdSave.onClick = "javascript:validateForm('Add');";

or

document.frmCalendar.cmdSave.onClick = "javascript:validateForm('Edit');";

function validateForm(pstrMode)
{
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:alert(document.frmCalendar.cmdSave.onC lick);" 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**************@tk2msftngp13.phx.gbl...
document.frmCalendar.cmdSave.onclick = function() { return
validateForm('Add');};


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
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...
2
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...
3
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...
3
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...
4
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...
3
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...
3
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...
1
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> ...
2
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...
3
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"...
0
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...
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...
1
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
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...
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.