【问题标题】:How to change background colour of a website when a button is clicked in ASP.NET C#在 ASP.NET C# 中单击按钮时如何更改网站的背景颜色
【发布时间】:2017-11-06 02:13:57
【问题描述】:

我试图弄清楚当用户单击 ASP.NET 服务器按钮时,它会更改背景颜色。有没有一种方法可以在代码隐藏文件中使用没有 javascript 而没有 body 标签和标签的 c# 来做到这一点?我的下一个问题是如何将它保存在 cookie 中,这样当用户重新访问该网站时,他们的配色方案会被保留。

【问题讨论】:

  • 添加代码 sn-p 以展示您的作品。没有人会为你从头开始编写代码。
  • 另见:I downvoted because no MCVE provided。尝试通过编辑问题正文而不是使用评论来提供示例代码。
  • 我要重新发布这个问题

标签: c# html css asp.net


【解决方案1】:

试试这个代码

在 C# 中

protected void button1_Click(object sender, EventArgs e)
{
   MyBody.Style[HtmlTextWriterStyle.BackgroundColor] = "red";    
}

在 HTML 中

<html>
<head><title>Change Background color</title></head>
<body id="MyBody"runat="server">
<h3>The background color of this page has been set programmatically.</h3>
<asp:Button id="Button1"Text="Click here for greeting..." OnClick="button1_Click"runat="server" />
</body>
</html>

【讨论】:

    【解决方案2】:

    你好@Serin Loly,这次我会这样做,但请从下次开始向我们展示你的努力。继续编码

    default.aspx

    <body id="bdy" runat="server">
    <form runat="server">
        <asp:Button OnClick="eventclick" runat="server" Text="click" /> 
    </form>
    </body>
    

    cs 代码

     protected void eventclick (object sender, EventArgs e)
        {
    
            bdy.Attributes.Add("style", "background:teal");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-09
      • 2017-08-02
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2014-10-10
      • 2020-09-13
      相关资源
      最近更新 更多