选择Gridview行 [英] Select Gridview Row

查看:65
本文介绍了选择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");
            }
        }
    }


这篇关于选择Gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆