【问题标题】:Retrieve value from DropDownList in nested GridView on RowCommand从 RowCommand 的嵌套 GridView 中的 DropDownList 中检索值
【发布时间】:2018-06-17 14:28:38
【问题描述】:

我有一个嵌套的 GridView(GvMP_Summary_Items)。每行包含一个 DropDownList。 DropDownList 以嵌套 GridView 的 RowDataBound 事件为界。

每行还包含 1 个按钮。在 RowCommand 事件上按下此按钮后,我想找到 DropDownList 的当前选定值,以便在代码中进一步使用它。

我的代码只会获取每一行的 DropDownList 的默认值,当前每行设置为0

下面是 RowCommand 事件:

Protected Sub GvMP_Summary_Items_RowCommand(sender As Object, e As GridViewCommandEventArgs)

  Dim lb As ImageButton = CType(e.CommandSource, ImageButton)
  Dim gvRow As GridViewRow = lb.BindingContainer //Getting current row to get index       

  Dim GvMP_Summary_Items As GridView = CType(gvRow.FindControl("GvMP_Summary_Items"), GridView)

  Dim intMPItem_Qty As Integer = CType(gvRow.FindControl("cboMPItem_Qty"), DropDownList).SelectedValue
  Dim strMPItem_Qty As String = CType(gvRow.FindControl("txtMPItem_Qty"), TextBox).Text

End Sub

我什至在 GridView 行中包含了一个 TextBox,其默认值为空 ""。尽管在行上,如果输入了某些内容,on RowCommand 事件会带回前面带有逗号(,)的值。

这证明我选择了正确的行,并且可以从 TextBox 但不能从 DropDownList 中检索值。

我有什么遗漏吗?为什么我可以返回在 TextBox 中输入的值,但不能返回 DropDownList 的选定值?还有为什么 TextBox 值前面的逗号(,)?

注意:在上述情况下,代码是使用 VB 编写的,所以在 VB 中通过 C# 回答,但我可以接受两者。

【问题讨论】:

  • 如何填充下拉列表?您是否有可能在每次回发时将其清除并重新填充,并将其重置为默认值?

标签: c# asp.net vb.net gridview nested-gridview


【解决方案1】:

重要的事情:

  1. Page_Load方法中绑定父GridView
  2. 在父 GridViewRowDataBound 事件中绑定子 GridView
  3. 在子 GridViewRowDataBound 事件中绑定 DropDownList
  4. CommandName 添加到子 GridView 内的 Button
  5. 最后,在子GridView的RowCommand事件中
    • 获取子 GridView
    • 然后从子 GridViewrow 中找到子 GridView 内的所有 控件

我不太了解 VB.NET,所以我添加了一个带有 RowCommand 事件的 Nested GridView 示例 (c#)(希望 OP 可以在 VB.NET 中使用它):

HTML 代码 (.Aspx):

<form id="form1" runat="server">

<asp:GridView ID="GridView_Outer" OnRowDataBound="GridView_Outer_RowDataBound" AutoGenerateColumns="false" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="Outer Column1">
            <ItemTemplate>
                <asp:Label ID="Label_Outer" runat="server" Text='<%# Eval("Label_Outer") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Outer Column2">
            <ItemTemplate>
                <asp:GridView ID="GridView_Inner" OnRowDataBound="GridView_Inner_RowDataBound" OnRowCommand="GridView_Inner_RowCommand" AutoGenerateColumns="false" runat="server">
                    <Columns>
                        <asp:TemplateField HeaderText="Inner Column1">
                            <ItemTemplate>
                                <asp:Label ID="Label_Inner" runat="server" Text='<%# Eval("Label_Inner") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Inner Column2">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox_Inner" Text='<%# Eval("TextBox_Inner") %>' runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Inner Column3">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList_Inner" runat="server"></asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Inner Column4">
                            <ItemTemplate>
                                <asp:Button ID="Button_Inner" runat="server" CommandName="BtnInnerCmd" Text="Inner Button" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:Label ID="Label_Result" runat="server"></asp:Label>

</form>

代码隐藏 (.Aspx.cs):

DataTable TempDT = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
    CreateDataTable();

    if (!IsPostBack)
    {
        GridView_Outer.DataSource = TempDT;
        GridView_Outer.DataBind();
    }
}

// create DataTable
public void CreateDataTable()
{
    TempDT = new DataTable();
    TempDT.Columns.Add("Label_Outer");
    TempDT.Columns.Add("Label_Inner");
    TempDT.Columns.Add("TextBox_Inner");

    TempDT.Rows.Add("OuterLabel", "InnerLabel", "");
    TempDT.Rows.Add("OuterLabel", "InnerLabel", "");

    // store DataTable into ViewState to prevent data loss on PostBack
    ViewState["DT"] = TempDT;
}

// Calls Outer GridView on Data Binding
protected void GridView_Outer_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // check if gridview row is not in edit mode
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get Outer GrridView 's controls
        Label Label_Outer = (Label)e.Row.FindControl("Label_Outer");
        GridView GridView_Inner = (GridView)e.Row.FindControl("GridView_Inner");

        // get DataTable from ViewState and set to Inner GridView
        GridView_Inner.DataSource = (DataTable)ViewState["DT"];
        GridView_Inner.DataBind();
    }
}

// Calls Inner GridView on Data Binding
protected void GridView_Inner_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // check if gridview row is not in edit mode
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get Outer GrridView 's controls
        DropDownList DropDownList_Inner = (DropDownList)e.Row.FindControl("DropDownList_Inner");

        // Create a DataTable to Bind data for DropDownlist
        DataTable TempDDLDT = new DataTable();
        TempDDLDT.Columns.Add("ItemText");
        TempDDLDT.Columns.Add("ItemValue");

        TempDDLDT.Rows.Add("ItemText1", "ItemValue1");
        TempDDLDT.Rows.Add("ItemText2", "ItemValue2");

        // bind DataTable to the DropDownList
        DropDownList_Inner.DataSource = TempDDLDT;
        DropDownList_Inner.DataTextField = "ItemText";
        DropDownList_Inner.DataValueField = "ItemValue";
        DropDownList_Inner.DataBind();
    }
}

// Calls when Inner GridView 's button clicked
protected void GridView_Inner_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // get Inner GridView 's clicked row
    GridViewRow InnerGridViewRow = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

    // get Inner GridView 's controls from clicked row
    TextBox TextBox_Inner = (TextBox)InnerGridViewRow.FindControl("TextBox_Inner");
    DropDownList DropDownList_Inner = (DropDownList)InnerGridViewRow.FindControl("DropDownList_Inner");

    // check if correct button is clicked
    if (e.CommandName == "BtnInnerCmd")
    {
        string DropDownListValue = DropDownList_Inner.SelectedValue;
        string TextBoxValue = TextBox_Inner.Text;

        Label_Result.Text = "DropDownList 's Selected Value is " + DropDownListValue +
                            "<br />TextBox 's Entered Value is " + TextBoxValue;
    }
}

演示图片:

注意: 以上 DropDownList 选择的是 Selected Value 而不是 Text。

【讨论】:

    【解决方案2】:

    这可以通过将RowCommandCommandSource 转换回按钮然后从中获取正确的行来非常容易地完成。获得该行后,您可以使用FindControl 来定位DropDownList。这适用于每个级别的嵌套项,也适用于顶部控件。

    VB

    Protected Sub GMP_Summary_Items_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    
        'cast the commandsource back to a button
        Dim btn As Button = CType(e.CommandSource,Button)
    
        'get the current gridviewrow from the button namingcontainer
        Dim row As GridViewRow = CType(btn.NamingContainer,GridViewRow)
    
        'use findcontrol to locate the dropdownlist in that row
        Dim ddl As DropDownList = CType(row.FindControl("cboMPItem_Qty"),DropDownList)
    
        'show the selected value of the dropdownlist
        Label1.Text = ddl.SelectedValue
    
    End Sub
    

    代码是用 code translator 从 C# 翻译过来的,所以它可能不是 100% 准确的。

    C#

    protected void GMP_Summary_Items_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //cast the commandsource back to a button
        Button btn = e.CommandSource as Button;
    
        //get the current gridviewrow from the button namingcontainer
        GridViewRow row = btn.NamingContainer as GridViewRow;
    
        //use findcontrol to locate the dropdownlist in that row
        DropDownList ddl = row.FindControl("cboMPItem_Qty") as DropDownList;
    
        //show the selected value of the dropdownlist
        Label1.Text = ddl.SelectedValue;
    }
    

    您确实需要将数据绑定到 GridView 和 DropDownLists 中 IsPostBack勾选,否则每次PostBack都会将数据反弹到DDL,丢失选中的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2020-10-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多