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

Get RadGrid ClientID from sender in javascript

2 Answers 555 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darren
Top achievements
Rank 1
Darren asked on 17 Oct 2012, 05:17 PM
I'm trying to get the ClientID of a RadGrid from a sender object in a javascript event.   I need to get the RadGrid from a filteritem keypress event.  How do I get that?

This is crude but I can get it like this:

                var RadGrid1 = $find(sender.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id);

Anybody got any idea how to get the uniquename from the sender object?

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Oct 2012, 06:03 PM
Hello,

function KeyPress(sender) {
               var GetGridClientID = sender.getAttribute("GID");
               var GetGridobject = $find(sender.getAttribute("GID"));
           }
<telerik:GridTemplateColumn>
                        <FilterTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" onkeypress="KeyPress(this);"></asp:TextBox>
                        </FilterTemplate>
</telerik:GridTemplateColumn>
  protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridFilteringItem)
            {
                GridFilteringItem item = e.Item as GridFilteringItem;
                TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
                TextBox1.Attributes.Add("GID", RadGrid1.ClientID);
            }
}


Thanks,
Jayesh Goyani
0
Darren
Top achievements
Rank 1
answered on 17 Oct 2012, 06:09 PM
Oh!  I see!

    [ToolboxData("<{0}:RadGridEx runat='server'></{0}:RadGridEx>")]
    public class RadGridEx : RadGrid
    {
        public RadGridEx()
            : base()
        {
...
}

        protected override void OnItemDataBound(GridItemEventArgs e)
        {
            base.OnItemDataBound(e);

            if ( e.Item is GridFilteringItem )
            {
                GridFilteringItem item = e.Item as GridFilteringItem;
                foreach (GridColumn col in this.MasterTableView.Columns)
                {
                    TextBox box = item[col.UniqueName].Controls[0] as TextBox;
                    if ( box != null )
                    {
                        box.Attributes.Add("GID", this.ClientID);
                        box.Attributes.Add("UNIQUENAME", col.UniqueName);
                    }
                }
            }
        }

        protected override void OnItemCreated(GridItemEventArgs e)
        {
            base.OnItemCreated(e);
            if (e.Item is GridFilteringItem)
            {
                GridFilteringItem item = (GridFilteringItem)e.Item;

                foreach (GridColumn col in this.MasterTableView.Columns)
                {
                    TextBox box = item[col.UniqueName].Controls[0] as TextBox;
                    if ( box != null )
                    {
                        box.Attributes.Add("onkeypress", "doFilter(this, event)");
                    }
                }
            }
        }
}

        function doFilter(sender, e) {
            if (e.keyCode == 13) {
                e.cancelBubble = true;
                e.returnValue = false;
                if ( e.stopPropagation ) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                var RadGrid1ClientID = sender.getAttribute("GID");
                var RadGrid1 = $find(RadGrid1ClientID);
                var masterTable = RadGrid1.get_masterTableView();
                var uniqueName = sender.getAttribute("UNIQUENAME");
                masterTable.filter(uniqueName, sender.value, Telerik.Web.UI.GridFilterFunction.Contains);
            }
        } 

This could be a bit simpler but it works.  Thanks Jayesh!  The attribute thing made this work!
Tags
Grid
Asked by
Darren
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Darren
Top achievements
Rank 1
Share this question
or