This is a migrated thread and some comments may be shown as answers.

Dynamically created controls disappear or multiply on postback

4 Answers 278 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Derek Hunziker
Top achievements
Rank 1
Derek Hunziker asked on 26 Aug 2009, 05:05 AM
Hi There,

I'm trying to add some extra GridTemplateColumns on my Page_Init using my own custom template class. When the page loads the first time, everything is fine and dandy. Yet after a postback from a filter or paging command, things go horribly wrong. More specifically, the extra columns that I i'm adding on Page_Init remain, but for every postback, an extra set of blank columns are added to the grid. My initial thought was to just wrap my Page_Init inside a if(!isPostBack) statement, but then no extra columns show at all after a postback. Please help!

Here's my Page_Init:

protected void Page_Init(object sender, EventArgs e) 
    { 
        ListBuilderTableAdapters.listsTableAdapter listAdapter = new ListBuilderTableAdapters.listsTableAdapter(); 
        ListBuilder.listsDataTable dt = listAdapter.GetActive(); 
 
        foreach (ListBuilder.listsRow dr in dt.Rows) 
        { 
            GridTemplateColumn gtc = new GridTemplateColumn(); 
            gtc.HeaderText = dr.list_name; 
            gtc.ItemTemplate = new MailingListTemplate(dr.list_name, dr.id); 
            mlRadGrid.MasterTableView.Columns.Add(gtc); 
        } 
    } 

... and my custom template looks like this.

public class MailingListTemplate : ITemplate 
    { 
        protected string Text; 
        protected int listID; 
 
        public MailingListTemplate(string Text, int listID) 
        { 
            this.listID = listID; 
            this.Text = Text; 
        } 
 
        public void InstantiateIn(Control container) 
        { 
            HiddenField hf = new HiddenField(); 
            hf.Value = listID.ToString(); 
 
            CheckBox cb = new CheckBox(); 
 
            container.Controls.Add(hf); 
            container.Controls.Add(cb); 
        } 
    } 

Also, my NeedDataSource uses the same datasource that I bind to on my Page_Load:

protected void mlRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
            mlRadGrid.DataSource = recipientAdapter.GetUnique(); 
    } 

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Aug 2009, 06:44 AM
 Hi Derek,

Your above code seems to be correct. I could not find any thing wrong. But I am not sure why you want to bind the Grid in PageLoad event as well as NeedDataSource event.

Shinu
0
Derek Hunziker
Top achievements
Rank 1
answered on 26 Aug 2009, 05:09 PM
Hi Shinu,

Thanks for your response. I rarely use staticlly defined datasources in my ASPX. Instead, I usually bind stuff on my Page_Load with a little logic applied. The flip side to this is that the grid requires a NeedDataSource event handler to remember the datasource when you do a filter. I only specify the datasource in the NeedDataSource handler because the grid automatically rebinds.

As for my issue, I think I found my answer in the link you sent me:

"RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file."



0
Joe
Top achievements
Rank 1
answered on 30 Dec 2012, 06:35 AM
Hello ,

I am ahvind same issue which Derek had.I have two columns statically declared.

 

<telerik:GridBoundColumn DataField="VitalSign" FilterControlAltText="Filter VitalSign column" HeaderText="Vital Sign" SortExpression="VitalSign" UniqueName="VitalSign" HeaderStyle-Font-Bold="true" ColumnEditorID="id_vitalsign" ItemStyle-Font-Bold="true" HeaderStyle-Width="2.7%" ItemStyle-Width="2.7%" ItemStyle-Font-Size="10" >

 

</telerik:GridBoundColumn>

 

<telerik:GridTemplateColumn HeaderText="Latest" HeaderStyle-Font-Bold="true" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="1%" ItemStyle-Width="1%" ItemStyle-Font-Size="10" >

 

 

<ItemTemplate>

 

<asp:LinkButton ID="Latest1" CommandName="EditSelected" Text="Edit" runat="server"></asp:LinkButton>

 

<asp:Label ID="Latest" runat="server" Text='<%#Eval("Latest") %>'></asp:Label>

 

<asp:TextBox ID="Latest_txt" runat="server" Text='<%#Eval("Latest_value") %>' Visible="false" ></asp:TextBox>

 

<asp:HiddenField runat="server" Value='<%#Eval("VitalSignID") %>' ID="hf1" />

 

<asp:LinkButton ID="LinkButton2" CommandName="save" Text="save" runat="server" Visible="false"></asp:LinkButton>

 

</ItemTemplate>

 

</telerik:GridTemplateColumn>
other template columns will be dynamically generated.But when Click on link button shown above all the dynamically added columns header,data disapear.The columns reamain though.After every postback samethings happpens.Please help me in moving frward.Thanks; 

0
Shinu
Top achievements
Rank 2
answered on 31 Dec 2012, 09:07 AM
Hi Joe,

RadGrid does not support mixing declarative grid columns with grid columns added dynamically at run-time. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file. You can find out the same mentioned in the following help document.

Programmatic Creation

Thanks,
Shinu.

Tags
Grid
Asked by
Derek Hunziker
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Derek Hunziker
Top achievements
Rank 1
Joe
Top achievements
Rank 1
Share this question
or