选择Gridview行

问题描述:



如何在单击GridView的每一行时显示警报?

在此先感谢...

Hi,

How can I display an alert on clicking on each row of a GridView?

Thanks in advance...

在GridView上添加OnRowDataBound处理程序
On GridView add OnRowDataBound handler
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnRowDataBound="grd_OnRowDataBound">



和后面的代码



And Code behind

public void grd_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick",String.Format("javascript:alert('You clicked {0}')", e.Row.RowIndex));
        }
    }


对于Gridview是RowDataBound 事件,对于Datagrid是ItemDataBound 事件.

绑定该事件,然后访问每一行.这是在数据源绑定到网格时触发的.在此处附加Javascript,例如:
It''s RowDataBound event for Gridview and ItemDataBound for Datagrid.

Bind that event and then access each row. This is triggered at the time datasource is binded to grid. Append the Javascript here, like:
e.Row.Attributes.Add("onclick","javascript:alert('Hi')");


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                cell.Attributes.Add("onclick", "javascript:alert('hai');");
                cell.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
            }
        }
    }