LinkBut​​ton无法在嵌套gridview

时间:2015-06-18 03:38:10

标签: c# asp.net gridview linkbutton

我在GridView1内有一个名为GridView2GridView1的网格视图。

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

  

对象引用未设置为对象的实例。

好像找不到我的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中找到标签。

由于

1 个答案:

答案 0 :(得分:1)

我建议你为子网格使用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);

    }
}