Hi krajeshkumar
I write some codes and run in my side. Here I suggest you to use Page.LoadTemplate("......") instead of using ITemplate. When postback the data in ViewState can't find the controls they are belonged to because this time these controls haven't been created at Page_Load. It will take much work to keep the state and there will be some problems in finding the controls.
You can have a look at my code and see how I find the controls.
.cs file
protected void Page_Init(object sender, EventArgs e) {
TemplateField tf = new TemplateField();
tf.ItemTemplate = Page.LoadTemplate("../WebUserControl1.ascx");
this.GridView1.Columns.Add(tf);
}
protected void Page_Load(object sender, EventArgs e)
{
//The file shown below is the way to find your control
for(int i=0;i<GridView1.Rows.Count;i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
TableCell tc = GridView1.Rows[i].Cells[0];
WebUserControl1 wuc = tc.Controls[0] as WebUserControl1;
if (wuc != null)
{
Response.Write(((TextBox)wuc.FindControl("TextBox1")).Text +"<br/>");
}
}
}
}
.ascx file
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="DA_Simu.WebUserControl1" %>
ABC:<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Oname") %>'></asp:TextBox>
.aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="nestedgridview.aspx.cs" Inherits="DA_Simu.gridview.nestedgridview" %>
<%@ Register src="../WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:simudbConnectionString %>"
SelectCommand="SELECT [Oid], [Onum], [Oname], [Ocategory] FROM [objects]"
UpdateCommand="UPDATE objects SET Ocategory = @Ocategory WHERE (Oid = @Oid)">
<UpdateParameters>
<asp:Parameter Name="Ocategory" />
<asp:Parameter Name="Oid" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Hope my code can be useful to solve your problem. Please feel free to contact with me.
Karl