[英]Changing color of selected row in asp.net gridview
嗨我想改變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;
}
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;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.SelectedRow.BackColor = System.Drawing.Color.White;
}
嘗試類似下面的東西。
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'");
}
}
我正在尋找這個問題的解決方案,然后看到這樣的幾個線程提到你需要跟蹤會話中的選定行。 雖然這可能適用於完全回發,當它部分時,我找到了一個更好的解決方案,使用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;
}
1)您需要做的第一件事是處理Grid的RowCommand事件,protected void GridView_RowCommand(object sender,GridViewCommandEventArgs e)。 你正在做的是將按鈕的click事件與gridview的命令事件分開處理。 2)您可以為命令參數指定索引,並更改行的顏色並將所有其他行設置為默認顏色。 這里可以找到一個例子
希望這可以幫助,
使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.