Select a row on mouse click in gridview

浪子不回头ぞ 提交于 2020-01-04 05:34:28

问题


I have a problem, I want to select a row in gridview on mouse click.

My code is this :

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

it is not working. i don't know why?

plz suggest me regarding that.

"Thanks"


回答1:


Found the tutorial about ASP.Net select row in gridview
In ASPX page under GridView tag add:

<SelectedRowStyle BackColor="Orange" />

In code behind try the following:

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

You can then catch this event using the RowCommand (something like).

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


来源:https://stackoverflow.com/questions/10314215/select-a-row-on-mouse-click-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!