Access Dynamic GridView ItemTemplate after postback

Last post 11-02-2009 10:17 AM by harinathch. 5 replies.

Sort Posts:

  • Access Dynamic GridView ItemTemplate after postback

    01-30-2008, 2:28 PM
    • Member
      point Member
    • krajeshkumar
    • Member since 01-30-2008, 5:06 PM
    • Posts 2

     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

     

     

  • Re: Access Dynamic GridView ItemTemplate after postback

    01-30-2008, 2:47 PM
    • Participant
      905 point Participant
    • Joseph Ghassan
    • Member since 07-24-2005, 4:48 AM
    • Lebanon
    • Posts 133

    Hi, try to access the controls at Gridview DataBound event Here :

    protected void GridView1_DataBound(object sender, EventArgs e)

     {

    }

    Hope this helps,

    Joseph

    Joseph Ghassan
    MCP |
    Blog


    MyPostAnsweredYourQuestion?ClickAnswer:DoNothing ;

  • Re: Access Dynamic GridView ItemTemplate after postback

    01-30-2008, 2:49 PM
    • Participant
      905 point Participant
    • Joseph Ghassan
    • Member since 07-24-2005, 4:48 AM
    • Lebanon
    • Posts 133

    Hi, try to access the controls at Gridview DataBound event Here :

    protected void GridView1_DataBound(object sender, EventArgs e)

     {

    }

    Hope this helps,

    Joseph

    Joseph Ghassan
    MCP |
    Blog


    MyPostAnsweredYourQuestion?ClickAnswer:DoNothing ;

  • Re: Access Dynamic GridView ItemTemplate after postback

    01-30-2008, 3:47 PM
    • Member
      point Member
    • krajeshkumar
    • Member since 01-30-2008, 5:06 PM
    • Posts 2

     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 

  • Re: Access Dynamic GridView ItemTemplate after postback

    02-01-2008, 4:01 AM
    Answer

     

    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
  • Re: Access Dynamic GridView ItemTemplate after postback

    11-02-2009, 10:17 AM
    • Member
      2 point Member
    • harinathch
    • Member since 11-02-2009, 10:15 AM
    • Posts 1

    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

Page 1 of 1 (6 items)