Hook JScript Client Events to Server Controls

Discussion in 'ASP .Net' started by David Pifer via .NET 247, May 2, 2004.

  1. Hello,

    Fooling around with ASP.Net and wanted to know if this ispossible...I wanted to attach a client event to a ASP:Checkboxcontrol. I got this to work by adding this line of code to theSub Page_Load

    Code:
    chkBoxProduct.Attributes.Add("OnClick", "testFunction();")

    This function just displays an alert box that says the basic'Hello World!'

    That is fine...I got this to work when I ran the WebForm1.aspxpage however, this is what I can not get to work let's say Ichange the JavaScript function that is in between the <HEAD>tags on the HTML page to have a parameter and for simplicitywant to show the name of the checkbox I am checking.

    Code:
    function testFunction(chkBox)
    {
    alert(chkBox.Name);
    }

    Here is the code from the WebForm1.aspx page

    Code:
    <asp:CheckBox ID="chkBoxProduct" OnClick="testFunction(this)"Runat="server"></asp:CheckBox>

    and then I try to do this in the Sub Page_Load

    Code:
    chkBoxProduct.Attributes.Add("OnClick", "testFunction(chkBox);")

    When I run the program and click on the checkbox I get 'chkBox isundefined'. So can I only add attributes to the controls thattake no parameters?

    Thanks for the help.
    Dave Pifer

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>SAhJiuqd6U+I4dJC5KipHA==</Id>
    David Pifer via .NET 247, May 2, 2004
    #1
    1. Advertising

  2. David Pifer via .NET 247

    David Jessee Guest

    2 options:
    1) chkBoxProduct.Attributes.Add("OnClick", "testFunction(this);")
    2) chkBoxProduct.Attributes.Add("OnClick",
    "testFunction("+chkBoxProduct.ClientId+");")


    "David Pifer via .NET 247" <> wrote in message
    news:...
    Hello,

    Fooling around with ASP.Net and wanted to know if this is possible...I
    wanted to attach a client event to a ASP:Checkbox control. I got this to
    work by adding this line of code to the Sub Page_Load

    Code:
    chkBoxProduct.Attributes.Add("OnClick", "testFunction();")

    This function just displays an alert box that says the basic 'Hello World!'

    That is fine...I got this to work when I ran the WebForm1.aspx page however,
    this is what I can not get to work let's say I change the JavaScript
    function that is in between the <HEAD> tags on the HTML page to have a
    parameter and for simplicity want to show the name of the checkbox I am
    checking.

    Code:
    function testFunction(chkBox)
    {
    alert(chkBox.Name);
    }

    Here is the code from the WebForm1.aspx page

    Code:
    <asp:CheckBox ID="chkBoxProduct" OnClick="testFunction(this)"
    Runat="server"></asp:CheckBox>

    and then I try to do this in the Sub Page_Load

    Code:
    chkBoxProduct.Attributes.Add("OnClick", "testFunction(chkBox);")

    When I run the program and click on the checkbox I get 'chkBox is
    undefined'. So can I only add attributes to the controls that take no
    parameters?

    Thanks for the help.
    Dave Pifer

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>SAhJiuqd6U+I4dJC5KipHA==</Id>
    David Jessee, May 2, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    4
    Views:
    1,245
    Jon Maz
    Sep 9, 2004
  2. Maxwell2006
    Replies:
    5
    Views:
    2,371
    Steven Cheng[MSFT]
    Mar 7, 2006
  3. John Bentley
    Replies:
    14
    Views:
    332
    Jim Ley
    Feb 5, 2004
  4. VK
    Replies:
    4
    Views:
    344
    Thomas 'PointedEars' Lahn
    Jan 12, 2006
  5. Eran
    Replies:
    1
    Views:
    285
Loading...

Share This Page