共用方式為


WebControl.MergeStyle(Style) 方法

定義

將指定樣式的任何非空白項目複製到 Web 控制項,但不覆寫控制項的任何現有樣式項目。 這個方法主要由控制項開發人員使用。

public:
 void MergeStyle(System::Web::UI::WebControls::Style ^ s);
public void MergeStyle (System.Web.UI.WebControls.Style s);
member this.MergeStyle : System.Web.UI.WebControls.Style -> unit
Public Sub MergeStyle (s As Style)

參數

s
Style

Style,表示要複製的樣式。

範例

下列範例示範如何使用 MergeStyle 方法,將樣式與控件的 DataGrid 樣式合併。

注意

下列程式代碼範例會使用單一檔案程式代碼模型,如果直接複製到程式代碼後置檔案,可能無法正常運作。 此程式代碼範例必須複製到擴展名為 .aspx的空文本檔。 如需 Web Forms 程式代碼模型的詳細資訊,請參閱 ASP.NET 網頁代碼模型


<%@ 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;
      
         ItemsGrid.MergeStyle(myStyle);
      }
 
   </script>
 
<head runat="server">
    <title>WebControl MergeStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>WebControl MergeStyle Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           BackColor="Yellow"
           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="Merge 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
      
         ItemsGrid.MergeStyle(myStyle)

      End Sub
 
   </script>
 
<head runat="server">
    <title>WebControl MergeStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>WebControl MergeStyle Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           BackColor="Yellow"
           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="Merge Custom Style"
           OnClick="Button_Click"
           runat="server"/>
 
   </form>
 
</body>
</html>

適用於

另請參閱