I have a requirement to use gridview to show and update the information. Here the columns are not fixed, it needs to be dynamically generated. So for this I create a class extending ITemplate and created a ItemTemplate. There is no problem is load the grid,
only the big problem is that I am not able to access the controls that are inside ItemTemplate on postback, Even after Init the girdview on postback it doesn't help me. So can you please guide me in getting a solution for the same.
Sorry this doen't help me. This is what I want to do, on postback i want to fetch the dynamic ItemTemplate Control values using FindControl method in a Save method
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.
krajeshkumar
0 Points
2 Posts
Access Dynamic GridView ItemTemplate after postback
Jan 30, 2008 06:28 PM|LINK
Hi All,
I have a requirement to use gridview to show and update the information. Here the columns are not fixed, it needs to be dynamically generated. So for this I create a class extending ITemplate and created a ItemTemplate. There is no problem is load the grid, only the big problem is that I am not able to access the controls that are inside ItemTemplate on postback, Even after Init the girdview on postback it doesn't help me. So can you please guide me in getting a solution for the same.
Thnaks
Joseph Ghassan
Participant
905 Points
133 Posts
Re: Access Dynamic GridView ItemTemplate after postback
Jan 30, 2008 06:47 PM|LINK
Hi, try to access the controls at Gridview DataBound event Here :
protected void GridView1_DataBound(object sender, EventArgs e) {}
Hope this helps,
Joseph
MCP | Blog
MyPostAnsweredYourQuestion?ClickAnswer:DoNothing ;
Joseph Ghassan
Participant
905 Points
133 Posts
Re: Access Dynamic GridView ItemTemplate after postback
Jan 30, 2008 06:49 PM|LINK
Hi, try to access the controls at Gridview DataBound event Here :
protected void GridView1_DataBound(object sender, EventArgs e) {}
Hope this helps,
Joseph
MCP | Blog
MyPostAnsweredYourQuestion?ClickAnswer:DoNothing ;
krajeshkumar
0 Points
2 Posts
Re: Access Dynamic GridView ItemTemplate after postback
Jan 30, 2008 07:47 PM|LINK
Sorry this doen't help me. This is what I want to do, on postback i want to fetch the dynamic ItemTemplate Control values using FindControl method in a Save method
Thanks
Karl Cheng -...
Contributor
2564 Points
276 Posts
Re: Access Dynamic GridView ItemTemplate after postback
Feb 01, 2008 08:01 AM|LINK
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
Click "Mark as Answer" on the post if the answers help
Click "Unmark as Answer" if you need more direction
Thank you for your cooperation
harinathch
Member
2 Points
1 Post
Re: Access Dynamic GridView ItemTemplate after postback
Nov 02, 2009 02:17 PM|LINK
Hi Rajesh, We are also facing the same issue. Please let us know what is the solution for the above problem on how to save the data.
Thanks,
HarinathCh