从RowCommand上的嵌套GridView中的DropDownList中检索值

Pri*_*PCS 15 c# vb.net asp.net gridview nested-gridview

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

每行还包含1个Button.在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
Run Code Online (Sandbox Code Playgroud)

我甚至在GridView行中包含一个TextBox,默认值为空"".虽然在行上输入了某些内容,但是RowCommand事件会在其前面带回逗号(,).

这证明我正在拾取正确的行,并且可以从TextBox但不是DropDownList中检索值.

有什么我想念的吗?为什么我可以返回在TextBox中输入的值而不是DropDownList的选定值?另外为什么逗号(,)在TextBox值前面?

注意:在上面的情况下,代码是用VB编写的,所以在VB中用C#解答,但我可以同时接受这两个.

小智 3

重要的事情:

  1. 在Page_Load方法中绑定父GridView

  2. 在父GridViewRowDataBound事件中绑定子 GridView

  3. 在子GridViewRowDataBound事件中绑定 DropDownList

  4. CommandName添加到子 GridView 内的按钮

  5. 最后,在子 GridView 的 RowCommand 事件中

    • 获取子GridView
    • 然后从子GridView中查找子 GridView 内的所有控件

我不太了解 VB.NET,所以我添加了一个带有 RowCommand 事件的嵌套 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>
Run Code Online (Sandbox Code Playgroud)

隐藏代码 (.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

演示图片:

在此输入图像描述 注意:上面的 DropDownList 选择选定值而不是文本。