从RowCommand

时间:2018-01-08 15:22:39

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

我有一个嵌套的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,默认值为空""。虽然在行上输入了某些内容,但是RowCommand事件会在其前面带回逗号(,)。

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

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

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

2 个答案:

答案 0 :(得分:3)

通过将CommandSource的{​​{1}}强制转换回按钮然后从中获取正确的行,可以非常轻松地完成此操作。获得该行后,您可以使用RowCommand找到FindControl。这适用于嵌套项的每个级别,也适用于顶级控件。

<强> VB

DropDownList

代码是使用code translator从C#翻译的,因此可能不是100%准确。

<强> C#

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
  

您需要将数据绑定到GridView和DropDownLists中   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; } 检查,否则数据将在每个PostBack上反弹到DDL并且所选值丢失

答案 1 :(得分:2)

  

重要的事情:

     
      
  1. Page_Load 方法
  2. 中绑定父GridView   
  3. 在父 GridView RowDataBound 事件中绑定子GridView
  4.   
  5. 在子 GridView RowDataBound 事件中绑定DropDownList
  6.   
  7. CommandName 添加到子GridView中的Button
  8.   
  9. 最后,在子GridView的RowCommand事件中      
        
    • 获取子 GridView
    •   
    • 然后从子 GridView
    • 中查找子GridView中的所有控件   
  10.   

我不太了解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>

Code-Behind(.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;
    }
}

演示图片:

enter image description here 注意: DropDownList上方选择选定值而不是文本。