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

Rebind grid on web control postback

2 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peichung
Top achievements
Rank 1
Peichung asked on 06 Mar 2009, 02:49 PM

I have a button inside a user web control (.ascx) that does the postback.  I would like the grid to refresh on the postback of the button click.  Therefore, I refresh the grid in the aspx’s Page_Load:

        if (IsPostBack)

            Grid1.Rebind();

I also added validateRequest="false" EnableEventValidation="false" to <%@ Page%> to supress the invalid postback exception.  This did refresh my grid on the web control button click postback.  However, when I put the grid item in edit mode, none of the events in the edit form was fired, such as button click or combo’s selection change.   I am guessing this is because Grid1.Rebind(); in Page_Load  cause ItemDataBound event raised when an event was fired in the edit form.  Here is my ItemDataBound handler:

    protected void Grid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            MyCombo combo = editedItem.FindControl("myCombo") as MyCombo;
            combo.UserName = UserName;
            combo.DataBind();
        }
    }

 Please help. Thanks,

2 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 10 Mar 2009, 09:51 AM
Hi Peichung,

Can you please check whether you are using advanced binding for your grid with NeedDataSource event handling? This should ensure that the grid will be bound to data each time when necessary and you can safely remove the Rebind() call on !IsPostBack condition inside the PageLoad handler. I suppose that duplicate bindings are the cause for the issue in your particular case.

Modify your code accordingly and let us know whether this helps.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Peichung
Top achievements
Rank 1
answered on 16 Mar 2009, 03:32 PM

After some digging, I realized that NeedDataSource will not be raised if the postback was caused by web components other than the grid itself, such as button click.  Therefore, I should have called Rebind() in the button's OnClick event handler.  However, in my situation, I cannot do that because my button is sitting inside a user control (.ascx).  I finally figured out a work around by creating a hidden field in the user control and set the hidden field's value in the client side event handler.  Then in the hosting page's Page_Load(), I use the hidden field discern whether the postback was caused by the user control and, therefore, call Rebind() when needed.

 

 

 

Tags
Grid
Asked by
Peichung
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Peichung
Top achievements
Rank 1
Share this question
or