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

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

问题描述

我试图有一个onclick事件添加到行,一旦数据绑定到一个gridview夏精。的code下不加入任何属性(检查viewsource一旦创建的网页),当然,不加功能。现在,我刚刚拿到的onclick打印页面,但最终还是会链接到另一个页面。对什么是错的任何想法?

此外,由于计算器社区。这个社区一直是一个很大的帮助。计划通过一些职位,这个周末去自己,我可以给回位开始回答问题。

C#服务器端

 保护无效dataTbl_RowDataBound(GridViewRowEventArgs E){
            e.Row.Attributes.Add(ID,e.Row.Cells [0]。文本);
            e.Row.Attributes.Add(的onclick,rowClick('+ e.Row.RowIndex +'));        }

JavaScript客户端

 函数rowClicked(计数器){
    文件撰写(柜);
}


解决方案

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

 保护无效grvSearch_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    尝试
    {
        开关(e.Row.RowType)
        {
            案例DataControlRowType.Header:
                // ...
                打破;
            案例DataControlRowType.DataRow:
                e.Row.Attributes.Add(的onmouseover,this.style.backgroundColor ='#93A3B0'; this.style.color ='白'; this.style.cursor ='指针');
                如果(e.Row.RowState == DataControlRowState.Alternate)
                {
                    e.Row.Attributes.Add(的onmouseout的String.Format(this.style.color ='黑'; this.style.backgroundColor ='{0};,grvSearch.AlternatingRowStyle.BackColor.ToKnownColor() ));
                }
                其他
                {
                    e.Row.Attributes.Add(的onmouseout的String.Format(this.style.color ='黑'; this.style.backgroundColor ='{0};,grvSearch.RowStyle.BackColor.ToKnownColor() ));
                }
                e.Row.Attributes.Add(onclick事件,Page.ClientScript.GetPostBackEventReference(grvSearch,选择$+ e.Row.RowIndex.ToString()));
                打破;
        }
    }
    抓住
    {
        //...扔
    }
}

这赶上去的事件当用户单击该行:

 保护无效grvSearch_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    尝试
    {
        //做任何你想要与grvSearch.SelectedIndex
    }
    抓住
    {
        //...扔
    }
}

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?

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# server-side

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

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

解决方案

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

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天全站免登陆