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

Dynamically added RadGrid gets hid when sorted !

2 Answers 59 Views
Grid
This is a migrated thread and some comments may be shown as answers.
MahMah
Top achievements
Rank 1
MahMah asked on 09 Jul 2012, 05:20 AM
Firstly I thought that I have a problem with RadTabStrip (this question) but after simplifying the question I found out that the problem is arising from RadGrid so here is the code to reproduce the problem :

Here is the web form code :

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="b1" OnClick="b1_OnClick" Text="Create Grid" />
        <asp:ScriptManager runat="server">
        </asp:ScriptManager>
        <asp:PlaceHolder runat="server" ID="ph1"></asp:PlaceHolder>
    </div>
    </form>
</body>

And here is the code behind :

protected void b1_OnClick(object sender, EventArgs e)
{
    var grid = new RadGrid
                   {
                       AllowSorting = true,
                        
                       DataSource = new List<string> { "abc", "def", "ghi", "klm" }
                   };
 
    ph1.Controls.Add(grid);
}

When I sort the only column of the grid column it causes the grid to disappear ! Is there any scientific reason for this disappearing ?

What have I missed in my code ?

Thank you for your consideration.


2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jul 2012, 05:44 AM
Hello,

Please check below code snippet.
<asp:Button runat="server" ID="b1" OnClick="b1_OnClick" Text="Create Grid" UseSubmitBehavior="false" />
     <asp:PlaceHolder runat="server" ID="ph1"></asp:PlaceHolder>
protected bool IsGridShow
    {
        get
        {
            if (ViewState["IsGridShow"] != null)
                return Convert.ToBoolean(ViewState["IsGridShow"]);
            else
                return false;
        }
        set
        {
            ViewState["IsGridShow"] = value;
        }
    }
 
 protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = new List<string> { "abc", "def", "ghi", "klm" };
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.Params["__EVENTTARGET"] != null && Request.Params["__EVENTTARGET"] == "b1") || IsGridShow)
        {
            var grid = new RadGrid
            {
                AllowSorting = true,
            };
            grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
            ph1.Controls.Add(grid);
            IsGridShow = true;
        }
}


Thanks,
Jayesh Goyani
0
MahMah
Top achievements
Rank 1
answered on 09 Jul 2012, 05:58 AM
Thanks Jayesh for your quick reply.

It works well.

But can I ask you why we have to add it every time the page loads ? It has some cost for us to implement this schema in large scale projects.

Is there any way to avoid adding it in every page load ? and what happened to the previously created grid ? does that exist in memory but we can not access it ? If its true so we have a memory leakage by using this way.

Thanks for your attention.
Tags
Grid
Asked by
MahMah
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
MahMah
Top achievements
Rank 1
Share this question
or