【问题标题】:Dropdown selected value to bind the DropDown next row inside gridview下拉选定的值以在 gridview 中绑定 DropDown 下一行
【发布时间】:2017-12-04 17:33:30
【问题描述】:

我在一个动态的网格视图中有一个下拉菜单。我想要的是从第一行的下拉列表中选择第一个值后,使用第一行中的选定值绑定第二行中的下拉列表。

                                        <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>

帮助我实现这一目标。提前致谢。

【问题讨论】:

    标签: c# asp.net gridview dropdown


    【解决方案1】:

    我假设您的 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;
           }
        }
        

    【讨论】:

    • 非常感谢您的解决方案。正如我在答案中发布的那样,我能够使用更少的步骤来解决这个问题
    【解决方案2】:

    我能够通过使用 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();
            }
    
        }
    

    【讨论】:

    • 您的代码中有潜在的故障点。当第 1 行不存在时会抛出异常。
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    相关资源
    最近更新 更多