By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,647 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,647 IT Pros & Developers. It's quick & easy.

Setting an HTML button's onclick property client-side

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.