How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

Last post 08-05-2003 12:50 PM by HumanCompiler. 7 replies.

Sort Posts:

  • How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    09-10-2002, 1:33 PM
    • Member
      10 point Member
    • pike
    • Member since 07-27-2002, 10:42 PM
    • Posts 2
    Hi.

    i am trying to dynamically generate DropDownList controls with AutoPostBack=true inside of Repeater control.

    I want each DropDownList control call OnSelectedIndexChanged function and pass "id" parameter bound by Repeater.

    OleDbCommand myCMD = new OleDbCommand("select id from some_table");
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(myCMD);
    oleDbConnection1.Open();
    dataAdapter1.Fill(ds,"id");
    Parent.DataSource = ds.Tables["id"];
    Page.DataBind();
    oleDbConnection1.Close();

    <asp:Repeater id="Parent" runat="server" OnItemDataBound="Parent_ItemDataBound" OnItemCreated="Parent_ItemCreated">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "id") %>
    <asp:DropDownList ID="MonthsID" AutoPostBack="true" runat="server" BackColor="#eeffee" ForeColor="#CC0000" Font-Bold="True">
    <asp:ListItem Value="0">0</asp:ListItem>
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>

    </asp:DropDownList>
    </ItemTemplate>
    </asp:Repeater>

    Thank you very much for your help.
  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    09-10-2002, 2:51 PM
    • Member
      328 point Member
    • HumanCompiler
    • Member since 06-19-2002, 5:05 AM
    • Kirkland, WA USA
    • Posts 64
    put this in your DropDownList

    OnSelectedIndexChanged="MonthsID_SelectedIndexChanged"

    then in your code, put the declaration for that sub and put whatever you want to happen in there...make sure it's declared as Public


    Public Sub MonthsID_SelectedIndexChanged(...)
    'code here
    End Sub
    Erik Porter
    Channel 9 Dev Team
  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    09-10-2002, 3:37 PM
    • Member
      10 point Member
    • pike
    • Member since 07-27-2002, 10:42 PM
    • Posts 2
    You didn't understand my question.

    I need to put not just one control in Repeater, but MANY.

    I want to be able to call event handler associated with that specific child control without knowing its name.

    I heard of event bubbling, but don't know how to use it for repeater.

    If you can please help me figuring this out.


    Thank you very much.
  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    09-10-2002, 3:58 PM
    • Member
      328 point Member
    • HumanCompiler
    • Member since 06-19-2002, 5:05 AM
    • Kirkland, WA USA
    • Posts 64
    yes, i did understand your question...you didn't understand my answer ;)

    what i posted will make it so that every child control (DropDownList) created by the Repeater, will call the same SelectedIndexChanged Sub...just give it a go and you'll see
    Erik Porter
    Channel 9 Dev Team
  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    08-04-2003, 4:39 PM
    • Member
      105 point Member
    • ericday
    • Member since 01-22-2003, 8:58 PM
    • Posts 22
    Erik,

    I am trying to accomplish roughly the same thing as pike. I have the event firing as per your solution of having all dropdownlists linked to the same method. This means I know what the selected value of the changed dropdownlist is.

    In my repeater itemtemplate I also have a textbox. What I want to do is populate the textbox, on the same row, with the selected value of the dropdownlist. In the method that gets fired on the selectedindexchanged event how do I reference the textbox that is on the same row as the dropdownlist?

    Here is what my itemtemplate looks like if it helps any:

    <itemtemplate>
    <tr>
    <td>
    <asp:dropdownlist runat="server" autopostback="True" id="dropdownid" onselectedindexchanged="dropdownmethod">
    <asp:listitem value="0">0</asp:listitem>
    <asp:listitem value="1">1</asp:listitem>
    <asp:listitem value="2">2</asp:listitem>
    </asp:dropdownlist>
    </td>
    </tr>
    <tr>
    <td>
    <asp:textbox runat="server" textmode="MultiLine" rows="4" width="100%"></asp:textbox>
    </td>
    </tr>
    </itemtemplate>

    When a user selects an item in the dropdown it should populate the textbox with the selected value, but only the textbox on the same row. Other textboxes should not be changed. Any help is appreciated.

    Thanks for your time,

    Eric

  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    08-04-2003, 5:12 PM
    • Member
      328 point Member
    • HumanCompiler
    • Member since 06-19-2002, 5:05 AM
    • Kirkland, WA USA
    • Posts 64
    Hi Eric, I'm honestly not sure off the top of my head and I'm a bit busy to research it.

    You might want to check out the Parent Property of the current DropDownList. I don't know this for sure, but I'm afraid the Parent will be the Repeater, not the item it sits in itself, although I may be wrong. If it is the Repeater itself, I'd imagine you'll have to go to great lengths to get what you want, such as tagging on an extra attribute to every generated DropDownList that is its Index in the Items Collection of the Repeater so you can go back in based off of that Index to find the TextBox you're looking for.

    Sorry I'm not much of a help, these are all just guesses off the top of my head.

    -Erik
    Erik Porter
    Channel 9 Dev Team
  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    08-05-2003, 9:45 AM
    • Member
      105 point Member
    • ericday
    • Member since 01-22-2003, 8:58 PM
    • Posts 22
    Thanks Erik!

    The parent property was exactly what I needed. The parent of the dropdownlist is an object called RepeaterItem. RepeaterItem has a controls collection. Looping through that collection allows me to find my textbox.

    I have included my code for others to see. This is the method that is linked to the onselectedindexchanged event of the dropdownlist as discussed in previous posts.

    Public Sub Test(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim objDL As DropDownList = CType(sender, DropDownList)
    Dim objRepeaterItem As RepeaterItem = objDL.Parent
    Dim ctl As Control
    Dim txt As TextBox

    For Each ctl In objRepeaterItem.Controls
    If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then
    txt = ctl
    txt.Text = objDL.SelectedValue
    End If
    Next
    End Sub

    Thanks again for your help Erik.
  • Re: How to dynamically generate DropDownList controls with AutoPostBack in Repeater control?

    08-05-2003, 12:50 PM
    • Member
      328 point Member
    • HumanCompiler
    • Member since 06-19-2002, 5:05 AM
    • Kirkland, WA USA
    • Posts 64
    Awesome, glad that worked out for you! :)
    Erik Porter
    Channel 9 Dev Team
Page 1 of 1 (8 items)