Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Setting an HTML button's onclick property client-side

Reply
Thread Tools

Setting an HTML button's onclick property client-side

 
 
Mark Rae
Guest
Posts: n/a
 
      07-26-2004
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


 
Reply With Quote
 
 
 
 
Steven
Guest
Posts: n/a
 
      07-26-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      07-26-2004
"Steven" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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" ...


 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      07-26-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      07-26-2004
"bruce barker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


Excellent! Works perfectly

Thanks very much.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting the value of a Master page's control's property using a property of the Master page Nathan Sokalski ASP .Net 25 03-04-2010 03:42 AM
Ant setting property by regexp substitution on an existing property kevin cline Java 2 07-26-2008 02:21 PM
setting Onclick and setting the ClientID in behind code isn't working in Mozilla davidr@sharpesoft.com ASP .Net 2 08-22-2006 09:30 PM
setting Onclick and setting the ClientID in behind code isn't working in Mozilla davidr@sharpesoft.com ASP .Net 0 08-21-2006 11:55 PM
document.onclick=doIt() same as document.onclick=doIt ? bob Javascript 3 08-21-2003 12:14 PM



Advertisments