自定义控件

asp.net控件继承自Control.

UniqueID,ClientID属性.

 

实现接口:

 

1、数据回发接口。IPostBackDataHandler

 

两个方法:

 

LoadPostData(PostDatakey key,PostBackDataCollention pos){

  //获取属性的上次请求的值,间接从视图状态获得。

 

  //给属性设置当前请求的值,最终保存到视图状态。

 

  //比较上次请求和本次请求的值,来决定返回真假。

  return true/false;//asp.net会根据这个值来决定是否调用下面的方法。

}

RaisePostDataChangedEvent();这个方法里点火数据变化事件。

 

     writer.WriteAttribute ("onchange", "javascript:" +
                    Page.GetPostBackEventReference (this,[param]));

                 //这个方法很有意思,获取自动回发的客户段脚本参考。

 即:οnchange="javascript:__doPostBack('Input','')">

 

//下面也是上面的api产生的。

<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform = document._ctl0;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>

 

例子:

 

public class MyTextBox : Control, IPostBackDataHandler
    {
        bool MyAutoPostBack = false;
        public event EventHandler TextChanged;

        public string Text
        {
            get
            {
                string text = (string) ViewState["MyText"];
                return (text == null) ? "" : text;
            }
            set { ViewState["MyText"] = value; }
        }

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

        public bool LoadPostData (string postDataKey,
            NameValueCollection postCollection)
        {
            string temp = Text;
            Text = postCollection[postDataKey];
            return (temp != Text);
        } 

        public void RaisePostDataChangedEvent ()
        {
            if (TextChanged != null)
                TextChanged (this, new EventArgs ()); // Fire event
        }

        protected override void Render (HtmlTextWriter writer)
        {
            writer.WriteBeginTag ("input");
            writer.WriteAttribute ("type", "text");
            writer.WriteAttribute ("name", UniqueID);

            if (ID != null)
                writer.WriteAttribute ("id", ClientID);

            if (Text.Length > 0)
                writer.WriteAttribute ("value", Text);

            if (AutoPostBack)
                writer.WriteAttribute ("onchange", "javascript:" +
                    Page.GetPostBackEventReference (this));

            writer.Write (HtmlTextWriter.TagRightChar);
        }
    }
}

2-触发回发的动作事件接口

IPostBackEventHandler

 

接口方法:

RaisePostBackEvent( String evtArgs//对应客户端的 function __doPostBack(eventTarget, eventArgument) 中的参数对象)

 

因为eventTarget,asp.net会调用目标对象的RaisePostBackEvent这个方法,在这个方法里点火回发事件处理。

 

Summary

Here’s a summary of the important concepts presented in this chapter:

  • Custom controls are authored by deriving from System.Web.UI.Control.

  • A custom control overrides the Render method it inherits from Control to render itself to a Web page.

  • Implementing IPostBackDataHandler enables a control to update property values using data transmitted in postbacks and also to fire change events.

  • Controls that fire change events persist property values in view state in order to detect changes in property values.

  • Controls can force postbacks by rendering HTML elements that use client-side scripts to submit forms to the server. Page.GetPostBackEventReference outputs the postback script and returns code that calls the postback function.

  • Implementing IPostBackEventHandler enables a control to fire events when an element it rendered to a page causes a postback.

  • Composite controls serve as containers for other controls. A composite can be populated with child controls declaratively or programmatically.

  • Controls have the option of returning client-side script when they render themselves to a Web page. Page.RegisterClientScriptBlock ensures that functions returned in a script block are returned just once per request.

  • Controls can use the Browser property of the Request object to determine which type of browser submitted the request and adapt their output accordingly.

  • Graphical controls can be authored by returning <img> tags whose Src attributes reference image files stored on the server or HTTP handlers that generate images and return them to the client.

  • The @ Register directive enables custom controls to be used in Web forms.

Controls are the atoms from which Web forms are composed. Encapsulating complex rendering and behavioral logic in custom controls is a great way to share your knowledge with other developers and shield them from implementation details at the same time. As you design and implement controls of your own, keep in mind the principles discussed in this chapter. And feel free to use the samples contained herein as the basis for controls of your own.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值