0

How can I add onClientClick event to this

<a href="javascript:__doPostBack('ctl00$m$g_8dbaa58f_d132_4042_b180_3c555d885dde$ctl00$GridView1','Delete$0')">Delete</a>

Since it's a commandfield in Gridview so I couldn't get it directly, check this question for more information.

Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152

2 Answers2

1

Subscribe to the gridview's RowDataBound event and search for the anchor tag in the item's Controls collection.

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        // this will apply the action in the lambda expression to all controls or sub-controls of the row matching type LinkButton  
        // (you may have a different control type and you may want to alter your lambda expression to check that the control matched is really the control you want to manipulate)
        SearchControls<LinkButton>(e.Row, button => button.OnClientClick = "alert('here');");
    }

    // recursive Control search function
    private void SearchControls<T>(Control start, Action<T> itemMatch)
    {
        foreach (var c in start.Controls.OfType<T>())
            itemMatch(c);

        foreach (var c in start.Controls.OfType<Control>())
            SearchControls<T>(c, itemMatch);
    }
Nathan
  • 6,095
  • 2
  • 35
  • 61
0

Please try this.

<a href = "#" onclick="alert('Client click event..!')">Detail</a>
Pavan Kumar
  • 190
  • 2
  • 8