Bagikan melalui


IStyleSheet Antarmuka

Definisi

Menentukan metode yang harus diterapkan kelas untuk mendukung pembuatan aturan gaya.

public interface class IStyleSheet
public interface IStyleSheet
type IStyleSheet = interface
Public Interface IStyleSheet

Contoh

Contoh kode berikut menggunakan Header implementasi IStyleSheet untuk menunjukkan secara terprogram membuat aturan gaya baru dan mendaftarkan objek kustom Style .

Di bagian pertama contoh, objek kustom Style , , labelStyledibuat lalu didaftarkan untuk lokasi (URL) saat ini. label1 Kemudian label memanggil MergeStyle metode sehingga labelStyle gaya diterapkan ke label1 label.

Bagian kedua dari contoh menentukan objek kustom Style lain, bodyStyle, dan mengatur propertinya untuk membuat aturan gaya baru.

Catatan

Kelas ini ditujukan terutama untuk pengembang yang ingin membuat implementasi kustom. Contoh ini menunjukkan implementasi yang disediakan oleh .NET Framework.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="istylesheetcs.aspx.cs" Inherits="istylesheetcs" %>

<!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" >
<head id="head1" runat="server">
    <title>IStyleSheet Example</title>
</head>    
<body>
    <form id="form1" runat="server">
        <h1>IStyleSheet Example</h1>
        <asp:Label 
          id="label1" 
          runat="server">
        </asp:Label>
        <br /><br />
        <asp:Label 
          id="label2" 
          runat="server">
        </asp:Label>
    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="istylesheetvb.aspx.vb" Inherits="istylesheetvb" %>

<!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" >
<head id="head1" runat="server">
    <title>IStyleSheet Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <h1>IStyleSheet Example</h1>
        <asp:Label 
          id="label1" 
          runat="server">
        </asp:Label>
        <br /><br />
        <asp:Label 
          id="label2" 
          runat="server">
        </asp:Label>
    </form>
  </body>
</html>

Berikut ini adalah file code-behind untuk halaman Web dalam contoh sebelumnya.

public partial class istylesheetcs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a Style object to hold style rules to apply to a Label control.
        Style labelStyle = new Style();

        labelStyle.ForeColor = System.Drawing.Color.DarkRed;
        labelStyle.BorderColor = System.Drawing.Color.DarkBlue;
        labelStyle.BorderWidth = 2;

        // Register the Style object so that it can be merged with 
        // the Style object of the controls that use it.
        Page.Header.StyleSheet.RegisterStyle(labelStyle, null);

        // Merge the labelCssStyle style with the label1 control's
        // style settings.
        label1.MergeStyle(labelStyle);
        label1.Text = "This is what the labelCssStyle looks like.";

        // Create a Style object for the <BODY> section of the Web page.
        Style bodyStyle = new Style();

        bodyStyle.ForeColor = System.Drawing.Color.Blue;
        bodyStyle.BackColor = System.Drawing.Color.LightGray;

        // Add the style to the header of the current page.
        Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "BODY");

        // Add text to the label2 control to see the label without 
        // the labelStyle applied to it.  
        label2.Text = "This is what the bodyStyle looks like.";
    }
}

Keterangan

Kelas yang mengimplementasikan antarmuka ini dapat menggunakannya untuk mendukung pembuatan aturan gaya.

Untuk menyesuaikan cara lembar gaya kaskade dibuat dan didaftarkan, Anda harus membuat kelas yang mengimplementasikan antarmuka ini.

Kelas HtmlHead mengimplementasikan antarmuka ini untuk digunakan oleh ASP.NET melalui Header properti .

Catatan

Menambahkan atau memodifikasi gaya atau aturan gaya secara terprogram selama postback asinkron tidak didukung. Saat Anda menambahkan kapabilitas AJAX ke halaman Web ASP.NET, postback asinkron memperbarui wilayah halaman tanpa memperbarui seluruh halaman. Untuk informasi selengkapnya, lihat Gambaran Umum Microsoft Ajax.

Metode

CreateStyleRule(Style, IUrlResolutionService, String)

Saat diimplementasikan oleh kelas, membuat aturan gaya untuk jenis elemen bahasa dokumen yang ditentukan, atau pemilih.

RegisterStyle(Style, IUrlResolutionService)

Saat diimplementasikan oleh kelas, menambahkan aturan gaya baru ke lembar gaya yang disematkan di <head> bagian halaman Web.

Berlaku untuk

Lihat juga