更改选定行的颜色在asp.net的GridView [英] Changing color of selected row in asp.net gridview

查看:98
本文介绍了更改选定行的颜色在asp.net的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试图我已经定义是这样我网格视图更改所选行的颜色在asp.net gridview的

 < ASP:GridView控件ID =gridContractor=服务器AllowPaging =真AllowSorting =真
                的AutoGenerateColumns =FALSE的CssClass =GridViewStyle网格=无EnableModelValidation =真
                的DataKeyNames =的DeviceIDOnRowCommand =gridContractor_RowCommandOnPageIndexChanging =gridContractor_PageIndexChanging
                WIDTH =100%EmptyDataText =没有记录显示EmptyDataRowStyle-Horizo​​ntalAlign =中心>
                < AlternatingRowStyle背景色=白/>
                <柱体和GT;
                    < ASP:BoundField的的HeaderText =设备IMEI数据字段=的DeviceID可见=假>
                        < HeaderStyle Horizo​​ntalAlign =左VerticalAlign =中/>
                        < ItemStyle Horizo​​ntalAlign =左VerticalAlign =中东WIDTH =175/>
                    < / ASP:BoundField的>
                    < ASP:BoundField的的HeaderText =人名的DataField =PersonName的>
                        < HeaderStyle Horizo​​ntalAlign =左VerticalAlign =中/>
                        < ItemStyle Horizo​​ntalAlign =左VerticalAlign =中/>
                    < / ASP:BoundField的>
                    < ASP:BoundField的的HeaderText =#观察数据字段=GpsPointsCountControlStyle-WIDTH =50像素>
                        < HeaderStyle Horizo​​ntalAlign =右VerticalAlign =中/>
                        < ItemStyle Horizo​​ntalAlign =右VerticalAlign =中东WIDTH =50/>
                    < / ASP:BoundField的>
                    < ASP:BoundField的的HeaderText =#违反数据字段=ViolationCountControlStyle-WIDTH =60像素>
                        < HeaderStyle Horizo​​ntalAlign =右VerticalAlign =中/>
                        < ItemStyle Horizo​​ntalAlign =右VerticalAlign =中东WIDTH =60/>
                    < / ASP:BoundField的>
                    < ASP:的TemplateField的HeaderText =ItemStyle-Horizo​​ntalAlign =中心HeaderStyle宽度=50>
                        <&ItemTemplate中GT;
                            < ASP:按钮的ID =btnEdit=服务器文本=查看的CommandName =查看启用=真的OnClick =btn_GrdClick
                                CommandArgument =<%#绑定('的DeviceID')%GT; />
                        < / ItemTemplate中>
                        < HeaderStyle宽度=50/>
                        < ItemStyle Horizo​​ntalAlign =中心>< / ItemStyle>
                    < / ASP:的TemplateField>
                < /专栏>
                < RowStyle的CssClass =RowStyle/>
                < EmptyDataRowStyle的CssClass =EmptyRowStyle/>
                < PagerStyle的CssClass =PagerStyle/>
                < SelectedRowStyle背景色=艾莉斯蓝/>
                < HeaderStyle的CssClass =HeaderStyle/>
                < EditRowStyle的CssClass =EditRowStyle/>
                < AlternatingRowStyle的CssClass =AltRowStyle/>
            < / ASP:GridView的>

和我已经处理按钮点击事件的问题是,每一次我选择行的previous行的颜色是没有得到改变,即使我这样做。一旦点击该行仍黄色能够有人帮助我,请

我aspx.cs code

 保护无效btn_GrdClick(对象发件人,EventArgs的发送)
    {
        GridViewRow previousRow =会话[previousRow]作为GridViewRow;
        如果(previousRow!= NULL)
            previousRow.ForeColor = Color.White;
        GridViewRow行=(GridViewRow)((按钮)发送方).NamingContainer;
        row.ForeColor = Color.Yellow;
        会话[previousRow] =排;
    }


解决方案

GridViewRow 是一个控制。喜欢的页面的每一个对象,它会在页面生命周期过程中创建的。结果
您在会话持有提到的是最后一个请求中创建的对象。

要解决这个问题,只保留该行的索引(或键)在会话,并用它来改变previous行的颜色。

 保护无效btn_GrdClick(对象发件人,EventArgs的发送)
{
    如果(会话[previousRowIndex]!= NULL)
    {
      VAR previousRowIndex =(int)的会议[previousRowIndex];
      GridViewRow previousRow = MyGridView.Rows [previousRowIndex]。
      previousRow.ForeColor = Color.White;
    }    GridViewRow行=(GridViewRow)((按钮)发送方).NamingContainer;
    row.ForeColor = Color.Yellow;
    会话[previousRowIndex] = row.RowIndex;
}

Hi i am trying to change the color of the selected row in the asp.net gridview i have defined my grid view like this

<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>

and i have handled the button click event the problem is that each time i am selecting a row the previous row color is not getting changed even though i am doing it. once clicked the row remains yellow can somebody help me please

my aspx.cs code

 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 is a control. Like every object on the page, it will be created during the page life cycle.
The reference you hold in Session is to the object created during the last request.

To solve the problem, keep only the index(or key) of the row in Session and use that to change the color of previous row.

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

这篇关于更改选定行的颜色在asp.net的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆