I have a gridviews named GridView1
and GridView2
inside GridView1
.
My problem is that I want to place a Linkbutton
in GridView2
, but in when I click the link button, I am getting this error.
Object reference not set to an instance of an object.
It seems like it could not find my GridView2
.
Here my aspx markup:
<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>
Code Behind
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);
}
}
I really don't know what should I do. As far as I have read through, I should make use of row command and so on, but I am not sure how to find the label inside GridView2 .
Thanks
I would suggest you to use Row_Command event for child grid