I have two dropdownlists inside a repeater. And based on the value I pick in the first dropdownlist the second needs to get populated (both need to be populated from the DB via stored proc). Any help on this Please.
I am adding the code I have so far but it's not completely correct
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<tr>
<td style="width: 40%; text-align: left">
<asp:DropDownList ID="ddlOne" runat="server" Width="150px" OnSelectedIndexChanged="PopulateSecondDropDown_selectIndexchanged"
AutoPostBack="true">
</asp:DropDownList>
</td>
</td>
<td style="width: 40%; text-align: left">
<asp:DropDownList ID="ddlTwo" runat="server" Width="150px">
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Save" runat="server" Text="Save" OnClick="Save_Values_Click"/>
On Page Load doing this
rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
rpt.DataSource = ListForFirstDropDown.GetDropDownList; // this returns values only for the first dropdownlist
rpt.DataBind();
protected void rpt_ItemDataBound(object source, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
// not sure how to handle this here
}
}
public void PopulateSecondDropDown_selectIndexchanged(object sender, EventArgs e)
{
ddlTwo.Items.Clear();
int ID = Convert.ToInt32(ddlOne.Text);
LoadSecondDropDown(ID);
}
private void LoadSecondDropDown(int ID)
{
rpt.DataSource = ListForSecondDropDown.GetDropDownList(ID); // this returns values only for the second dropdownlist
rpt.DataBind();
}
With an SQLDataSource, you can use SelectParameters to define criteria used in the select statement. This allows you to use a number of sources for parameter value, including another control on the page.
Here's an example of an SQLDataSource populating a drop down list, whose selected value is used in another data source and list:
<asp:SqlDataSource
runat="server"
ID="firstDataSource"
ConnectionString="<%$ connectionStrings:MyConnectionString %>"
SelectCommand="SELECT DISTINCT item1 FROM table" />
<asp:DropDownList
runat="server"
ID="itemList"
DataSourceID="firstDataSource"
DataValueField="item1"
DataTextField="item1"
AutoPostBack="true" />
<asp:SqlDataSource
runat="server"
ID="secondDataSource"
ConnectionString="<%$ connectionStrings:MyConnectionString %>"
SelectCommand="SELECT item2 FROM table WHERE item1 = @item1">
<SelectParameters>
<asp:ControlParameter ControlID="itemList" PropertyName="SelectedValue" Name="item1" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList
runat="server"
DataSourceID="secondDataSource"
DataValueField="item2"
DataTextField="item2" />
This works within a repeater's ItemTemplate
. If the "firstDataSource" needs a key from the repeater item, you can use a hidden field control and another ControlParameter
.
<asp:Repeater runat="server" DataSourceID="topDataSource">
<ItemTemplate>
<asp:HiddenField runat="server" ID="key" Value='<%# Eval("id") %>' />
<!-- ... -->
</ItemTemplate>
</asp:Repeater>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.