自定义控件设计

 一.设计样式

例:

法一:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
WebControls.Style stl;
WebControls.TextBox txt;
stl = new System.Web.UI.WebControls.Style();
stl = this.ControlStyle;
stl.ForeColor = Drawing.Color.Red;
stl.AddAttributesToRender(writer);
txt = new WebControls.TextBox();
txt.Text = “hello, world”;
txt.RenderControl(writer);
}

注意:DesignMode 属性

System.Web.UI.WebControls.Control txt;
if (this.DesignMode == true)
{
txt = (System.Web.UI.WebControls.TextBox) this.FindControl(“txtInput”);
txt.Text = “#databound”;
}
else
{
...databinding support
}

二.custom control behavior

属性:DefaultProperty,DefaultEvent,Category,ToolboxItem

例:[DefaultProperty(“Category”), DefaultEvent(“InvalidValue”)]

[Category(“Appearance”)] 

[ToolBoxItem(False), ToolboxData(“<{0}:BookDetail runat=server></{0}:BookDetail>”)]

三.返回:GetPostBackEventReference 添加属性:AddAttributesToRender

 bool AutoPostBackOn;
        public bool AutoPostBack
        {
            get
            {
               return AutoPostBackOn;
            }
            set
            {
               AutoPostBackOn = value;
            }
        } 

protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            if (this.AutoPostBack == true)
            {
               string strPostBackCode;
               strPostBackCode = this.Page.ClientScript.GetPostBackEventReference(this, "MainText");
            }
        }

 例:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CSharpCustomControls
{
    [DefaultProperty("Category"), DefaultEvent("InvalidValue")]
    [ToolboxData("<{0}:SixthSample runat=server></{0}:SixthSample>")]
    public class SixthSample : WebControl
    {
        protected override void CreateChildControls()
        {
            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;
            stl.BorderStyle = System.Web.UI.WebControls.BorderStyle.Inset;

            txt.ID = "Text1";
            txt.ForeColor = System.Drawing.Color.Red;
            txt.MergeStyle(stl);
            this.Controls.Add(txt);

            btn.ID = "Button1";
            btn.ForeColor = System.Drawing.Color.Green;
            btn.MergeStyle(stl);
            this.Controls.Add(btn);

            System.Web.UI.WebControls.Style stl2;
            System.Web.UI.WebControls.TextBox txt3 = new System.Web.UI.WebControls.TextBox();

            stl2 = new System.Web.UI.WebControls.Style();
            stl2 = txt.ControlStyle;
            txt3.MergeStyle(stl);
            this.Controls.Add(txt3);

            System.Web.UI.WebControls.TextBox txt2;
            System.Web.UI.WebControls.Button btn2;
            System.Web.UI.WebControls.Style stl3;

            stl3 = this.ControlStyle;

            txt2 = new System.Web.UI.WebControls.TextBox();
            txt2.ID = "Text2";
            txt2.MergeStyle(stl3);
            this.Controls.Add(txt2);

            btn2 = new System.Web.UI.WebControls.Button();
            btn2.ID = "Button2";
            btn2.MergeStyle(stl3);
            this.Controls.Add(btn2);           
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            //comment this routine out to see the full results of the CreateChildControls method
            System.Web.UI.WebControls.Style stl;
            System.Web.UI.WebControls.TextBox txt;

           
           
            stl = new System.Web.UI.WebControls.Style();
            stl = this.ControlStyle;
            stl.ForeColor = System.Drawing.Color.Red;
            stl.AddAttributesToRender(writer);

            txt = new System.Web.UI.WebControls.TextBox();
            txt.Text = "hello, world";
            txt.RenderControl(writer);
        }

     }
}

三.比较

1.用户控件

继承:public partial class Customer: System.Web.UI.UserControl

注册:<%@ Register Src="Customer.ascx" TagName="Customer" TagPrefix="uc1" %>

编译选择:CompilerOptions,WarningLevel,Debug,LinePragmas

Two attributes specify how the control is recompiled at run time:Src,CompilationMode

Two options are relevant when writing Visual Basic 2005 in the single-file model:
❑ Explicit: When set to true, requires that all variables used in the code in this file be declared.
❑ Strict: When set to true, requires that most type conversions performed in the code in this file be
explicit.
One option is relevant when using the single-file model, regardless of the language:
❑ Language: This is used to specify the server-side language used in the ascx file. If you are working
in the single-file model and adding server-side code to the ascx file along with your tags, the
language that you use in the ascx is specified here. This has no effect on the language used in the
codebehind file in the two-file model.

If you are using the two-file model you should specify this attribute:
❑ CodeFile: The file containing your source code.
The other attributes that you can use are:
❑ AutoEventWireup: When set to true, it causes ASP.NET to tie events to your code routines by
the name of the routine instead of through event handlers (for example, with this attribute set to
true, when a button called btnSubmit fires a Click event, ASP.NET will look for a routine called
btnSubmit_Click to execute).
❑ Description: A text string description of the user control.
❑ EnableTheming: When set to true, allows themes to be used with the user control.
❑ EnableViewState:

Assuming that you’re using the single-file model with C# with event handlers associated with
your routines by name, you want to set the following attributes and let the other attributes
remain at their default values:
<%@ Control ClassName=”MyClass” Language=”CS” Inherits=”AClass” %>
❑ ClassName to the name that you want to assign to the class associated with your control
❑ Inherits to the name in the ClassName attribute
❑ Language to CS to use C#

protected void Page_PreRender(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlConnection con;
System.Data.SqlClient.SqlCommand cmd;
System.Data.SqlClient.SqlDataReader dr;
con = new System.Data.SqlClient.SqlConnection(strConnection);
cmd = new System.Data.SqlClient.SqlCommand(
“Select CustFName, CustLName From Customers Where CustId = ‘“ +
strCustId + “;”, con);
dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
dr.Read();
this.txtCustFirstName.Text = dr.GetString(0);
this.txtCustLastName.Text = dr.GetString(1);
dr.Close();
con.Close();
}

.Simulating a Constructor:如:

protected override void Construct()
{
}

动态行为:user controls support the same CreateChildControls method and Controls collection,to add a constituent user control to the host page at run time, you must use the LoadControl method of the host page. The LoadControl method must be passed the name of your constituent,user control.
System.Web.UI.Control uc;
uc = this.LoadControl(“CountryInfo.ascx”);
this.Controls.Add(uc);

利用WebParts:

System.Web.UI.WebControls.WebParts.WebPartManager wpm;
wpm = System.Web.UI.WebControls.WebParts.WebPartManager.GetCurrentWebPartManager(
this.Page);
wpm.DisplayMode = WebPartManager.DesignDisplayMode;

 

例子:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Customer.ascx.cs" Inherits="Customer" %>
<asp:Panel ID="Panel1" runat="server" Height="216px" Style="z-index: 100; left: -29px;
    position: absolute; top: -39px" Width="336px">
    <asp:Button ID="Button1" runat="server" Style="z-index: 107; left: 160px; position: absolute;
        top: 174px" Text="Save Changes" />
    <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 108; left: 162px; position: absolute;
        top: 92px"></asp:TextBox>
    <asp:DropDownList ID="DropDownList1" runat="server" Style="z-index: 109; left: 163px;
        position: absolute; top: 118px" Width="154px" Font-Bold="True" Font-Names="Arial">
        <asp:ListItem>United States</asp:ListItem>
    </asp:DropDownList>
</asp:Panel>
<asp:Label ID="Label1" runat="server" BackColor="#C0C0FF" Style="z-index: 102; left: 136px;
    position: absolute; top: 12px" Text="Customer Information:" Font-Names="Arial"></asp:Label>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Customer : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Web.UI.Control uc;

        uc = this.LoadControl("RenderMethodExCS.ascx");
        this.Controls.Add(uc);

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值