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

GridButtonColumn - Prevent Post Back?

2 Answers 722 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bryan Kowalchuk
Top achievements
Rank 1
Bryan Kowalchuk asked on 13 Jan 2011, 06:06 PM
I have a simple Rad Grid with a GridButtonColumn in the item template. I have no problems attaching a onclick attribute through code to fire some Javascript on the page using the ItemCreated event.

The problem is that I cannot figure out how to prevent the user click (on a button or link style column) from causing a postback. I can clear the standard postback function with:

button.Attributes.Add("href", "#")

on the ItemDataBound event, but it still does a post back.

Any ideas?

FYI, the javascript set the URL and opens a RadWindow to a page with more details on the item in the grid, but the postback causes an unecessary page refresh and messes up the rad window as well.

2 Answers, 1 is accepted

Sort by
1
Shinu
Top achievements
Rank 2
answered on 14 Jan 2011, 07:25 AM
Hello Bryan,

You can attach the onClick event to the Button at runtime and use return false to avoid the postback. Sample code given below.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           Button button = (Button)item["PushButton1"].Controls[0];
           button.Attributes.Add("OnClick", "DoSomething(); return false;");
       }
   }

aspx:
<Columns>    
       <telerik:GridButtonColumn UniqueName="PushButton1" ButtonType="PushButton" HeaderStyle-Width="3%"
         ItemStyle-Width="3%">
       </telerik:GridButtonColumn>
 <Columns>


Thanks,
Shinu.
0
Bryan Kowalchuk
Top achievements
Rank 1
answered on 17 Jan 2011, 05:12 PM
The magic "return false;" worked great, thanks.
Tags
Grid
Asked by
Bryan Kowalchuk
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Bryan Kowalchuk
Top achievements
Rank 1
Share this question
or