DropDownLists in DataGrids-- can I dynamically name them?

Discussion in 'ASP .Net' started by Jim Bancroft, Dec 18, 2004.

  1. Jim Bancroft

    Jim Bancroft Guest

    Hi everyone,

    I'd like to put some DropDownLists in my DataGrid, populating each from a
    database query. I'm having a little trouble naming them based on the
    current record in the DataSet.

    For example, here's a snippet of what I have now in my DataGrid

    <ItemTemplate>
    <asp:DropDownList runat="server" id="lstCategories"
    DataValueField="CategoryID"
    DataTextField="Name"/>
    </ItemTemplate>

    I can create the DropDownLists ok, but I'm stuck choosing one id for the
    name, and having the DataGrid supply the final name for each. I'd like to
    name them myself, giving them ids like "lstCategories0," "lstCategories1,"
    "lstCategories2," etc. Is it possible to do that somehow?

    And, if I can do it, how can I set their DataSource properties and selected
    items in my code-behind file? The goal is to have each DropDown reading
    from the same data source, but with their selected item dependent on their
    id values.

    Thanks very much,

    -Jim
    Jim Bancroft, Dec 18, 2004
    #1
    1. Advertising

  2. Jim Bancroft

    Karl Seguin Guest

    Jim,
    Those are good questions, and we get that kind of stuff a lot around here.
    Check out my tutorial on databinding and I think you'll find the answers you
    are looking for:
    http://openmymind.net/databinding/index.html

    As a quick note before you go through it though, you should be naming these
    dropdownlist with the same id, that is "lstCategories". In other words, you
    probably DON'T want the 0,1,2,3,4. You can get a reference to an individual
    one by doing something like
    myDataGrid.Items[0].FindControl("lstCategories") which will return the
    dropdownlist in the 1st row.

    As for binding to them, hook into the grid's ItemDataBound event...the
    tutorial goes into great detail on this point, so you should find everything
    you need for that there.

    Hope this helps,
    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Jim Bancroft" <> wrote in message
    news:e%...
    >
    > Hi everyone,
    >
    > I'd like to put some DropDownLists in my DataGrid, populating each from a
    > database query. I'm having a little trouble naming them based on the
    > current record in the DataSet.
    >
    > For example, here's a snippet of what I have now in my DataGrid
    >
    > <ItemTemplate>
    > <asp:DropDownList runat="server" id="lstCategories"
    > DataValueField="CategoryID"
    > DataTextField="Name"/>
    > </ItemTemplate>
    >
    > I can create the DropDownLists ok, but I'm stuck choosing one id for the
    > name, and having the DataGrid supply the final name for each. I'd like to
    > name them myself, giving them ids like "lstCategories0," "lstCategories1,"
    > "lstCategories2," etc. Is it possible to do that somehow?
    >
    > And, if I can do it, how can I set their DataSource properties and

    selected
    > items in my code-behind file? The goal is to have each DropDown reading
    > from the same data source, but with their selected item dependent on their
    > id values.
    >
    > Thanks very much,
    >
    > -Jim
    >
    >
    Karl Seguin, Dec 18, 2004
    #2
    1. Advertising

  3. Create a view of the table, then you can specify the view (which contains
    the identical same information as the table) for the other data set.
    However, this skirts the issue without solving it.

    "Jim Bancroft" <> wrote in message
    news:e%...
    >
    > Hi everyone,
    >
    > I'd like to put some DropDownLists in my DataGrid, populating each from a
    > database query. I'm having a little trouble naming them based on the
    > current record in the DataSet.
    >
    > For example, here's a snippet of what I have now in my DataGrid
    >
    > <ItemTemplate>
    > <asp:DropDownList runat="server" id="lstCategories"
    > DataValueField="CategoryID"
    > DataTextField="Name"/>
    > </ItemTemplate>
    >
    > I can create the DropDownLists ok, but I'm stuck choosing one id for the
    > name, and having the DataGrid supply the final name for each. I'd like to
    > name them myself, giving them ids like "lstCategories0," "lstCategories1,"
    > "lstCategories2," etc. Is it possible to do that somehow?
    >
    > And, if I can do it, how can I set their DataSource properties and

    selected
    > items in my code-behind file? The goal is to have each DropDown reading
    > from the same data source, but with their selected item dependent on their
    > id values.
    >
    > Thanks very much,
    >
    > -Jim
    >
    >
    Chuck Insight, Dec 18, 2004
    #3
  4. Jim Bancroft

    Jim Bancroft Guest

    Thanks, Karl.

    -Jim

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > Jim,
    > Those are good questions, and we get that kind of stuff a lot around here.
    > Check out my tutorial on databinding and I think you'll find the answers
    > you
    > are looking for:
    > http://openmymind.net/databinding/index.html
    Jim Bancroft, Dec 20, 2004
    #4
    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. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    507
    tdavisjr
    Jun 16, 2006
  2. shivam
    Replies:
    0
    Views:
    328
    shivam
    Aug 20, 2007
  3. falk

    SelectedIndex to dynamically created DropDownLists

    falk, Sep 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    132
  4. ChuckSlayer
    Replies:
    1
    Views:
    135
    ChuckSlayer
    Feb 10, 2007
  5. why the lucky stiff
    Replies:
    5
    Views:
    134
    why the lucky stiff
    Sep 22, 2004
Loading...

Share This Page