自定义控件的构建(7)

在处理回传事件中,还需要考虑的因素就是事件参数和选项

传递回传事件参数

前面曾提到GetPostBackClientHyperlinkc(),可以为其提供一个可选参数,其在引发回传时从浏览器传到服务器,它的值传递到服务器的

RaisePostBackEvent()中,这里实现的功能类似于GridView的分页功能,单击时显示某一页数据

 namespace HandlePostBack
 {   //传递回传事件参数
    public class HandlePostEventParamsControl : WebControl, IPostBackEventHandler
    {
        private string _controlToPage;
        public string ControlToPage
        {
            get { return _controlToPage; }
            set { _controlToPage = value; }
        }
     
        protected override void RenderContents(HtmlTextWriter writer)
        {
            GridView gridview = GetControlToPage();
            for (int i = 0; i < gridview.PageCount; i++)
            {
                string eRef = Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString());
                writer.Write("[");
                if (i == gridview.PageIndex)
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Href, eRef);
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write("{0}", i + 1);
                writer.RenderEndTag();
                writer.Write("] ");
            }
        }
        private GridView GetControlToPage()
        {
            if (String.IsNullOrEmpty(_controlToPage))
            {
                throw new Exception("Must set control property");
            }
            return (GridView)Page.FindControl(_controlToPage);
        }
        public void RaisePostBackEvent(string eventArgument)
        {
            GridView gridview = GetControlToPage();
            gridview.PageIndex = Int32.Parse(eventArgument);
        }
 }   }

 

测试部分的主要代码

 <div>
    <handleps:HandlePostEventParamsControl ID="ParamsControl1" ControlToPage="GridView1" runat="server" />
        <asp:GridView ID="GridView1" DataSourceID="SQL1" runat="server"  AllowPaging="True" PageSize="2" PagerSettings-Visible="false">      
        </asp:GridView>
 </div>
当页面呈现在浏览器时,生成类似下面的链接
[<a href=”javascript:_doPostBack(‘ParamsControl1’,’0’)” 1</a>]
[<a href=”javascript:_doPostBack(‘ParamsControl1’,’1’)” 2</a>]
当单击页面时,关联的页面传回到服务器,RaisePostBackEvent()接受该页码并更改它关联的GridView所显示的分页
处理回传选项

这里主要用到的类是PostBackOptions类,可以到MSDN上查看其属性。

   //使用回传选项
    public class HandlePostOpticesControl : WebControl
    {
        private string _Text;
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }
        private string _PostBackUrl;
        public string PostBackUrl
        {
            get { return _PostBackUrl; }
            set { _PostBackUrl = value; }
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            PostBackOptions options = new PostBackOptions(this);
            options.ActionUrl = _PostBackUrl;
            string eRef = Page.ClientScript.GetPostBackEventReference(options);
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, eRef);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
            base.AddAttributesToRender(writer);
        }
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Input;
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (!String.IsNullOrEmpty(_Text))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.For, this.ClientID);
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write(_Text);
                writer.RenderEndTag();
            }
        }
    }

这个控件可以用来支持跨页提交,当控件被单击时,表单被提交到PostBackUrl所指定的页面,在AddAttributesToRender()中,创建了PostBackOptions类的实例,其用于修改ActionUrl以支持跨页提交,该实例传递给GetPostBackEventReference(),生成用于引发回发的JS

演示部分的关键代码

<handleps:HandlePostOpticesControl ID="PostOpticesControl1" runat="server" Text="高级选项" PostBackUrl="TestStateControl.aspx" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

 

 

本文参考了《ASP.NET 3.5揭秘 (卷2)》

posted @ 2010-08-03 08:30  ringgo  阅读(532)  评论(0编辑  收藏  举报