Databinding keeps disappearing when post back

1 Answer 88 Views
Grid OrgChart
yuki
Top achievements
Rank 2
Iron
Iron
Iron
yuki asked on 20 Apr 2023, 10:58 AM

My page logic is that I click on an 'Apply' button, then in the Apply_Click method, I do some data binding, such as: Grid1.Datasource = xxx; Grid1.DataBind(); . And the grid can show properly on the page. But when I click on other buttons on this page (for example: click the filter on the grid) to trigger a post back, the content on the grid just disappears (The datasource is stored in a session, so it's not datasource's problem). I need to add databind() to Page_Load() to prevent this from happening. But this is very weird and feels not right. Can you help me locate the real problem?

The same issue is happening bothe in Grid and OrgChart.

1 Answer, 1 is accepted

Sort by
0
Accepted
Doncho
Telerik team
answered on 25 Apr 2023, 06:36 AM

Hi Yuki,

The RadGrid is a complex control that has its own binding procedure (it includes calls to the DataBind() internally when needed). Explicit calls to the DataBind usually lead to unexpected behavior of most of the RadGrid features. Whenever refreshing the RadGrid is needed you should use the Rebind() method.

Please make sure that the RadGrid is bound either via the NeedDataSource event or to a Declarative DataSource. Also, make sure the DataBind() method is not used anywhere for the Grid. More details about the binding procedures of RadGrid you can find here

In case you need to change the DataSource bound to the RadGrid runtime, here is one approach you can use while keep binding the Grid in the NeedDataSource event:

Define a custom property that holds the DataSource, change the value of the property, and rebind the Grid:

private DataTable _gridDataSource;

public DataTable GridDataSource
{
    get 
    {
        if (_gridDataSource == null)
        {
            //set initial data source
            _gridDataSource = OrdersTable();
        }
        return _gridDataSource; 
    }
    set
    {
        _gridDataSource = value;
    }
}

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = GridDataSource;
}

//change the data source of the RadGrid on a button click
protected void RadButton1_Click(object sender, EventArgs e)
{
    //change the datasource and rebind the RadGrid
    GridDataSource = EmployeesTable();
    RadGrid1.Rebind();
}

I hope this will help you achieve the desired behavior.

Please let me know if any further questions come up.

Kind regards,
Doncho
Progress Telerik

Heads up! Telerik UI for ASP.NET AJAX versions for .NET 3.5 and 4.0 are retired. Progress will continue shipping assemblies compatible with .NET 4.5 and later. See whether this affects your apps in this article.
yuki
Top achievements
Rank 2
Iron
Iron
Iron
commented on 02 May 2023, 02:19 AM

Hello Doncho, thanks for the answer, I tried to use Rebind() instead of DataBind() in the method of button click, and it worked fine. Your answer exactly solved my problem, thanks!
yuki
Top achievements
Rank 2
Iron
Iron
Iron
commented on 02 May 2023, 02:43 AM | edited

Hello Doncho, sorry to bother you again. Thanks for solving the problem of rebinding data for Rad Grid. But how about Org Chart, I also have problem rebinding data for org charts, and org charts have no Rebind() method. Could you please give me some guidance on that? Many thanks.

The data for my Org charts also disappear after post back, just like in rad grid. I click on a button to add datasource to the org chart, and bind it, it works all right, and after I trigger some post backs on the same page, the org chart becomes blank which means the databinding is not valid.

Doncho
Telerik team
commented on 04 May 2023, 01:30 PM

Hi Yuki,

Yes, RadOrgChart is quite different than RadGrid in terms of binding.

The OrgChart should be data-bound on each page load or in the Page_Init event so it can be processed in the Control state and persist across postbacks. As in the current case the data source of the chart should be changed dynamically it seems that the Page_Load event is suitable for the purpose.

Setting a new data source to the OrgChart is possible in any Control you should make sure to call the DataBind() method of the OrgChart afterward.

To be able to change the DataSource of the OrgChart and keep using the new data source after a postback occurs you can use a Session variable to maintain the current instance of the DataSource object so it can be set back to the control after page reloads due to postback. 

Here is one example you can refer to:

<telerik:RadButton runat="server" ID="RadButton1" Text="Change DataSource" AutoPostBack="true" OnClick="RadButton1_Click" />

<telerik:RadButton runat="server" ID="RadButton2" Text="Postback" AutoPostBack="true" />

<telerik:RadOrgChart RenderMode="Lightweight" ID="RadOrgChart1" runat="server" Skin="Default">
</telerik:RadOrgChart>

C#

public DataTable SessionDataSource
{
    get
    {
        if (Session["SessionDataSource"] == null || !IsPostBack)
        {
            Session["SessionDataSource"] = Createcategories();
        }
        return (DataTable)Session["SessionDataSource"];
    }
    set
    {
        Session["SessionDataSource"] = value;
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    RadOrgChart1.DataFieldID = "CategoryID";
    RadOrgChart1.DataFieldParentID = "ParentCategory";
    RadOrgChart1.DataTextField = "Name";
    RadOrgChart1.DataSource = SessionDataSource;
    //bind the orgchart on each load
    RadOrgChart1.DataBind();
}

private DataTable Createcategories()
{
    var categories = new DataTable();
    categories.Columns.Add("CategoryID");
    categories.Columns.Add("ParentCategory");
    categories.Columns.Add("Name");

    categories.Rows.Add(new string[] { "1", null, "MATTER" });
    categories.Rows.Add(new string[] { "2", "1", "SUBSTANCE" });
    categories.Rows.Add(new string[] { "3", "1", "MIXTURES" });
    categories.Rows.Add(new string[] { "4", "2", "HOMOGENEOUS" });
    categories.Rows.Add(new string[] { "5", "4", "ELEMENTS" });
    categories.Rows.Add(new string[] { "6", "4", "COMPOUNDS" });
    categories.Rows.Add(new string[] { "7", "5", "METALS" });
    categories.Rows.Add(new string[] { "8", "5", "METALLOIDS" });
    categories.Rows.Add(new string[] { "9", "5", "NONMETALS" });
    categories.Rows.Add(new string[] { "10", "6", "ORGANIC" });
    categories.Rows.Add(new string[] { "11", "6", "INORGANIC" });
    categories.Rows.Add(new string[] { "12", "3", "HOMOGENEOUS" });
    categories.Rows.Add(new string[] { "13", "3", "HETEROGENEOUS" });
    categories.Rows.Add(new string[] { "14", "12", "SOLUTIONS" });
    categories.Rows.Add(new string[] { "15", "13", "COLLOIDS" });
    categories.Rows.Add(new string[] { "16", "13", "SUSPENSIONS" });
    return categories;
}

protected void RadButton1_Click(object sender, EventArgs e)
{
    //generate a new data source and store it in the session variable
    var newCategories = new DataTable();
    newCategories.Columns.Add("CategoryID");
    newCategories.Columns.Add("ParentCategory");
    newCategories.Columns.Add("Name");

    newCategories.Rows.Add(new string[] { "1", null, "MATTER" });
    newCategories.Rows.Add(new string[] { "2", "1", "SUBSTANCE" });
    newCategories.Rows.Add(new string[] { "3", "1", "MIXTURES" });

    SessionDataSource = newCategories;

    //set the new datasource and bind the OrgChart
    RadOrgChart1.DataSource = SessionDataSource;
    RadOrgChart1.DataBind();
}

yuki
Top achievements
Rank 2
Iron
Iron
Iron
commented on 05 May 2023, 02:55 AM

Hi Doncho, thanks for your detailed explanation and codes! 😊 I bind my datasource in page load every time when there is a post back, and it works all right now.
Tags
Grid OrgChart
Asked by
yuki
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Doncho
Telerik team
Share this question
or