我在gridview中有一个动态的下拉列表。我想要的是从第一行的下拉列表中选择第一个值后,使用第一行上的选定值绑定第二行中的下拉列表。
<asp:TemplateField HeaderText="Staff">
<ItemTemplate>
<asp:DropDownList ID="ddlInsentiveCategory" runat="server" OnSelectedIndexChanged="ddlInsentiveCategory_SelectedIndexChanged" AutoPostBack="true" DataSourceID="InsentiveDataSource"
DataTextField="Name" DataValueField="SalesStaffID">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator70" runat="server" ControlToValidate="ddlInsentiveCategory"
CssClass="rfv" ErrorMessage="*" ForeColor="Red" InitialValue="0" ValidationGroup="group2"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="InsentiveDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Connection %>"
SelectCommand="SELECT '--Select--' as [Name],'0' as [SalesStaffID] union all SELECT [Name],[SalesStaffID] FROM [SalesStaff] WHERE ([StaffCategoryID] = @StaffCategoryID)">
<SelectParameters>
<asp:ControlParameter ControlID="hdnInsentiveCategoryID" Name="StaffCategoryID" PropertyName="Value"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="hdnInsentiveCategoryID" runat="server" Value='<%# Bind("StaffCategoryID") %>' />
</ItemTemplate>
</asp:TemplateField>
帮助我实现这一目标。提前谢谢。
答案 0 :(得分:0)
我假设你的gridview也在使用SqlDataSource,否则在提到的方法中需要进行一些小改动。
您需要将Selecting
事件添加到SqlDataSource以获取下拉列表
如下面的标记。
<asp:SqlDataSource ID="InsentiveDataSource" runat="server"
OnSelecting="InsentiveDataSource_Selecting" ....
在后面的代码中添加以下C#代码。正在订阅下拉列表SqlDataSource的Selecting事件,因为它允许在获取数据之前对SqlDataSource进行更改。
在这里,您将获得其索引具有的下拉列表的选定值 刚被改变,然后让下一行下拉控制。
一旦获得下一行下拉控件,我们只需调用它
DataBind
方法将触发其数据的Selecting事件
源
在选择事件中,我们可以动态地将参数值设置为上一行的下拉选择值。
private int dropdpownValue;
protected void ddlInsentiveCategory_SelectedIndexChanged ( object sender, EventArgs e)
{
//get selected value of drop down and then find
//the row index of this drop down control
DropDownList ddl = sender as DropDownList;
int.TryParse( (sender as DropDownList).SelectedItem.Value, out ddlValue);
GridViewRow row = (GridViewRow) ddl.NamingContainer;
int ddlRowIndex = row.RowIndex;
//find the next row dropdownlist and if there is a next row,
//then get this control and databind it
GridView gridView = (GridView)ddl.NamingContainer.NamingContainer ;
if((ddlRowIndex + 1) <= (gridView.Rows.Count -1)) {
DropDownList nextRowDDL = ((System.Web.UI.WebControls.GridView)ddl.NamingContainer.NamingContainer ).Rows[ ddlRowIndex + 1].FindControl("ddlInsentiveCategory")
nextRowDDL.DataBind();
}
}
protected void InsentiveDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if(dropdpownValue> 0) {
e.Command.Parameters[0].Value= dropdpownValue;
dropdownValue = 0;
}
}
答案 1 :(得分:0)
我只能使用SelectedIndexChanged事件来解决这个问题。代码如下。希望这会有所帮助。
protected void ddlInsentiveCategory_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlMainCategory = (DropDownList)gvInsentivePayment.Rows[0].FindControl("ddlInsentiveCategory");
DropDownList ddlDependentCategory = (DropDownList)gvInsentivePayment.Rows[1].FindControl("ddlInsentiveCategory");
if (ddlDependentCategory.SelectedIndex == 0)
{
ddlMainCategoryValue = Convert.ToInt32(ddlMainCategory.SelectedValue);
List<SalesStaff> FetchDependentList = // Bind the list using selected value from the 1st dropdown in the first row.
ddlDependentCategory.DataSourceID = null; //Assigning null to remove the DatasourceID in client side.
ddlDependentCategory.DataSource = FetchDependentList;
ddlDependentCategory.DataTextField = "Name";
ddlDependentCategory.DataValueField = "ID";
ddlDependentCategory.DataBind();
}
}