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

[Solved] How to attach javascript to the sort buttons?

1 Answer 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kelly
Top achievements
Rank 1
Kelly asked on 05 May 2008, 02:37 PM
Is there any way to attach my own javascript function call to the sort buttons in a RadGrid?  I need to execute javascript code when the sort button is clicked, but before the postback occurs.

I need to suppress an exit confirmation message ("Are you sure you want to leave this page? You have unsaved data etc.") for all links and buttons that keep the user on the same page.  All of the buttons I figured out except for the sort buttons.  In Firefox it doesn't show the exit confirmation at all (maybe because of the AJAX call) but in IE it does, regardless of the value I set for EnableAJAX in the grid.

The sort buttons don't seem to be server controls, so I'm not sure how to get to them in the codebehind.

So I tried to attach my function call with javascript like below ... this works for the first two postbacks but doesn't work after that.  I even tried putting this code in the grid footer in an attempt to make it execute again when the grid is refreshed (but it didn't).   I am out of ideas and would really appreciate some help with this. Thanks!

                            <script language="javascript"
                                //the purpose of this script is to suppress the Exit confirmation message in IE 
                                //for the sort links.  problem appears in IE only. appears in footer so that it 
                                //will execute again when a postback occurs in the grid 
                                var links = document.getElementsByTagName("a"); 
                                alert("found links"); 
                                for (var i=(links.length-1); i>=0; i--) 
                                { 
                                    if (links[i].getAttribute("title") == "Click here to sort"
                                    { 
                                        alert("found a sort button"); 
                                        var cmd = links[i].getAttribute("href").replace("javascript:"""); 
                                        links[i].onclick = function () { 
                                            suppressConfirmation(); 
                                            eval(cmd); 
                                            return false
                                        } 
                                    } 
                                } 
                            </script> 
 
 

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 08 May 2008, 05:39 AM
Hello Kelly,

You can assign the handler from the code behind, as shown in the code snippet below:

.cs
 protected void RadGrid1_PreRender(object sender, EventArgs e)  
    {  
        GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];  
        (headerItem["CustomerID"].Controls[0] as LinkButton).Attributes.Add("onclick", "customHandler();");  
    } 

I hope this information helps.

Regards,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Kelly
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or