Javascript Submit Problem

Discussion in 'ASP .Net' started by czuvich, Nov 29, 2006.

  1. czuvich

    czuvich Guest

    All,

    I seem to be having an issue with calling the
    'document.getElementById()' in my javascript function. I have a
    checkbox where runat=server is set since I am using it in the code
    behind (this is asp.net 2.0). I then have a submit button. I want to
    check to see if this checkbox is checked in the javascript button. But
    since the control is runat=server, whenever I call the
    getElementById(checkbox) it returns a null value (can't recognize the
    control).

    Of course whenever I remove the runat=server it runs fine, but I need
    this to be able to use in my code behind. I was wanting to know if
    there is a way to reference a server object in javascript. If so.. how?
    Thanks.
    czuvich, Nov 29, 2006
    #1
    1. Advertising

  2. Hi,

    czuvich wrote:
    > All,
    >
    > I seem to be having an issue with calling the
    > 'document.getElementById()' in my javascript function. I have a
    > checkbox where runat=server is set since I am using it in the code
    > behind (this is asp.net 2.0). I then have a submit button. I want to
    > check to see if this checkbox is checked in the javascript button. But
    > since the control is runat=server, whenever I call the
    > getElementById(checkbox) it returns a null value (can't recognize the
    > control).
    >
    > Of course whenever I remove the runat=server it runs fine, but I need
    > this to be able to use in my code behind. I was wanting to know if
    > there is a way to reference a server object in javascript. If so.. how?
    > Thanks.


    What HTML code gets produced? What ID do you use in your getElementById?
    Often, people confuse the control's ID and the ClientID. The ID is the
    one you give to the control. However, this ID gets modified by ASP.NET,
    because it must ensure that the ID is unique throughout the page. This
    is why your server-side code must pass the ClientID to the client-side
    code, using ClientScriptManager.RegisterClientScriptBlock

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Nov 29, 2006
    #2
    1. Advertising

  3. czuvich

    czuvich Guest

    Laurent Bugnion wrote:
    > Hi,
    >
    > czuvich wrote:
    > > All,
    > >
    > > I seem to be having an issue with calling the
    > > 'document.getElementById()' in my javascript function. I have a
    > > checkbox where runat=server is set since I am using it in the code
    > > behind (this is asp.net 2.0). I then have a submit button. I want to
    > > check to see if this checkbox is checked in the javascript button. But
    > > since the control is runat=server, whenever I call the
    > > getElementById(checkbox) it returns a null value (can't recognize the
    > > control).
    > >
    > > Of course whenever I remove the runat=server it runs fine, but I need
    > > this to be able to use in my code behind. I was wanting to know if
    > > there is a way to reference a server object in javascript. If so.. how?
    > > Thanks.

    >
    > What HTML code gets produced? What ID do you use in your getElementById?
    > Often, people confuse the control's ID and the ClientID. The ID is the
    > one you give to the control. However, this ID gets modified by ASP.NET,
    > because it must ensure that the ID is unique throughout the page. This
    > is why your server-side code must pass the ClientID to the client-side
    > code, using ClientScriptManager.RegisterClientScriptBlock
    >
    > HTH,
    > Laurent
    > --
    > Laurent Bugnion, GalaSoft
    > Software engineering: http://www.galasoft-LB.ch
    > PhotoAlbum: http://www.galasoft-LB.ch/pictures
    > Support children in Calcutta: http://www.calcutta-espoir.ch


    Hey,
    Thanks for the reply. How interesting is that.... The ID it gave it in
    the html was ctl00_ContentPlaceHolder1_ckbUseDate. I'll do some more
    research into the RegisterClientScriptBlock. Right now, I have a .js
    file. Will this ID change each time the page is run.. or will that name
    be constant? Thanks!
    czuvich, Nov 29, 2006
    #3
  4. Hi,

    czuvich wrote:

    > Hey,
    > Thanks for the reply. How interesting is that.... The ID it gave it in
    > the html was ctl00_ContentPlaceHolder1_ckbUseDate. I'll do some more
    > research into the RegisterClientScriptBlock. Right now, I have a .js
    > file. Will this ID change each time the page is run.. or will that name
    > be constant? Thanks!


    To be honest, not very interesting ;-) It's what I told you. You give an
    ID to your control on the ASPX page, but if your control runs inside
    another control (user control), the framework creates a unique ID named
    ClientID. In your code behind, you must get the ClientID and pass it to
    JavaScript

    Example with a Label in a Control:

    string strScript = "<script type='text/javascript'>"
    + Environment.NewLine
    + "var strLabelClientId = " + lblTest.ClientID + ";"
    + Environment.NewLine
    + "</script>";

    this.Page.ClientManager.RegisterClientScriptBlock( typeof( MyControl ),
    "myScript", strScript );

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Nov 29, 2006
    #4
  5. czuvich

    czuvich Guest

    Thanks a lot! I got it working.

    Laurent Bugnion wrote:
    > Hi,
    >
    > czuvich wrote:
    >
    > > Hey,
    > > Thanks for the reply. How interesting is that.... The ID it gave it in
    > > the html was ctl00_ContentPlaceHolder1_ckbUseDate. I'll do some more
    > > research into the RegisterClientScriptBlock. Right now, I have a .js
    > > file. Will this ID change each time the page is run.. or will that name
    > > be constant? Thanks!

    >
    > To be honest, not very interesting ;-) It's what I told you. You give an
    > ID to your control on the ASPX page, but if your control runs inside
    > another control (user control), the framework creates a unique ID named
    > ClientID. In your code behind, you must get the ClientID and pass it to
    > JavaScript
    >
    > Example with a Label in a Control:
    >
    > string strScript = "<script type='text/javascript'>"
    > + Environment.NewLine
    > + "var strLabelClientId = " + lblTest.ClientID + ";"
    > + Environment.NewLine
    > + "</script>";
    >
    > this.Page.ClientManager.RegisterClientScriptBlock( typeof( MyControl ),
    > "myScript", strScript );
    >
    > HTH,
    > Laurent
    > --
    > Laurent Bugnion, GalaSoft
    > Software engineering: http://www.galasoft-LB.ch
    > PhotoAlbum: http://www.galasoft-LB.ch/pictures
    > Support children in Calcutta: http://www.calcutta-espoir.ch
    czuvich, Nov 29, 2006
    #5
    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. Mark
    Replies:
    1
    Views:
    1,670
  2. Ghafran Abbas
    Replies:
    0
    Views:
    4,185
    Ghafran Abbas
    Oct 12, 2004
  3. Joop
    Replies:
    5
    Views:
    571
  4. ljlolel
    Replies:
    2
    Views:
    493
  5. jatrojoomla
    Replies:
    1
    Views:
    165
Loading...

Share This Page