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

Columns missing after PostBack

8 Answers 261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hemika
Top achievements
Rank 1
Hemika asked on 26 Mar 2013, 05:27 PM
Hello, 
I have a dropdown, 2 link buttons(Lets say A and B) and a radgrid on my mark up page. The dropdown will select my DataSource for the grid. LinkButton A will get the data source and and adds the columns to the grid programmatically and binds them all. The other(LinkButton B) will get the datafrom the grid and do something different(not related to the page), anyway it will create a postback. The columns will be different depending on the selected value of the dropdown as is the data source. I am able to bind the data and columns on the grid programmatically. But when I click on LinkButton B, The columns go missing but the data is still there. As the header texts are missing. (Check the images attached). Here is my code:

ASP Page
<asp:DropDownList ID="ddlDataSource" runat="server"></asp:DropDownList>
<asp:LinkButton ID="lnkBtnA" runat="server" Text="Button A" OnClick="LnkBtnA_OnClick"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnB" runat="server"  Text="Button B" OnClick="LnkBtnB_OnClick"></asp:LinkButton>
<telerik:RadGrid ID="radGrid1" runat="server">
   <ExportSettings OpenInNewWindow="true" ExportOnlyData="true" IgnorePaging="true">
   </ExportSettings>
   <ClientSettings>
      <Scrolling AllowScroll="true" UseStaticHeaders="true" ScrollHeight="200px" />
   </ClientSettings>
   <MasterTableView Width="100%" HorizontalAlign="NotSet" AutoGenerateColumns="False" NoMasterRecordsText="No Items available">
      <Columns>
       </Columns>
   </MasterTableView>
</telerik:RadGrid>

C# Code : 
protected void LnkBtnA_OnClick(object sender, EventArgs e)
{
    try
    {
        this.radGrid1.Visible = true;
        this.radGrid1.MasterTableView.Columns.Clear();
        switch (this.ddlDataSource.SelectedValue)
        {
            case "DataSource1":
                this.AddColumnsToGrid();
  
                this.radGrid1.DataSource = MyDataSource1;
                this.radGrid1.DataBind();
  
                break;
            case "DataSource2":
                this.AddColumnsToGrid();
  
                this.radGrid1.DataSource = MyDataSource2;
                this.radGrid1.DataBind();
                break;
            default:
                break;
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}
 
        private void AddColumnsToGrid()
        {
            this.radGrid1.MasterTableView.Columns.Clear();
            switch (this.ddlDataSource.SelectedValue)
            {
                case "DataSource1":
                    GridBoundColumn subjectColumnR1 = new GridBoundColumn();
                    subjectColumnR1.DataField = "Subject";
                    subjectColumnR1.HeaderText = "Subject";
                    this.radGrid1.MasterTableView.Columns.Add(subjectColumnR1);
 
                    GridBoundColumn dueDateColumnR1 = new GridBoundColumn();
                    dueDateColumnR1.DataField = "DueDateTime";
                    dueDateColumnR1.HeaderText = "DueDateTime";
                    this.radGrid1.MasterTableView.Columns.Add(dueDateColumnR1);
 
                    GridBoundColumn ownerColumnR1 = new GridBoundColumn();
                    ownerColumnR1.DataField = "Owner";
                    ownerColumnR1.HeaderText = "Owner";
                    this.radGrid1.MasterTableView.Columns.Add(ownerColumnR1);
                    break;
                case "DataSource2":
                    GridBoundColumn subjectColumnR2 = new GridBoundColumn();
                    subjectColumnR2.DataField = "Subject";
                    subjectColumnR2.HeaderText = "Subject";
                    this.radGrid1.MasterTableView.Columns.Add(subjectColumnR2);
 
                    GridBoundColumn dueDateColumnR2 = new GridBoundColumn();
                    dueDateColumnR2.DataField = "DueDateTime";
                    dueDateColumnR2.HeaderText = "DueDateTime";
                    this.radGrid1.MasterTableView.Columns.Add(dueDateColumnR2);
 
                    GridBoundColumn ownerColumnR2 = new GridBoundColumn();
                    ownerColumnR2.DataField = "Manager";
                    ownerColumnR2.HeaderText = "Manager";
                    this.radGrid1.MasterTableView.Columns.Add(ownerColumnR2);
                    break;
                default:
                    break;
            }
        }
 
        protected void LnkBtnB_OnClick(object sender, EventArgs e)
        {
            try
            {
                this.AddColumnsToGrid(); // <-- Even though I do this. The columns go missing.
                // Does something not related to the question
            }
            catch (Exception ex)
            {
                throw;
            }
        }

I saw this post as well. The RadGrid is created on Page_Init and then added. But I want to keep the mark up and only bind the data and columns through server side. I tried using Rebind() and DataBind() function on 'LnkBtnB_OnClick', Then all the data disappears. I do not want to Bind the data on every post back.

If theres a way to make the columns not to disappear then its great. I am also okay if theres a fix to bind only the columns as well. But not to bind all the data.

Thank you

8 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Mar 2013, 08:22 PM
Hello,

Please try with below code snippet.

protected void BindData()
        {
            try
            {
                this.radGrid1.Visible = true;
                this.radGrid1.MasterTableView.Columns.Clear();
                dynamic data = new[] {
              new { ID = 1, Name ="aaa"},
              new { ID = 2, Name = "bbb"},
              new { ID = 3, Name = "ccc"},
              new { ID = 4, Name = "ddd"},
              new { ID = 5, Name ="eee"},
              new { ID = 6, Name ="aaa"},
              new { ID = 7, Name = "bbb"},
              new { ID = 8, Name = "ccc"},
              new { ID = 9, Name = "ddd"},
              new { ID = 10, Name ="eee"}
            };
 
                switch (this.ddlDataSource.SelectedValue)
                {
                    case "DataSource1":
                        this.AddColumnsToGrid();
 
                        this.radGrid1.DataSource = data;
                        this.radGrid1.DataBind();
 
                        break;
                    case "DataSource2":
                        this.AddColumnsToGrid();
 
                        this.radGrid1.DataSource = data;
                        this.radGrid1.DataBind();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
        protected void LnkBtnA_OnClick(object sender, EventArgs e)
        {
            BindData();
        }
 
        private void AddColumnsToGrid()
        {
            this.radGrid1.MasterTableView.Columns.Clear();
            switch (this.ddlDataSource.SelectedValue)
            {
                case "DataSource1":
                    GridBoundColumn subjectColumnR1 = new GridBoundColumn();
                    subjectColumnR1.DataField = "ID";
                    subjectColumnR1.HeaderText = "ID";
                    this.radGrid1.MasterTableView.Columns.Add(subjectColumnR1);
                    break;
                case "DataSource2":
                    GridBoundColumn subjectColumnR2 = new GridBoundColumn();
                    subjectColumnR2.DataField = "Name";
                    subjectColumnR2.HeaderText = "Name";
                    this.radGrid1.MasterTableView.Columns.Add(subjectColumnR2);
                    break;
                default:
                    break;
            }
        }
 
        protected void LnkBtnB_OnClick(object sender, EventArgs e)
        {
            try
            {
                this.AddColumnsToGrid();
                BindData();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
<div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <asp:DropDownList ID="ddlDataSource" runat="server">
            <asp:ListItem Text="DataSource1" Value="DataSource1"></asp:ListItem>
            <asp:ListItem Text="DataSource2" Value="DataSource2"></asp:ListItem>
        </asp:DropDownList>
        <asp:LinkButton ID="lnkBtnA" runat="server" Text="Button A" OnClick="LnkBtnA_OnClick"></asp:LinkButton>
        <asp:LinkButton ID="lnkBtnB" runat="server" Text="Button B" OnClick="LnkBtnB_OnClick"></asp:LinkButton>
        <telerik:RadGrid ID="radGrid1" runat="server">
            <ExportSettings OpenInNewWindow="true" ExportOnlyData="true" IgnorePaging="true">
            </ExportSettings>
            <ClientSettings>
                <Scrolling AllowScroll="true" UseStaticHeaders="true" ScrollHeight="200px" />
            </ClientSettings>
            <MasterTableView Width="100%" HorizontalAlign="NotSet" AutoGenerateColumns="False"
                NoMasterRecordsText="No Items available">
                <Columns>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>


Thanks,
Jayesh Goyani
0
Hemika
Top achievements
Rank 1
answered on 27 Mar 2013, 03:04 AM
Hello Jayesh,
Thanks for the reply. In the code snippet you provided, The 'BindData()' happens on both 'LnkBtnA_OnClick' and 'LnkBtnB_OnClick' . I only want the binding to happen on 'LnkBtnA_OnClick' . This is because I have a bigger process behind the scene to get my datasource. I cant repeat it on each and every postBack. If theres a way to stop the columns from dissappearing, It would be better. Or even to bind the columns on each postBack. But not the datasource.

Thank you
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Mar 2013, 04:55 PM
Hello,

We have to call BindData() in both button click.

On LnkBtnA click get the data from the DB and store this data in session/viewstate/Cache.
OnLnkBtnB click get the data from session/viewstate/Cache and assign to RadGrid.

Thanks,
Jayesh Goyani
0
Hemika
Top achievements
Rank 1
answered on 30 Mar 2013, 06:10 AM
Hello Jayesh,
I cannot save my datasource on session/viewstate/Cache. I might get more than 40,000 records. So holding that up is not good for performance of the page. 
Also I noticed that, actually the columns aren't going missing its just only the "Header Text" property of the column. 

Thanks, 
Hemika Kodikara


0
Hemika
Top achievements
Rank 1
answered on 30 Mar 2013, 09:00 AM
I switched to an ASP GridView as I am only showing data. Problem solved. :)
0
Jennifer
Top achievements
Rank 1
answered on 19 Apr 2013, 07:19 PM
You need to turn your scrolling feature off and it will fix it for you.  I am still on the search for how to turn it back on but columns and headers were fine after i turned the scroll feature off.
0
Hemika
Top achievements
Rank 1
answered on 20 Apr 2013, 06:47 AM
Thanks for the reply. Currently I am using the asp gridview. Incase if I need to move to radgrid I will use your solution. Any idea why it is?
0
Fernando
Top achievements
Rank 2
answered on 16 Mar 2016, 02:47 PM

I know this is a old post, but I can't find something newer and so similar to my problem.
I think it's happen because we can't create buttons that we can turn off postback, except if we use template columns. And they are so boring, at least for me.
In my case I use the OnItemCreated server side event of the radgrid to set the OnClientClick of the Image Buttons on the first column of my grid, doing something like this:

protected void GridRelatorio_OnItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataitem = e.Item as GridDataItem;
 
        ImageButton btn = dataitem["btncol"].Controls[0] as ImageButton;
        btn.ToolTip = "oioi";
        btn.OnClientClick = "alert('oioi'); return false;";
    }
}

And this prevent the postback and solve my problem.
Tags
Grid
Asked by
Hemika
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Hemika
Top achievements
Rank 1
Jennifer
Top achievements
Rank 1
Fernando
Top achievements
Rank 2
Share this question
or