日期:2011-10-03  浏览次数:20570 次

方法一:

.aspx代码

<form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 112px"
     runat="server"></asp:TextBox>
    <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 144px" runat="server"
     Text="设 置"></asp:Button>
    <asp:Label id="Label2" style="Z-INDEX: 103; LEFT: 256px; POSITION: absolute; TOP: 80px" runat="server">动态设置TextBox的样式</asp:Label>
    <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 184px" runat="server"></asp:Label></FONT>
  </form>

.aspx.cs代码

private void Button1_Click(object sender, System.EventArgs e)
  {//设置样式
   TextBox1.Style.Add("width",TextBox1.Text.ToString());
   Label1.Text="宽度"+TextBox1.Style["width"].ToString();
  }

方法二:

.aspx代码

<form id="Form1" method="post" runat="server">
   <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 48px" runat="server">动态设置服务器控件的样式</asp:Label>
   <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 264px; POSITION: absolute; TOP: 112px" runat="server"
    Text="设 置"></asp:Button>
   <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 264px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
  </form>

.aspx.cs代码

private void Button1_Click(object sender, System.EventArgs e)
  {//设置样式
   Style style=new Style();
   style.BorderColor=Color.Black;
   style.BorderStyle=BorderStyle.Inset;
   style.BorderWidth=new Unit(1);
   TextBox1.ApplyStyle(style);
  }