2

我有一个问题,我想通过鼠标单击在 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);
        }
    }

它不工作。我不知道为什么?

请就此向我提出建议。

“谢谢”

4

1 回答 1

4

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

<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);
    } 
}
于 2012-04-25T11:11:24.147 回答