您好我正在尝试更改asp.net gridview中所选行的颜色我已经定义了这样的网格视图
<asp:GridView ID="gridContractor" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None" EnableModelValidation="True"
DataKeyNames="DeviceID" OnRowCommand="gridContractor_RowCommand" OnPageIndexChanging="gridContractor_PageIndexChanging"
Width="100%" EmptyDataText = "No records to display" EmptyDataRowStyle-HorizontalAlign="Center">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="Device IMEI" DataField="DeviceID" Visible="false">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="175" />
</asp:BoundField>
<asp:BoundField HeaderText="Person Name" DataField="PersonName">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField HeaderText="#Observations" DataField="GpsPointsCount" ControlStyle-Width="50px">
<HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="50" />
</asp:BoundField>
<asp:BoundField HeaderText="#Violations" DataField="ViolationCount" ControlStyle-Width="60px">
<HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="60" />
</asp:BoundField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="View" CommandName="View" Enabled="true" OnClick="btn_GrdClick"
CommandArgument="<%#Bind('DeviceID') %>" />
</ItemTemplate>
<HeaderStyle Width="50" />
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle BackColor="AliceBlue" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
我已经处理了按钮点击事件,问题是我每次选择一行时前一行的颜色都没有改变,即使我正在这样做。一旦点击该行仍然是黄色可以有人帮助我
我的aspx.cs代码
protected void btn_GrdClick(object sender, EventArgs e)
{
GridViewRow PreviousRow = Session["PreviousRow"] as GridViewRow;
if (PreviousRow != null)
PreviousRow.ForeColor = Color.White;
GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
row.ForeColor = Color.Yellow;
Session["PreviousRow"] = row;
}
答案 0 :(得分:4)
GridViewRow
是一个控件。与页面上的每个对象一样,它将在页面生命周期中创建
您在Session
中保存的引用是在上一个请求期间创建的对象。
要解决此问题,请仅保留Session
中行的索引(或键),并使用它来更改上一行的颜色。
protected void btn_GrdClick(object sender, EventArgs e)
{
if(Session["PreviousRowIndex"] != null)
{
var previousRowIndex = (int)Session["PreviousRowIndex"];
GridViewRow PreviousRow = MyGridView.Rows[previousRowIndex];
PreviousRow.ForeColor = Color.White;
}
GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
row.ForeColor = Color.Yellow;
Session["PreviousRowIndex"] = row.RowIndex;
}
答案 1 :(得分:2)
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.SelectedRow.BackColor = System.Drawing.Color.White;
}
答案 2 :(得分:1)
尝试下面的内容。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.backgroundColor='yellow'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
}
}
答案 3 :(得分:1)
我正在寻找这个问题的解决方案,然后看到这样的几个线程提到你需要跟踪会话中的选定行。虽然这可能适用于完全回发,当它部分时,我找到了一个更好的解决方案,使用Css。
Gridview在更新面板中,周围的div有一个class = grid,gridview有class = datatable,然后在Gridview中定义了以下元素;
RowStyle CssClass =“item-row”
SelectedRowStyle CssClass =“selectedItem-row”
然后关联的css看起来像这样;
.grid .datatable .item-row TD {
border-bottom: #bbd9ee 1px solid;
text-align: left;
padding-bottom: 6px;
padding-left: 4px;
padding-right: 4px;
font-size: 0.7em;
border-top: #bbd9ee 1px solid;
padding-top: 6px;
}
.grid .datatable .item-row TD.highlightcell {
background-color: #fffacd;
color: #000;
}
.grid .datatable .item-row:hover {
background-color: #fffacd;
color: #000;
}
.grid .datatable .selectedItem-row TD {
border-bottom: #bbd9ee 1px solid;
text-align: left;
padding-bottom: 6px;
padding-left: 4px;
padding-right: 4px;
font-size: 0.7em;
border-top: #bbd9ee 1px solid;
padding-top: 6px;
background-color: #d7ffcd;
}
答案 4 :(得分:0)
1)你需要做的第一件事是处理Grid的RowCommand事件,protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)。你正在做的是将按钮的click事件与gridview的命令事件分开处理。 2)您可以为命令参数指定索引,并更改行的颜色并将所有其他行设置为默认颜色。可以找到一个例子here
希望这有帮助,
答案 5 :(得分:0)
使用GridView加载事件
我已经举例说明了我的代码,
Protected Sub grvaddbook_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles grvaddbook.Load
Dim row1 As GridViewRow
row1 = grvaddbook.HeaderRow
Dim i As Integer
i = 0
For Each row As GridViewRow In grvaddbook.Rows
Dim main As Label = DirectCast(grvaddbook.Rows(i).Cells(0).FindControl("lblismain"), Label)
If main.Text = 1 Then
Dim lbtnmakedefault As LinkButton = DirectCast(grvaddbook.Rows(i).Cells(0).FindControl("lbtnmakedefault"), LinkButton)
lbtnmakedefault.Visible = False
mainaid = DirectCast(grvaddbook.Rows(i).Cells(0).FindControl("lblaid"), Label).Text
End If
i = i + 1
Next
End Sub
答案 6 :(得分:0)
protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "xxx")
{
foreach (GridViewRow rx in gridName.Rows)
{
rx.ForeColor = Color.White;
}
((System.Web.UI.WebControls.TableRow)((System.Web.UI.Control)e.CommandSource).DataItemContainer).ForeColor = Color.Yellow;
}
}
干杯