ASP.NET gridview 行 onclick [英] ASP.NET gridview row onclick

查看:31
本文介绍了ASP.NET gridview 行 onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦数据绑定到 gridview webcontrol,我试图将 onclick 事件添加到行中.下面的代码没有添加任何属性(在创建页面后检查视图源),当然也没有添加任何功能.现在,我刚刚将 onclick 打印到页面,但最终它会链接到另一个页面.对出了什么问题有什么想法吗?

I'm attempting to have an onclick event added to a row once the data is bound to a gridview webcontrol. The code below is not adding any attributes (checked the viewsource once the page is created) and, of course, no functionality is added. Right now, I've just got the onclick printing to the page, but eventually it will link to another page. Any ideas on what's wrong?

另外,感谢整个 stackoverflow 社区.这个社区一直是一个很大的帮助.计划在这个周末自己浏览一些帖子并开始回答问题,因为我可以回馈一下.

Also, thanks to the stackoverflow community at large. This community has always been a great help. Plan to go through some posts myself this weekend and start answering questions as I can to give back a bit.

C# 服务器端

protected void dataTbl_RowDataBound(GridViewRowEventArgs e){
            e.Row.Attributes.Add("id",e.Row.Cells[0].Text);
            e.Row.Attributes.Add("onclick", "rowClick('"+e.Row.RowIndex+"')");

        }

Javascript 客户端

Javascript client-side

function rowClicked(counter){
    document.write(counter);
}

推荐答案

我在我的 GridView 的 RowDataBound 中使用这个,添加属性来选择行:

I'm using this in the RowDataBound of my GridView, to add the attribute to select the row:

protected void grvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:
                //...
                break;
            case DataControlRowType.DataRow:
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#93A3B0'; this.style.color='White'; this.style.cursor='pointer'");
                if (e.Row.RowState == DataControlRowState.Alternate)
                {
                    e.Row.Attributes.Add("onmouseout", String.Format("this.style.color='Black';this.style.backgroundColor='{0}';", grvSearch.AlternatingRowStyle.BackColor.ToKnownColor()));
                }
                else
                {
                    e.Row.Attributes.Add("onmouseout", String.Format("this.style.color='Black';this.style.backgroundColor='{0}';", grvSearch.RowStyle.BackColor.ToKnownColor()));
                }
                e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(grvSearch, "Select$" + e.Row.RowIndex.ToString()));
                break;
        }
    }
    catch 
    {
        //...throw
    }
}

当用户单击该行时,这是为了捕获 de 事件:

And this to catch de event when an user click the row:

protected void grvSearch_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        //Do wherever you want with grvSearch.SelectedIndex                
    }
    catch
    {
        //...throw
    }
}

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

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