在gridview中单击鼠标时选择一行

时间:2012-04-25 10:55:48

标签: c#-4.0 ms-access-2007

我遇到问题,我想在鼠标点击中选择gridview中的一行。

我的代码是:

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvdetails, "Select$" + e.Row.RowIndex);
        }
    }

它不起作用。我不知道为什么?

plz建议我这个。

“谢谢”

1 个答案:

答案 0 :(得分:4)

找到关于ASP.Net select row in gridview的教程 在GridView标签下的ASPX页面中添加:

<SelectedRowStyle BackColor="Orange" />

在代码后面尝试以下内容:

protected override void Render(System.Web.UI.HtmlTextWriter writer) 
{ 
    foreach (GridViewRow row in PeopleGridView.Rows) { 
        if (row.RowType == DataControlRowType.DataRow) { 
            row.Attributes["onmouseover"] =  
               "this.style.cursor='hand';this.style.textDecoration='underline';"; 
            row.Attributes["onmouseout"] =  
               "this.style.textDecoration='none';"; 
            // Set the last parameter to True 
            // to register for event validation. 
            row.Attributes["onclick"] =  
             ClientScript.GetPostBackClientHyperlink(PeopleGridView, 
                "Select$" + row.DataItemIndex, true); 
        } 
    } 
    base.Render(writer); 
}

然后,您可以使用RowCommand(类似于)来捕获此事件。

private void PeopleGridView_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Select") { 
        // Get the list of customers from the session  
        List<Customer> customerList = 
                 Session["Customers"] as List<Customer>;

         Debug.WriteLine(customerList[Convert.ToInt32(e.CommandArgument)].LastName);
    } 
}