Javascript and DropDownLists question

Discussion in 'ASP .Net' started by =?Utf-8?B?QWxCcnVBbg==?=, Aug 1, 2007.

  1. I have three dropdownlists in an AJAX UpdatePanel and I need to return the
    selected index for two of them; the problem is that I'm only able to return
    the selected index for the first dropdownlist of the three and it is the one
    that I don't need. The dropdownlists are named, in order, ddlEntityType,
    ddlEntityGroups and ddlProcesses. The Javascript for attempting to read the
    selected index is as follows:

    var entityTypeIndex =
    document.getElementById('ct100$ContentPlaceHolder1$ddlEntityType').selectedIndex; // this one works
    var entityGroupIndex =
    document.getElementById('ct100$ContentPlaceHolder1$ddlEntityGroups').selectedIndex; // this doesn't work
    var processesIndex =
    document.getElementById('ct100$ContentPlaceHolder1$ddlProcesses').selectedIndex; // this doesn't work

    Trying all three in the Immediate Window, the first one still works; the
    message I get back for the other two is: Object required

    Why won't it find the dropdownlists for the two that I need to return the
    SelectedIndex from?
     
    =?Utf-8?B?QWxCcnVBbg==?=, Aug 1, 2007
    #1
    1. Advertising

  2. Hi,

    AlBruAn wrote:
    > I have three dropdownlists in an AJAX UpdatePanel and I need to return the
    > selected index for two of them; the problem is that I'm only able to return
    > the selected index for the first dropdownlist of the three and it is the one
    > that I don't need. The dropdownlists are named, in order, ddlEntityType,
    > ddlEntityGroups and ddlProcesses. The Javascript for attempting to read the
    > selected index is as follows:
    >
    > var entityTypeIndex =
    > document.getElementById('ct100$ContentPlaceHolder1$ddlEntityType').selectedIndex; // this one works
    > var entityGroupIndex =
    > document.getElementById('ct100$ContentPlaceHolder1$ddlEntityGroups').selectedIndex; // this doesn't work
    > var processesIndex =
    > document.getElementById('ct100$ContentPlaceHolder1$ddlProcesses').selectedIndex; // this doesn't work
    >
    > Trying all three in the Immediate Window, the first one still works; the
    > message I get back for the other two is: Object required
    >
    > Why won't it find the dropdownlists for the two that I need to return the
    > SelectedIndex from?


    The unique name for the client, aka ClientId is generated by the server.
    You must make sure that 'ct100$ContentPlaceHolder1$ddlEntityGroups' and
    the other are really named this way. To make this sure, view the HTML
    source on the web client. In your server-side code, use the control's
    ClientId to generate the JavaScript code to get it.

    HTH,
    Laurent
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering, Blog: http://www.galasoft.ch
    PhotoAlbum: http://www.galasoft.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, MVP, Aug 1, 2007
    #2
    1. Advertising

  3. "Laurent Bugnion, MVP" <> wrote in message
    news:%...

    >> var entityTypeIndex =
    >> document.getElementById('ct100$ContentPlaceHolder1$ddlEntityType').selectedIndex;
    >> // this one works
    >> var entityGroupIndex =
    >> document.getElementById('ct100$ContentPlaceHolder1$ddlEntityGroups').selectedIndex;
    >> // this doesn't work
    >> var processesIndex =
    >> document.getElementById('ct100$ContentPlaceHolder1$ddlProcesses').selectedIndex;
    >> // this doesn't work


    var entityTypeIndex =
    document.getElementById('<%=ddlEntityType.ClientID%>').selectedIndex;
    var entityGroupIndex =
    document.getElementById('<%=ddlEntityGroups.ClientID%>').selectedIndex;
    var processesIndex =
    document.getElementById('<%=ddlProcesses.ClientID%>').selectedIndex;


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Aug 1, 2007
    #3
  4. Thanks for the tip. I'd inadvertently typed the number "1" rather than the
    letter "l"; I thought I'd copied and pasted it, but apparently not.

    "Laurent Bugnion, MVP" wrote:

    > Hi,
    >
    > AlBruAn wrote:
    > > I have three dropdownlists in an AJAX UpdatePanel and I need to return the
    > > selected index for two of them; the problem is that I'm only able to return
    > > the selected index for the first dropdownlist of the three and it is the one
    > > that I don't need. The dropdownlists are named, in order, ddlEntityType,
    > > ddlEntityGroups and ddlProcesses. The Javascript for attempting to read the
    > > selected index is as follows:
    > >
    > > var entityTypeIndex =
    > > document.getElementById('ct100$ContentPlaceHolder1$ddlEntityType').selectedIndex; // this one works
    > > var entityGroupIndex =
    > > document.getElementById('ct100$ContentPlaceHolder1$ddlEntityGroups').selectedIndex; // this doesn't work
    > > var processesIndex =
    > > document.getElementById('ct100$ContentPlaceHolder1$ddlProcesses').selectedIndex; // this doesn't work
    > >
    > > Trying all three in the Immediate Window, the first one still works; the
    > > message I get back for the other two is: Object required
    > >
    > > Why won't it find the dropdownlists for the two that I need to return the
    > > SelectedIndex from?

    >
    > The unique name for the client, aka ClientId is generated by the server.
    > You must make sure that 'ct100$ContentPlaceHolder1$ddlEntityGroups' and
    > the other are really named this way. To make this sure, view the HTML
    > source on the web client. In your server-side code, use the control's
    > ClientId to generate the JavaScript code to get it.
    >
    > HTH,
    > Laurent
    > --
    > Laurent Bugnion [MVP ASP.NET]
    > Software engineering, Blog: http://www.galasoft.ch
    > PhotoAlbum: http://www.galasoft.ch/pictures
    > Support children in Calcutta: http://www.calcutta-espoir.ch
    >
     
    =?Utf-8?B?QWxCcnVBbg==?=, Aug 1, 2007
    #4
  5. Hi,

    AlBruAn wrote:
    > Thanks for the tip. I'd inadvertently typed the number "1" rather than the
    > letter "l"; I thought I'd copied and pasted it, but apparently not.


    That's why you shouldn't. You should use ClientID instead. This way, if
    the containing control's name changes, you don't need to change your code.

    HTH,
    Laurent
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering, Blog: http://www.galasoft.ch
    PhotoAlbum: http://www.galasoft.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, MVP, Aug 1, 2007
    #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. Neil

    DropDownLists and NULL values

    Neil, Jan 30, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    10,897
    sloan
    Jan 31, 2006
  2. Neil
    Replies:
    2
    Views:
    391
  3. =?Utf-8?B?QmFzIFBhYXA=?=

    Problem with FormView and DropDownLists

    =?Utf-8?B?QmFzIFBhYXA=?=, Apr 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    10,676
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 11, 2006
  4. =?Utf-8?B?cm9kY2hhcg==?=

    Panels, DropDownLists,RadioButtons and Bears

    =?Utf-8?B?cm9kY2hhcg==?=, Aug 21, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    =?Utf-8?B?cm9kY2hhcg==?=
    Aug 21, 2006
  5. Don Miller
    Replies:
    0
    Views:
    403
    Don Miller
    Jan 23, 2007
Loading...

Share This Page