Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Wrox Professional Web Parts and Custom Controls with ASP.NET 2.0 phần 4 pptx
PREMIUM
Số trang
45
Kích thước
980.4 KB
Định dạng
PDF
Lượt xem
760

Wrox Professional Web Parts and Custom Controls with ASP.NET 2.0 phần 4 pptx

Nội dung xem thử

Mô tả chi tiết

Dim txt1 As System.Web.UI.WebControls.TextBox

Dim txt2 As System.Web.UI.WebControls.TextBox

Dim stl As New System.Web.UI.WebControls.Style

stl = New System.Web.UI.WebControls.Style

stl.ForeColor = Drawing.Color.Red

stl.Font.Bold = True

stl.BorderWidth = 12

txt1 = New System.Web.UI.WebControls.TextBox

txt1.Id = “Text1”

txt1.ApplyStyle(stl)

txt1.Text = “Hello”

Me.Controls.Add(txt1)

txt2 = New System.Web.UI.WebControls.TextBox

txt2.Id = “Text2”

txt2.Text = “World”

txt2.ApplyStyle(stl)

Me.Controls.Add(txt2)

In C#:

System.Web.UI.WebControls.TextBox txt1;

System.Web.UI.WebControls.TextBox txt2;

System.Web.UI.WebControls.Style stl;

stl = new System.Web.UI.WebControls.Style();

stl.ForeColor = Drawing.Color.Red;

stl.Font.Bold = true;

stl.BorderWidth = 12;

txt1 = new System.Web.UI.WebControls.TextBox();

txt1.Id = “Text1”;

txt1.Text = “Hello”;

txt1.ApplyStyle(stl);

this.Controls.Add(txt1);

txt2 = new System.Web.UI.WebControls.TextBox();

txt2.Id = “Text2”;

txt2.Text = “World”;

txt2.ApplyStyle(stl);

this.Controls.Add(txt2);

The (very unattractive) results can be seen in Figure 3-10.

If style values have already been set on a control, the MergeStyle method integrates the Style object’s

properties with the already existing style settings. Settings in the Style object will not override the style

settings already on the control. This makes the MergeStyle method a very useful tool for integrating

style settings made by the developer with style settings that you want to maintain as part of your custom

control’s design.

108

Chapter 3

08_57860x ch03.qxd 10/4/05 9:20 PM Page 108

Figure 3-10

This Visual Basic 2005 code retrieves the control’s Style object using the ControlStyle method. The code

then sets the BorderStyle property of the Style object to Inset and sets the ForeColor property on a

TextBox and a Button object. Finally, the code uses the Style object with the constituent controls through

the MergeStyle method to apply the style to the text box without overriding their individual ForeColor

settings. Only controls that don’t have an explicit ForeColor setting will pick up the new ForeColor:

Dim txt As System.Web.UI.WebControls.TextBox

Dim btn As System.Web.UI.WebControls.Button

Dim stl As System.Web.UI.WebControls.Style

stl = Me.ControlStyle()

stl.BorderStyle = System.Web.UI.WebControls.BorderStyle.Inset

txt = New System.Web.UI.WebControls.TextBox

txt.ID = “Text1”

txt.ForeColor = Drawing.Color.Red

txt.MergeStyle(stl)

Me.Controls.Add(txt)

btn = New System.Web.UI.WebControls.Button

btn.ForeColor = Drawing.Color.Green

btn.ID = “Button1”

btn.MergeStyle(stl)

Me.Controls.Add(btn)

In C#

System.Web.UI.WebControls.TextBox txt;

System.Web.UI.WebControls.Button btn;

System.Web.UI.WebControls.Style stl;

txt = new System.Web.UI.WebControls.TextBox();

btn = new System.Web.UI.WebControls.Button();

stl = this.ControlStyle;

109

Creating Custom Controls

08_57860x ch03.qxd 10/4/05 9:20 PM Page 109

stl.BorderStyle = System.Web.UI.WebControls.BorderStyle.Inset;

txt.ID = “Text1”;

txt.ForeColor = Drawing.Color.Red;

txt.MergeStyle(stl);

this.Controls.Add(txt);

btn.ID = “Button1”;

btn.ForeColor = Drawing.Color.Green;

btn.MergeStyle(stl);

this.Controls.Add(btn);

Recycling Styles

You can retrieve a Style object from an existing control by accessing the control’s ControlStyle property.

This Visual Basic 2005 code extracts a Style object from an existing text box and then uses it with another

constituent control:

Dim stl As System.Web.UI.WebControls.Style

Dim txtNew As New System.Web.UI.WebControls.TextBox

stl = New System.Web.UI.WebControls.Style

stl = txtName.ControlStyle

txtNew.MergeStyle(stl)

In C#:

System.Web.UI.WebControls.Style stl;

System.Web.UI.WebControls.TextBox txtNew = new System.Web.UI.WebControls.TextBox();

stl = System.Web.UI.WebControls.Style();

stl = txtName.ControlStyle;

txtNew.MergeStyle(stl);

You can extract the style for your custom control using the WebControl object’s ControlStyle property.

By retrieving the Style object from the ControlStyle property, you can access the Style object created in

the CreateControlStyle routine from other routines without having to declare your Style object at the

class level. If the Style object for your control has not yet been created, reading the ControlStyle property

causes your CreateControlStyle method to run.

This Visual Basic 2005 code retrieves the Style object from the custom control and then uses it with two

constituent controls:

Dim txt As System.Web.UI.WebControls.TextBox

Dim btn As System.Web.UI.WebControls.Button

Dim stl As System.Web.UI.WebControls.Style

stl = Me.ControlStyle()

txt = New System.Web.UI.WebControls.TextBox

txt.Id = “Text1”

txt.MergeStyle(stl)

110

Chapter 3

08_57860x ch03.qxd 10/4/05 9:20 PM Page 110

Me.Controls.Add(txt)

btn = New System.Web.UI.WebControls.Button

btn.Id = “Button1”

btn.MergeStyle(stl)

Me.Controls.Add(btn)

In C#:

System.Web.UI.WebControls.TextBox txt;

System.Web.UI.WebControls.Button btn;

System.Web.UI.WebControls.Style stl;

stl = this.ControlStyle();

txt = new System.Web.UI.WebControls.TextBox();

txt.Id = “Text1”;

txt.MergeStyle(stl);

this.Controls.Add(txt);

btn = new System.Web.UI.WebControls.Button();

btn.Id = “Button2”;

btn.MergeStyle(stl);

this.Controls.Add(btn);

If you extract the Style object from the ControlStyle property of your custom control, any changes that you

make to the Style object’s properties will be reflected in the HTML for your custom control, provided they

are made before your control’s Render method is called.

If you do override the default Render method with your own version, remember that you suppress

any default processing that occurs. For instance, overriding the Render method prevents the default

RenderBeginTag method from executing, and this is where the information from the Style object is added

to your control’s HTML.

You can ensure that the default rendering does take place by calling the base Render* methods from

your version of the Render method and passing those methods the HTMLTextWriter object passed to

the Render method. This Visual Basic 2005 code modifies the Style object drawn from the ControlStyle

method and ensures that the Style information is added to the resulting HTML by calling the default

RenderBeginTag and RenderEndTag methods through the MyBase object:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

Dim stl As System.Web.UI.WebControls.Style

stl = Me.ControlStyle

stl.ForeColor = Drawing.Color.Red

MyBase.RenderBeginTag(writer)

writer.Write(strText)

MyBase.RenderEndTag(writer)

End Sub

111

Creating Custom Controls

08_57860x ch03.qxd 10/4/05 9:20 PM Page 111

Tải ngay đi em, còn do dự, trời tối mất!