如何在嵌套gridview

时间:2017-09-20 10:52:13

标签: c# asp.net .net gridview model-view-controller

如何在嵌套gridview

中单击按钮时获取文本框值
<asp:GridView ID="GridView2" runat="server">

                   <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>

                                    <asp:ImageButton ID="ImageButton1" runat="server"  OnClick="Show_Hide_ChildGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" Height="20" Width="20" />

                                 <asp:Panel ID="pnlOrders" runat="server" Style="display: none;">

                                    <asp:GridView ID="gvOrders" runat="server" OnRowCommand="gvOrders_RowCommand">
                                        <Columns>


                                            <asp:TemplateField HeaderText="volNo">
                                                <ItemTemplate>
                                                    <asp:TextBox Width="90px" ID="txtVolume" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Volume") %>'></asp:TextBox>
                                                </ItemTemplate>
                                                <HeaderStyle Width="91px" />
                                            </asp:TemplateField>

                                           <asp:ButtonField ButtonType="Button" CommandName="Save" Text="Save" HeaderText="Save">
                                                 <HeaderStyle Width="50px" />
                                            </asp:ButtonField>

                                        </Columns>


                                    </asp:GridView>

                                </asp:Panel>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="JournalName" HeaderText="JournalName" ></asp:BoundField>

                    </Columns>

                </asp:GridView>

我无法从子gridview获取文本框值。 它显示错误,即此(gridview id)在当前上下文中不存在。 在父网格视图中工作正常的地方。当我尝试使用父网格视图 然后它的工作正常,意味着代码是正确的。我的方式。可能就是这样 所以请检查。设计部分和.cs部分都已发布。而且我也提到了我收到错误的地方

 protected void gvOrders_RowCommand(object sender, GridViewCommandEventArgs e)
    {



        if (e.CommandName == "Save")
        {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                **GridViewRow row = gvOrders.Rows[rowIndex];** // Here i am getting error that gvOrders does not exit in the current context

                //GridView GVCollapse = (GridView)sender;  //find nested grid view
                //GridViewRow row = (GridViewRow)GVCollapse.Parent.Parent;

                //Fetch value of Name.
                string Journal = (row.FindControl("txtJournalName") as TextBox).Text;
                string Volume = (row.FindControl("txtVolume") as TextBox).Text;
                string Issues = (row.FindControl("txtIssues") as TextBox).Text;
                string DispatchDate = (row1.FindControl("txtDispatchDate") as TextBox).Text;
                string Remarks = (row.FindControl("txtRemarks") as TextBox).Text;



                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + Journal + "\\nCountry: " + Volume + "\\nDate: " + DispatchDate + "');", true);



        }
    }

1 个答案:

答案 0 :(得分:0)

将ButtonField替换为Button:

<asp:TemplateField HeaderText="Save">
    <ItemTemplate>
        <asp:Button ID="btnSave" CommandName="Save" runat="server" Text="Save" />
    </ItemTemplate>
</asp:TemplateField>

在RowCommand事件中尝试:

if (e.CommandName == "Save")
{
    GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
    GridView gv = (GridView)row.FindControl("gvOrders");

    string Journal = (row.FindControl("txtJournalName") as TextBox).Text;
    //... some other code
}