LinkBut​​ton 在嵌套的 gridview 中不起作用

LinkButton is not working inside nested gridview

我在 GridView1 中有一个名为 GridView1GridView2 的网格视图。

我的问题是我想在 GridView2 中放置一个 Linkbutton,但是当我点击 link 按钮时,出现了这个错误。

Object reference not set to an instance of an object.

好像找不到我的GridView2.

这是我的 aspx 标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="700px" Height="16px" 
     style="text-align:left; margin-top: 0px; " DataKeyNames ="progressID" OnRowDataBound="GridView1_OnRowDataBound" 
     CssClass="Grid" CellPadding="1" ForeColor="#333333"  HeaderStyle-HorizontalAlign="left">
     <AlternatingRowStyle BackColor="White" />
     <Columns>
         <asp:TemplateField HeaderText="Progress">
             <ItemTemplate>
                 <%# Eval("message") %> 
                 <br />
                 <asp:label ID="labelRemark" runat="server" style="font-style:italic; Font-Size:11.5px;" text='<%# Eval("remark") %>'></asp:label>
                 <br />
                 <div id="div<%# Eval("progressID") %>">
                     <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
                          DataKeyNames="progressID"  GridLines="None"  style="text-align:center; font-size:small;" CellPadding="1" ForeColor="#333333" Width="376px">
                         <AlternatingRowStyle BackColor="White" />
                         <Columns>
                             <asp:BoundField DataField="tackingNo" HeaderText="Tacking No" ItemStyle-Width="100px" ItemStyle-Font-Size="Small" >
                                 <ItemStyle Font-Size="Small" Width="100px" />
                             </asp:BoundField>
                             <asp:BoundField DataField="courierDate" HeaderText="Courier Date" ItemStyle-Width="100px">
                                 <ItemStyle Width="100px" />
                             </asp:BoundField>
                             <asp:TemplateField HeaderText="Website">
                                 <ItemTemplate>
                                     <asp:Label ID="Label1" runat="server" Text='<%# Bind("companyurl") %>' Visible="False"></asp:Label>
                                     <asp:LinkButton ID="LinkButton1" Text= '<%# Bind("providernm") %>' runat="server"
                                          onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
                                 </ItemTemplate>
                             </asp:TemplateField>        
                         </Columns>
                         <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
                         <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                         <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                         <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                         <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                         <SortedAscendingCellStyle BackColor="#FDF5AC" />
                         <SortedAscendingHeaderStyle BackColor="#4D0000" />
                         <SortedDescendingCellStyle BackColor="#FCF6C0" />
                         <SortedDescendingHeaderStyle BackColor="#820000" />
                     </asp:GridView>
                 </div>
             </ItemTemplate>                         
         </asp:TemplateField>
         <asp:BoundField DataField="dateupdate" HeaderText="Date Update" />                
     </Columns>
     <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
     <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
     <SortedAscendingCellStyle BackColor="#FDF5AC" />
     <SortedAscendingHeaderStyle BackColor="#4D0000" />
     <SortedDescendingCellStyle BackColor="#FCF6C0" />
     <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

代码隐藏

public partial class Form : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {   
        string TxID = TextBox1.Text;
        Label1.Text = "Tracking Details For Parcel No :" + TxID + " ";

        CheckProductTrack checkProductTrack = new CheckProductTrack();
        int trackingID = checkProductTrack.getTrackRecord(TxID);

        if(trackingID!=0)
        {
            GridView1.DataSource = ProgressTrackClass.ProgressTrack.getProgress(trackingID);
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();

            string CS = ConfigurationManager.ConnectionStrings["TMXEntities"].ConnectionString;
            SqlConnection con = new SqlConnection(CS);

            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = new SqlCommand("SELECT  tradeit.TSTxDetails.TxID, tradeit.TSTxDetails.ProductID, tradeit.TSTxDetails.ProductName, tradeit.TSTxDetails.Qty, tradeit.TSTxMaster.TxStatus "
                                             + " FROM tradeit.TSTxDetails INNER JOIN tradeit.TSTxMaster ON tradeit.TSTxMaster.TxID=tradeit.TSTxDetails.TxID WHERE tradeit.TSTxDetails.TxID='" + TxID + "' ", con);

            con.Open();
            da.Fill(ds);
            GridView3.DataSource = ds;
            GridView3.DataBind();

            int rowCount = GridView3.Rows.Count;
            if (rowCount == 0)
            {
                GridView3.Visible = false;
                Label2.Text = "TxId Entered is not Exist";
            }
            else
            {
                GridView3.Visible = true;
                Label2.Text = "";
            }
            con.Close();
        }
    }

    protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string progressID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
            GridView GridView2 = (GridView)e.Row.FindControl("GridView2");

            int proID = Convert.ToInt32(progressID);
            GridView2.DataSource = ProgressLogisClass.ProgressLogis.getCourierRecord(proID);
            GridView2.DataBind();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        GridViewRow row = ((LinkButton)sender).Parent.Parent as GridViewRow;
        int rind = row.RowIndex;

        GridView gridview2 = (GridView)GridView1.FindControl("GridView2");
        Label ctrl = (Label)gridview2.Rows[rind].FindControl("Label1");

        Response.Redirect(ctrl.Text);
    }
}

我真的不知道该怎么办。据我通读,我应该使用 row 命令等等,但我不确定如何在 GridView2 中找到标签。

谢谢

我建议您对子网格使用 Row_Command 事件

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "go")
     {     
        GridViewRow Gv2Row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
        GridView Childgrid = (GridView)(Gv2Row.Parent.Parent);

    }
}