locked
Repeater with Dropdownlist and checkboxlist RRS feed

  • Question

  • User-1967952962 posted

    Hello All

                Inside my repeater i have a dropdownlist and checkboxlist, I have to populate the checkboxlist data when dropdownlist _SelectedIndexChanged  . I'm able to do this outside the repeater. But I cannot achieve this in repeater. Is there any other references for this problem or any help to solve this problem .

     

     

     

     

     

     

     

    Wednesday, November 7, 2012 5:51 PM

Answers

  • User619859613 posted

    See my sample:

     <asp:Repeater runat="server" ID="repItems" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    <div style="width: 20%; float: left;">
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                        </asp:CheckBoxList>
                        <h2>
                            <%# Eval("time") %></h2>
                        <asp:DropDownList ID="ddlnewrank" runat="server" OnSelectedIndexChanged="ddlnewrank_SelectedIndexChanged"
                            AutoPostBack="true">
                            <asp:ListItem Text="Value1"></asp:ListItem>
                            <asp:ListItem Text="Value2"></asp:ListItem>
                            <asp:ListItem Text="Value3"></asp:ListItem>
                            <asp:ListItem Text="Vlaue4"></asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </ItemTemplate>
            </asp:Repeater>
            <br />
        </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:TestDBConnectionString1.ProviderName %>"
            SelectCommand="SELECT [id], [time], [min] FROM [name]"></asp:SqlDataSource>
      protected void ddlnewrank_SelectedIndexChanged(System.Object sender, System.EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                RepeaterItem item = (RepeaterItem)ddl.NamingContainer;         
                if (item != null)
                {
                    CheckBoxList list = (CheckBoxList)item.FindControl("CheckBoxList1");
                    if (list != null)
                    {
                        //here bind the CheckboxList to data source that you want to, this just is a sample 
                        list.DataSourceID = "SqlDataSource1";
                        list.DataTextField = "min";
                        list.DataValueField = "id";
                        list.DataBind();
                    }
                }
            }

    if you want to dynamically populate the DropDownList, you can add Repeater ItemDataBound event:

     protected void repItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DropDownList ddl=(DropDownList)e.Item.FindControl("ddlnewrank");
                    if (ddl != null)
                    { 
                        bind the DropDownList here
                    }
                }          
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2012 5:10 AM

All replies

  • User619859613 posted

    See my sample:

     <asp:Repeater runat="server" ID="repItems" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    <div style="width: 20%; float: left;">
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                        </asp:CheckBoxList>
                        <h2>
                            <%# Eval("time") %></h2>
                        <asp:DropDownList ID="ddlnewrank" runat="server" OnSelectedIndexChanged="ddlnewrank_SelectedIndexChanged"
                            AutoPostBack="true">
                            <asp:ListItem Text="Value1"></asp:ListItem>
                            <asp:ListItem Text="Value2"></asp:ListItem>
                            <asp:ListItem Text="Value3"></asp:ListItem>
                            <asp:ListItem Text="Vlaue4"></asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </ItemTemplate>
            </asp:Repeater>
            <br />
        </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:TestDBConnectionString1.ProviderName %>"
            SelectCommand="SELECT [id], [time], [min] FROM [name]"></asp:SqlDataSource>
      protected void ddlnewrank_SelectedIndexChanged(System.Object sender, System.EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                RepeaterItem item = (RepeaterItem)ddl.NamingContainer;         
                if (item != null)
                {
                    CheckBoxList list = (CheckBoxList)item.FindControl("CheckBoxList1");
                    if (list != null)
                    {
                        //here bind the CheckboxList to data source that you want to, this just is a sample 
                        list.DataSourceID = "SqlDataSource1";
                        list.DataTextField = "min";
                        list.DataValueField = "id";
                        list.DataBind();
                    }
                }
            }

    if you want to dynamically populate the DropDownList, you can add Repeater ItemDataBound event:

     protected void repItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DropDownList ddl=(DropDownList)e.Item.FindControl("ddlnewrank");
                    if (ddl != null)
                    { 
                        bind the DropDownList here
                    }
                }          
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2012 5:10 AM
  • User-1967952962 posted

    This is what..i was looking for "ddlnewrank_SelectedIndexChanged" fixed my problem. Thank you

    Friday, November 9, 2012 11:11 AM