简体   繁体   English

在asp.net中以编程方式访问HTML表单?

[英]Accessing a forms html programatically in asp.net?

If I have a form called index.aspx and I want to set the background colour programatically how do I do such a thing? 如果我有一个名为index.aspx的表单,并且想以编程方式设置背景色,我该怎么做? Like if I wanted to set the pages background by calling a method called Changebackground? 就像我想通过调用一个名为Changebackground的方法来设置页面背景吗?

Sub ChangeBackground()
       ' change forms html here 
    End Sub

You can first make the "body" element a server control, assign it an ID, then set CSS style to the body element's "style" attribute, for example: 您可以首先将“ body”元素设置为服务器控件,为其分配一个ID,然后将CSS样式设置为body元素的“ style”属性,例如:

ASPX (Note that "body" element is now a server control): ASPX (请注意,“ body”元素现在是服务器控件):

<body runat="server" id="body">
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>

ASPX.CS: ASPX.CS:

protected void Page_Load(object sender, EventArgs e)
{
     body.Attributes["style"] = "background-color:red";
}

This code will set the page background to red. 此代码会将页面背景设置为红色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM