Listen to GridButtonColumn click in javascript

5 posts, 0 answers
  1. Kirie
    Kirie avatar
    11 posts
    Member since:
    Aug 2011

    Posted 25 Nov 2011 Link to this post

    Hi, i have a grid with a GridButtonColumn. When the button is clicked i want to redirect the user (in javascript, and pass a long the bound value of the GridButtonColumn). i found how to do this serverside, but i cant figure out how to catch the click (and the value) in javascript.

    Any help would be appriciated.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Nov 2011 Link to this post

    Hello Kirie,

    Try the following code.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
      if (e.Item is GridDataItem)
    {
      GridDataItem item = (GridDataItem)e.Item;
      LinkButton link = (LinkButton)item["ColumnUniqueName"].Controls[0];
      link.Attributes.Add("onclick", "functionName('" + item.ItemIndex + "');");
     }
    }

    Thanks,
    Princy.
  3. Kirie
    Kirie avatar
    11 posts
    Member since:
    Aug 2011

    Posted 28 Nov 2011 Link to this post

    Thank you for your swift answer Princy,

    but what if i want to send the bound DataTextField as a parameter to the javascript method, (or one of the other columns bound value), how can I acheieve that? I am kind of new to javascript :)

    Best regards
    Kirie
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Nov 2011 Link to this post

    Hello Kirie,

    Try the following javascript:
    JS:
    function functionName(index)
    {
       var row = $find('<%= RadGrid1.ClientID %>').get_masterTableView().get_dataItems()[index];
       alert(row.getDataKeyValue("ID"));
    }

    -Shinu.
  5. sina
    sina avatar
    1 posts
    Member since:
    Aug 2017

    Posted 29 Aug 2017 in reply to Princy Link to this post

    hello there,

    thanks for your answers.

    I get this cast error:Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton

    it belongs to this line : LinkButton LinkButton1 = (LinkButton ) ditem["TemplateColumn3"].Controls[0]

    please tell me how can i cast that.

Back to Top