C# ASP.NET gridview 行 onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9469037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:32:24  来源:igfitidea点击:

ASP.NET gridview row onclick

c#javascriptasp.netgridviewonclick

提问by steventnorris

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?

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

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.

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

C# server-side

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 client-side

Javascript 客户端

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

采纳答案by Lukinha RS

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

我在 GridView 的 RowDataBound 中使用它来添加属性以选择行:

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

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

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

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

回答by anAgent

To do this in jQuery, simply get the row click event like this:

要在 jQuery 中执行此操作,只需像这样获取行单击事件:

$(document).ready(function () {
    var clickCnt = 0;
    $('table tr').click(function(){
        clickCnt++;
        //Do something 
    });
});

With that, I recommend setting the TR IDto the primary key to the object that is displayed in the row.

因此,我建议将 TR ID设置为行中显示的对象的主键。

回答by Leon

Is the Gridset up to call dataTbl_RowDataBoundevent? If you debug with a breakpoint in that event, does that event get fired?

是否Grid设置了呼叫dataTbl_RowDataBound事件?如果在该事件中使用断点进行调试,该事件是否会被触发?

回答by Kadir