WebControl.ApplyStyle(Style) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定样式的所有非空白元素复制到 Web 控件,覆盖控件的所有现有的样式元素。 此方法主要由控件开发人员使用。
public:
void ApplyStyle(System::Web::UI::WebControls::Style ^ s);
public void ApplyStyle (System.Web.UI.WebControls.Style s);
member this.ApplyStyle : System.Web.UI.WebControls.Style -> unit
Public Sub ApplyStyle (s As Style)
参数
示例
下面的示例演示如何使用 ApplyStyle 方法将样式应用于 DataGrid 控件。
注意
下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 此代码示例必须复制到扩展名为 .aspx 的空文本文件中。 有关 Web 窗体代码模型的详细信息,请参阅 ASP.NET Web 窗体页代码模型。
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Load this data only once.
ItemsGrid.DataSource= CreateDataSource();
ItemsGrid.DataBind();
}
}
void Button_Click(Object sender, EventArgs e)
{
Style myStyle = new Style();
myStyle.ForeColor = System.Drawing.Color.Red;
myStyle.BackColor = System.Drawing.Color.Yellow;
ItemsGrid.ApplyStyle(myStyle);
}
</script>
<head runat="server">
<title>WebControl ApplyStyle Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>WebControl ApplyStyle Example</h3>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<Columns>
<asp:BoundColumn
HeaderText="Number"
DataField="IntegerValue">
</asp:BoundColumn>
<asp:BoundColumn
HeaderText="Description"
DataField="StringValue">
</asp:BoundColumn>
<asp:BoundColumn
HeaderText="Price"
DataField="CurrencyValue"
DataFormatString="{0:c}">
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<br /><br />
<asp:Button id="Button1"
Text="Apply Custom Style"
OnClick="Button_Click"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
Private Function CreateDataSource() As ICollection
Dim dt As DataTable = New DataTable
Dim dr As DataRow
Dim dv As DataView
Dim i As Integer
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
for i = 0 to 9
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dt.Rows.Add(dr)
next i
dv = New DataView(dt)
CreateDataSource = dv
End Function
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
' Load this data only once.
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim myStyle As Style = new Style()
myStyle.ForeColor = System.Drawing.Color.Red
myStyle.BackColor = System.Drawing.Color.Yellow
ItemsGrid.ApplyStyle(myStyle)
End Sub
</script>
<head runat="server">
<title>WebControl ApplyStyle Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>WebControl ApplyStyle Example</h3>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<Columns>
<asp:BoundColumn
HeaderText="Number"
DataField="IntegerValue">
</asp:BoundColumn>
<asp:BoundColumn
HeaderText="Description"
DataField="StringValue">
</asp:BoundColumn>
<asp:BoundColumn
HeaderText="Price"
DataField="CurrencyValue"
DataFormatString="{0:c}">
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<br /><br />
<asp:Button id="Button1"
Text="Apply Custom Style"
OnClick="Button_Click"
runat="server"/>
</form>
</body>
</html>