Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 分别为每行更改gridview中控件的css_C#_Asp.net_Css_Gridview - Fatal编程技术网

C# 分别为每行更改gridview中控件的css

C# 分别为每行更改gridview中控件的css,c#,asp.net,css,gridview,C#,Asp.net,Css,Gridview,我在gridview的templatefield中有四个按钮。 我需要改变css的点击按钮在每一行分别。 例如,在第一行中,单击第一个按钮。那么它必须是黄色的。在第3行中,单击第二个按钮。然后它必须是黄色的等等。 这是我的网格视图 <asp:GridView OnRowCommand="SelectedPollGridView_RowCommand" ID="SelectedPollGridView" runat="server" AutoGenerateColumns="False" D

我在gridview的templatefield中有四个按钮。 我需要改变css的点击按钮在每一行分别。 例如,在第一行中,单击第一个按钮。那么它必须是黄色的。在第3行中,单击第二个按钮。然后它必须是黄色的等等。 这是我的网格视图

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

我建议大家阅读一下如何在javascript中做到这一点,因为对一个元素的css进行简单的更改应该非常容易。但是,如果需要在服务器上执行此操作,则应该能够执行以下操作:

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

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

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

与其他按钮类似。

您只需使用以下代码:

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

非常感谢你。我只是想,如果我这样做,它会影响所有行:)我需要在服务器端这样做,因为它在更改css后在数据库中执行更新命令:)再次感谢:)非常感谢。这两个答案是一样的。再次感谢你,我的朋友:)
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";
}