如何将事件附加到此超链接按钮

时间:2013-05-01 09:37:10

标签: c# javascript dom gridview webforms

如何将onClientClick事件添加到此

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

因为它是Gridview中的命令字段所以我无法直接获取它check this question for more information

2 个答案:

答案 0 :(得分:1)

订阅gridview RowDataBound事件并在项目的Controls集合中搜索anchor标记。

    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);
    }

答案 1 :(得分:0)

请试一试。

<a href = "#" onclick="alert('Client click event..!')">Detail</a>