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

标签 c# asp.net gridview linkbutton

我在 GridView1 中有一个名为 GridView1GridView2 的 GridView 。

我的问题是我想在 GridView2 中放置一个 Linkbutton,但是当我点击链接按钮时,我收到了这个错误。

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);

    }
}

关于c# - LinkBut​​ton 在嵌套的 gridview 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30906059/

相关文章:

c# - ASP.NET 用户验证

c# - 网址路由规则冲突

c# - Gridview on Linkbutton click,将完整的Grid Row信息传递到下一页?最好的方法是什么?

gridview - 如何在yii2中的GridView中设置页面大小

c# - 检查 gridView 是否为空

c# - 在异常构造函数中记录错误是一种好习惯吗?

c# - XmlReader 跳过元素

c# - 将按下的任何键绑定(bind)到 VM WPF 中的命令

c# - 是否可以将 FolderBrowserDialog.RootFolder 设置为字符串中的任意路径?

c# - 如何在应用程序关闭/启动时保存/恢复 SignalR Hub、连接、状态