C# 回发后在gridview中高亮显示所选行
我正在使用下面的GetPostBackClientHyperlink来填充文本框,其中包含行选择的相关信息。我还希望在回发后高亮显示所选行。第二个属性将高亮显示该行,但不会回发。我似乎无法让他们一起工作 有什么想法吗 aspx.csC# 回发后在gridview中高亮显示所选行,c#,asp.net,C#,Asp.net,我正在使用下面的GetPostBackClientHyperlink来填充文本框,其中包含行选择的相关信息。我还希望在回发后高亮显示所选行。第二个属性将高亮显示该行,但不会回发。我似乎无法让他们一起工作 有什么想法吗 aspx.cs protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Attributes["onclick"] = ClientScript
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
//e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex);
}
您必须在从row onclick回发后添加突出显示,如下所示:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();//Your method to set datasource anddatabind GridView1
GridView1.Rows[index].Attributes.Add("style","background-color:yellow");
// Even better add a class here so that you have more control from css
// GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
您可以从GridView1\u rowdabund()移动注释行。
:
编辑:
这是我的标记,WebForm3.aspx:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="GridViewTest.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace GridViewTest
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
var lstItems = new List<ListItem>()
{
new ListItem {Text ="Items 1", Value ="1"},
new ListItem {Text ="Items 2", Value ="2"},
new ListItem {Text ="Items 3", Value ="3"},
new ListItem {Text ="Items 4", Value ="4"}
};
GridView1.DataSource = lstItems;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
//e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex);
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();
GridView1.Rows[index].Attributes.Add("style", "background-color:yellow");
GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
}
}
.您必须在从row onclick回发后添加突出显示,如下所示:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();//Your method to set datasource anddatabind GridView1
GridView1.Rows[index].Attributes.Add("style","background-color:yellow");
// Even better add a class here so that you have more control from css
// GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
您可以从GridView1\u rowdabund()移动注释行。
:
编辑:
这是我的标记,WebForm3.aspx:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="GridViewTest.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace GridViewTest
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
var lstItems = new List<ListItem>()
{
new ListItem {Text ="Items 1", Value ="1"},
new ListItem {Text ="Items 2", Value ="2"},
new ListItem {Text ="Items 3", Value ="3"},
new ListItem {Text ="Items 4", Value ="4"}
};
GridView1.DataSource = lstItems;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
//e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex);
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();
GridView1.Rows[index].Attributes.Add("style", "background-color:yellow");
GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
}
}
.您必须在从row onclick回发后添加突出显示,如下所示:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();//Your method to set datasource anddatabind GridView1
GridView1.Rows[index].Attributes.Add("style","background-color:yellow");
// Even better add a class here so that you have more control from css
// GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
您可以从GridView1\u rowdabund()移动注释行。
:
编辑:
这是我的标记,WebForm3.aspx:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="GridViewTest.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace GridViewTest
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
var lstItems = new List<ListItem>()
{
new ListItem {Text ="Items 1", Value ="1"},
new ListItem {Text ="Items 2", Value ="2"},
new ListItem {Text ="Items 3", Value ="3"},
new ListItem {Text ="Items 4", Value ="4"}
};
GridView1.DataSource = lstItems;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
//e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex);
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();
GridView1.Rows[index].Attributes.Add("style", "background-color:yellow");
GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
}
}
.您必须在从row onclick回发后添加突出显示,如下所示:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();//Your method to set datasource anddatabind GridView1
GridView1.Rows[index].Attributes.Add("style","background-color:yellow");
// Even better add a class here so that you have more control from css
// GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
您可以从GridView1\u rowdabund()移动注释行。
:
编辑:
这是我的标记,WebForm3.aspx:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="GridViewTest.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace GridViewTest
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
var lstItems = new List<ListItem>()
{
new ListItem {Text ="Items 1", Value ="1"},
new ListItem {Text ="Items 2", Value ="2"},
new ListItem {Text ="Items 3", Value ="3"},
new ListItem {Text ="Items 4", Value ="4"}
};
GridView1.DataSource = lstItems;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
//e.Row.Attributes["onclick"] = string.Format("RowSelect({0});", e.Row.RowIndex);
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
BindData();
GridView1.Rows[index].Attributes.Add("style", "background-color:yellow");
GridView1.Rows[index].Attributes.Add("class", "mycustomclass");
}
}
}
.谢谢@afzalugh的回复,不幸的是我仍然无法让它工作。看起来GridView_RowCommand事件在回发之前触发。我将MessageBox设置为弹出,以便查看调试事件的顺序。它看起来像是GridView_row命令激发、更改颜色。但是页面会重新加载并丢失格式。我希望您没有重新绑定网格。在我的代码中,属性设置在'BindData()'之后,而'BindData'应该具有GridView1.DataBind()代码>。我可以发布我使用过的测试项目。我开始认为它与我的site.css文件中的某些内容有关。我实现了一段从另一个项目中找到的代码,允许我在移动行时高亮显示行,并格式化gridview。这会影响我的输出吗?我发现了我的问题,这是一段从site.css文件格式化gridview的代码。注释掉一部分会使该行高亮显示。谢谢你的帮助@afzalulhThanks@afzalulh的回应,不幸的是,我仍然无法让它工作。看起来GridView_RowCommand事件在回发之前触发。我将MessageBox设置为弹出,以便查看调试事件的顺序。它看起来像是GridView_row命令激发、更改颜色。但是页面会重新加载并丢失格式。我希望您没有重新绑定网格。在我的代码中,属性设置在'BindData()'之后,而'BindData'应该具有GridView1.DataBind()代码>。我可以发布我使用过的测试项目。我开始认为它与我的site.css文件中的某些内容有关。我实现了一段从另一个项目中找到的代码,允许我在移动行时高亮显示行,并格式化gridview。这会影响我的输出吗?我发现了我的问题,这是一段从site.css文件格式化gridview的代码。注释掉一部分会使该行高亮显示。谢谢你的帮助@afzalulhThanks@afzalulh的回应,不幸的是,我仍然无法让它工作。看起来GridView_RowCommand事件在回发之前触发。我将MessageBox设置为弹出,以便查看调试事件的顺序。它看起来像是GridView_row命令激发、更改颜色。但是页面会重新加载并丢失格式。我希望您没有重新绑定网格。在我的代码中,属性设置在'BindData()'之后,而'BindData'应该具有GridView1.DataBind()代码>。我可以发布我使用过的测试项目。我开始认为它与我的site.css文件中的某些内容有关。我实现了一段从另一个项目中找到的代码,允许我在移动行时高亮显示行,并格式化gridview。这会影响我的输出吗?我发现了我的问题,这是一段从site.css文件格式化gridview的代码。注释掉一部分会使该行高亮显示。谢谢你的帮助@afzalulhThanks@afzalulh的回应,不幸的是,我仍然无法让它工作。看起来GridView_RowCommand事件在回发之前触发。我将MessageBox设置为弹出,以便查看调试事件的顺序。它看起来像是GridView_row命令激发、更改颜色。但是页面会重新加载并丢失格式。我希望您没有重新绑定网格。在我的代码中,属性设置在'BindData()'之后,而'BindData'应该具有GridView1.DataBind()代码>。我可以发布我使用过的测试项目。我开始认为它与我的site.css文件中的某些内容有关。我实现了一段从另一个项目中找到的代码,允许我在移动行时高亮显示行,并格式化gridview。这会影响我的输出吗?我发现了我的问题,这是一段从site.css文件格式化gridview的代码。注释掉一部分会使该行高亮显示。谢谢你的帮助@阿夫扎勒赫