c# - 为每一行分别更改 gridview 中控件的 css

标签 c# asp.net css gridview

我的 gridview 模板字段中有四个按钮。 我需要分别更改每行中单击按钮的 css。 例如在第一行,第一个按钮被点击。那么它必须是黄色的。在第 3 行中,单击了第二个按钮。那么它必须是黄色的等等。 这是我的 GridView

<asp:GridView OnRowCommand="SelectedPollGridView_RowCommand" ID="SelectedPollGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="PollID" DataSourceID="SelectedPollSqlDataSource">
    <Columns>
        <asp:TemplateField>            
            <ItemTemplate>
                <asp:Label Visible="false" ID="PollIDLabel" runat="server" Text='<%#Eval("PollID") %>'></asp:Label>

                <div runat="server" id="MainDiv" class="text-right">
                    <div runat="server" id="O1Div" visible='<%#Eval("O1Vis") %>' class="radio ">
                        <asp:Button CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="O1" ID="O1Button" runat="server" Text=" 1" />
                    </div>
                    <div runat="server" id="O2Div" visible='<%#Eval("O2Vis") %>' class="radio">
                        <asp:Button CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="O2" ID="O2Button" runat="server" Text=" 2" />
                    </div>
                    <div runat="server" id="O3Div" visible='<%#Eval("O3Vis") %>' class="radio">
                        <asp:Button CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="O3" ID="O3Button" runat="server" Text=" 3" />
                    </div>
                    <div runat="server" id="O4Div" visible='<%#Eval("O4Vis") %>' class="radio">
                        <asp:Button CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="O4" ID="O4Button" runat="server" Text=" 4" />
                    </div>
                </div>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>    
</asp:GridView>

下面是代码:

protected void SelectedPollGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{        
    int index = Convert.ToInt32(e.CommandArgument); 
    GridViewRow row = SelectedPollGridView.Rows[index];

    Label myPollIDLAbel = (Label)row.FindControl("PollIDLabel");

    if (e.CommandName == "O1")
    {
        //chnaging the css of O1 button JUST IN THIS ROW

    }
    else if (e.CommandName == "O2")
    {
        //chnaging the css of O2 button JUST IN THIS ROW
    }
    else if (e.CommandName == "O3")
    {
        //chnaging the css of O3 button JUST IN THIS ROW
    }
    else if (e.CommandName == "O4")
    {
        //chnaging the css of O4 button JUST IN THIS ROW
    }     
}

最佳答案

您只需使用以下代码:

if (e.CommandName == "O1")
{
  Button O1Button = (Button)row.FindControl("O1Button");

  //Change the background-color:
  O1Button.Style.Add("background-color", "yellow");

  //Change the class
  O1Button.CssClass = "class-name";
}
else if (e.CommandName == "O2")
{
    //chnaging the css of O2 button JUST IN THIS ROW
    Button O2Button = (Button)row.FindControl("O2Button");

  //Change the background-color:
  O2Button.Style.Add("background-color", "yellow");

  //Change the class
  O2Button.CssClass = "class-name";
}
else if (e.CommandName == "O3")
{
    //chnaging the css of O3 button JUST IN THIS ROW
    Button O3Button = (Button)row.FindControl("O3Button ");

  //Change the background-color:
  O3Button.Style.Add("background-color", "yellow");

  //Change the class
  O3Button.CssClass = "class-name";
}
else if (e.CommandName == "O4")
{
    //chnaging the css of O4 button JUST IN THIS ROW
    Button O4Button= (Button)row.FindControl("O4Button");

  //Change the background-color:
  O4Button.Style.Add("background-color", "yellow");

  //Change the class
  O4Button.CssClass = "class-name";
}     

关于c# - 为每一行分别更改 gridview 中控件的 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20326981/

相关文章:

c# - 在表达式中将派生类转换为父类

c# - 使用包含 & 符号的 URL 将 XML 加载到 XDocument

c# - UWP 设备总内存

c# - 如何在 Xamarin 应用程序和 Java Android 应用程序之间共享 sqlite 数据库

html - 溢出滚动条颜色

html - CSS 背景大小和 div 大小

html - CSS 中的文本样式

asp.net - 如何在不刷新父窗口的情况下关闭radwindow

c# - 无法使用 FormsAuthentication.SignOut() 从 ASP.NET MVC 应用程序注销

asp.net - AJAX 请求时 Azure SQL 中的间歇性连接超时