Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Checkbox, AutoPostBack & JavaScript

Reply
Thread Tools

Checkbox, AutoPostBack & JavaScript

 
 
Chris Walls
Guest
Posts: n/a
 
      12-29-2004
I have a CheckBox that when clicked, needs to first invoke a client-side
JavaScript function, then post back to the server. My ASP.NET code looks
like the following:

<asp:CheckBox ID="MyCheckBox" Runat="server" AutoPostBack="True"
OnCheckedChanged="OnMyCheckBoxCheckedChanged" />

I normally add JS code to a control by executing the following code in the
Page_Load method:

MyCheckBox.Attributes.Add("onclick", "DoSomethingFirst();");

However, the DoSomethingFirst() JS call is added to the generated SPAN tag
that surrounds the checkbox control, so my "DoSomethingFirst" function fires
*after* the AutoPostBack fires. How can I get my JS function to fire
*before* the AutoPostBack fires?

Thanks,
Chris




 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12-30-2004
Hi Chris,

Thanks for your posting. As for the checkbox post back problem, it is
caused by the asp.net CheckBox control is encapsulated and we're hard to do
some additional customize. Currently if you want to add some other
clientscript before the CheckBox post back, we may have two options:

1. Manualy create a new checkBox class which derived from the asp.net's
CheckBox control and override the render method so as to insert our own
script before the build in "AutoPostBack" script (__doPostBack)

2. Use the InputCheckBox control in the System.Web.UI.HtmlControls
namespace and we can add a serverchange event for it and also handler its
clientside "onclick" script event. But we need to manually post back the
page when user click it at clientside, for example:

<input type="checkbox" id="chkClient" runat="server" value="..."
name="chkClient" onclick="alert('hello');document.forms[0].submit();"
onserverchange="chkServer_CheckedChanged">

in page's codebehind we have :

protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
{
Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
}


Hope this helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



 
Reply With Quote
 
 
 
 
Chris Walls
Guest
Posts: n/a
 
      12-30-2004
The second option worked just fine. Thanks for the help.

"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:1FZz$(E-Mail Removed)...
> Hi Chris,
>
> Thanks for your posting. As for the checkbox post back problem, it is
> caused by the asp.net CheckBox control is encapsulated and we're hard to
> do
> some additional customize. Currently if you want to add some other
> clientscript before the CheckBox post back, we may have two options:
>
> 1. Manualy create a new checkBox class which derived from the asp.net's
> CheckBox control and override the render method so as to insert our own
> script before the build in "AutoPostBack" script (__doPostBack)
>
> 2. Use the InputCheckBox control in the System.Web.UI.HtmlControls
> namespace and we can add a serverchange event for it and also handler its
> clientside "onclick" script event. But we need to manually post back the
> page when user click it at clientside, for example:
>
> <input type="checkbox" id="chkClient" runat="server" value="..."
> name="chkClient" onclick="alert('hello');document.forms[0].submit();"
> onserverchange="chkServer_CheckedChanged">
>
> in page's codebehind we have :
>
> protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
> {
> Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
> }
>
>
> Hope this helps. Thanks.
>
> Regards,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12-31-2004
You're Welcome Chris,

HAPPY HOLIDAYS.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
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
dropdownlist autopostback with javascript confirm robert@orisoft.co.uk ASP .Net 5 10-11-2006 04:33 PM
AutoPostBack combined with javascript's onBeforeUnload causes "Unspecified error" Jonathan ASP .Net 0 06-14-2006 12:20 PM
javascript with Autopostback anoop ASP .Net 0 05-16-2006 08:43 AM
RadioButton AutoPostBack does not execute JavaScript M ASP .Net 1 03-27-2005 01:44 AM
autopostback - javascript intercept and cancel Susan van Houen ASP .Net 2 07-17-2003 08:15 PM



Advertisments