1. 新建 *.axcx 文件。
2. 跟一般网页一样放控件。
3. 在事件里面写处理过程。
4. 拖到aspx页面就可以用了。

过程注意的地方。
1.如果用到javascript ,用document.getElementById()来查找服务器控件需要在控件名前面加上 "<%= this.ClientID %>_控件名".
2.如果有图片之类需要路径的请用:/ a / b / c.jpg
3.需要调用用户控件事件的话需要写一个委托。
以下面做一个查询为例
html 代码

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Search.ascx.cs" Inherits="Search" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" enableViewState="True"%>
<script>
            var ParentName = "<%= this.ClientID %>";
            function initXMLHTTPRequest(){
                var xRequest = null;
                if ( window.XMLHttpRequest ){
                    xRequest = new XMLHttpRequest();
                } else if ( window.ActiveXObject ){
                    xRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                return xRequest;
            }
            function GetDataSetCity(obj)
            {                
                var svalue=obj.value;
                var webFileUrl = "?father=" + svalue;
              var result = "";
              var xmlHttp = initXMLHTTPRequest(); //new ActiveXObject("MSXML2.XMLHTTP");
              xmlHttp.open("POST", webFileUrl, false);
              xmlHttp.send("");
              
              result = xmlHttp.responseText;

                if(result != "")
                {
                    document.all(ParentName+"_dropCity").length=0;
                     var piArray = result.split(",");
                     for(var i=0;i<piArray.length;i++)
                     {
                         var ary1 = piArray[i].toString().split("|");
                         document.all(ParentName+"_dropCity").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
                     }
                }
                else
                {
                    for (i=(document.all(ParentName+"_dropCity").length-1);i>=0;i--){
                        document.all(ParentName+"_dropCity").options.remove(i);
                    }
                    document.all(ParentName+"_dropCity").options.add(new Option("城市","0"));
                }
            }
            function getData()
            {
                var province=document.getElementById(ParentName+"_dropProvince");
                var pindex = province.selectedIndex;
                var pValue = province.options[pindex].value;
                var pText  = province.options[pindex].text;
                
                var city=document.getElementById(ParentName+"_dropCity");
                var cindex = city.selectedIndex;
                var cValue = city.options[cindex].value;
                var cText  = city.options[cindex].text;
                
                document.getElementById(ParentName+"_hidprovince").value=pText;
                document.getElementById(ParentName+"_hidcity").value=cText;
            }
            function JHR()
            {
                window.history.back();
            }
</script>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
    <tr>
        <td vAlign="bottom" align="right" width="24" height="218"><IMG height="218" src="/yizhi/images/chaxun_12.gif" width="24"></td>
        <td vAlign="bottom" align="left">
            <table id="__01" height="235" cellSpacing="0" cellPadding="0" width="216" border="0">
                <tr>
                    <td colSpan="3"><IMG height="102" alt="" src="/yizhi/images/chaxun_01.gif" width="216"></td>
                </tr>
                <tr>
                    <td><IMG height="8" alt="" src="/yizhi/images/chaxun_02.gif" width="15"></td>
                    <td><IMG height="8" alt="" src="/yizhi/images/chaxun_03.gif" width="185"></td>
                    <td><IMG height="8" alt="" src="/yizhi/images/chaxun_04.gif" width="16"></td>
                </tr>
                <tr>
                    <td background="/yizhi/images/chaxun_05.gif" width="15"></td>
                    <td align="center" width="185" bgColor="#f7f7f7" height="103">
                        <table cellSpacing="5" cellPadding="0" width="90%" border="0">
                            <tr>
                                <td align="left"><asp:textbox id="txtName" Runat="server">请输入关键字</asp:textbox><a id="aSearchs" onclick="JHR();" href="UserListAll.aspx" target="_blank"></td>
                            </tr>
                            <tr>
                                <td align="left"><asp:DropDownList ID="dropCategory" Runat="server"></asp:DropDownList></td>
                            </tr>
                            <tr>
                                <td align="left"><asp:DropDownList ID="dropTime" Runat="server">
                                        <asp:ListItem Value="0">发布时间</asp:ListItem>
                                        <asp:ListItem Value="1">1天内</asp:ListItem>
                                        <asp:ListItem Value="2">2天内</asp:ListItem>
                                        <asp:ListItem Value="3">3天内</asp:ListItem>
                                        <asp:ListItem Value="4">4天内</asp:ListItem>
                                        <asp:ListItem Value="5">5天内</asp:ListItem>
                                        <asp:ListItem Value="10">10天内</asp:ListItem>
                                        <asp:ListItem Value="20">20天内</asp:ListItem>
                                        <asp:ListItem Value="30">一个月内</asp:ListItem>
                                        <asp:ListItem Value="90">三个月内</asp:ListItem>
                                    </asp:DropDownList></td>
                            </tr>
                            <tr>
                                <td align="left"><asp:DropDownList ID="dropProvince" Runat="server"></asp:DropDownList>
                                    <asp:DropDownList ID="dropCity" Runat="server"></asp:DropDownList></td>
                            </tr>
                            <tr>
                                <td align="left"><asp:Button ID="btnSearch" Runat="server" Text="搜 索"></asp:Button><INPUT id="hidprovince" style="WIDTH: 31px; HEIGHT: 22px" type="hidden" size="1" name="hidprovince"
                                        runat="server"><INPUT id="hidcity" style="WIDTH: 34px; HEIGHT: 22px" type="hidden" size="1" name="hidprovince"
                                        runat="server"></td>
                            </tr>
                        </table>
                    </td>
                    <td background="/yizhi/images/chaxun_07.gif" width="16"></td>
                </tr>
                <tr>
                    <td>
                        <img src="/yizhi/images/chaxun_08.gif" width="15" height="8" alt=""></td>
                    <td>
                        <img src="/yizhi/images/chaxun_09.gif" width="185" height="8" alt=""></td>
                    <td>
                        <img src="/yizhi/images/chaxun_10.gif" width="16" height="8" alt=""></td>
                </tr>
                <tr>
                    <td colspan="3">
                        <img src="/yizhi/images/chaxun_11.gif" width="216" height="14" alt=""></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

2. cs 代码

namespace ProductControls
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    /**//// <summary>
    ///Search 的摘要说明。
    /// </summary>
    public class Search : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.TextBox txtName;
        protected System.Web.UI.WebControls.DropDownList dropCategory;
        protected System.Web.UI.WebControls.DropDownList dropTime;
        protected System.Web.UI.WebControls.DropDownList dropProvince;
        protected System.Web.UI.WebControls.DropDownList dropCity;
        protected System.Web.UI.WebControls.Button btnSearch;
        protected System.Web.UI.HtmlControls.HtmlInputHidden hidprovince;
        protected System.Web.UI.HtmlControls.HtmlInputHidden hidcity;
        YzBLL.Members m=new YzBLL.Members();
        public string CategoryName
        {
            get
            {
                string Name = this.txtName.Text.Trim();
                if ( Name == "请输入关键字" )
                    Name = "";
                return Name;
            }
            set
            {
                
                this.txtName.Text = value;
            }
        }
        public string CategoryID
        {
            get
            {
                return this.dropCategory.SelectedValue;
            }
            set
            {
                this.dropCategory.SelectedValue = value;
            }
        }
        public string CreateTime
        {
            get
            {
                return this.dropTime.SelectedValue;
            }
            set
            {
                this.dropTime.SelectedValue = value;
            }
        }
        public string Area
        {
            get
            {
                string area = "不限";
                if ( this.hidprovince.Value.Trim() != "省份"  )
                {
                    area = this.hidprovince.Value;
                    if ( this.hidcity.Value.Trim() != "城市" )
                        area = area +"-"+this.hidcity.Value.Trim();
                }
                else
                    area = "";
                return area;
            }
        }
        private void Page_Load(object sender, System.EventArgs e)
        {
            this.BindddlProvince();
            if(father!="")
            {
                this.BindddlCity(Convert.ToInt32(father));
            }
            else
            {
                dropCity.Items.Clear();
                dropCity.Items.Add(new ListItem("城市","0"));
            }
            // 在此处放置用户代码以初始化页面
            if ( !Page.IsPostBack )
            {
                BindCategory();
                BindddlProvince();
                this.dropProvince.Attributes.Add("onchange","GetDataSetCity(this);");
                this.btnSearch.Attributes.Add("onclick","getData();");
            }
        }
        private void BindCategory()
        {
            DataTable dtCategory = new DataTable();
            YzBLL.YzGqInfo gq = new YzBLL.YzGqInfo();
            dtCategory = gq.GetCategory();
            this.dropCategory.DataSource = dtCategory;
            this.dropCategory.DataTextField = "CategoryName";
            this.dropCategory.DataValueField = "Categoryid";
            this.dropCategory.DataBind();
            this.dropCategory.Items.Insert(0,new ListItem("产品种类","0"));
        }
        //绑定ddlProvince
        public void BindddlProvince()
        {
            DataTable dt=m.AllProvince();
            this.dropProvince.DataSource=dt.DefaultView;
            this.dropProvince.DataTextField="province";
            this.dropProvince.DataValueField="provinceID";
            this.dropProvince.DataBind();
            this.dropProvince.Items.Insert(0,new ListItem("省份","0"));
        }
        //绑定BindddlCity
        public void BindddlCity(int father)
        {
            string mystr = "";
          DataTable dt=m.AllCity(father);

          if(dt.Rows.Count != 0)
          {
              mystr+=","+"0"+"|"+"城市";
            for(int i=0;i<dt.Rows.Count;i++)
            {
              mystr += "," + dt.Rows[i][1].ToString() + "|" + dt.Rows[i][2].ToString();
            }
            mystr = mystr.Substring(1);
          }
          this.Response.Write(mystr);
          this.Response.End();
        }

        private string father
        {
            get
            {
                if(ViewState["father"]!=null && ViewState["father"].ToString()!="")
                {
                    return ViewState["father"].ToString();
                }
                else
                {
                    if(Request["father"]!=null && Request["father"].ToString()!="")
                    {
                        return Request["father"];
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            set
            {
                ViewState["father"]=value;
            }
        }
        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /**//// <summary>
        ///        设计器支持所需的方法 - 不要使用代码编辑器
        ///        修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        public delegate void OnSearch(object sender,System.EventArgs e);
        public event OnSearch onSearch;
        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            if ( onSearch != null )
            {
                onSearch(sender,e);
            }
        }
    }
}

转载于:https://www.cnblogs.com/mullr2005/archive/2006/09/18/507178.html

如何实现用户自己定义控件。相关推荐

  1. git-osc自己定义控件之:CircleImageView

    git-osc自己定义控件之:CircleImageView 一.CircleImageView的使用 在项目中能够发现,用户的头像都是圆形的.感觉非常好奇,昨天最终发现了,原来是自定了一个Image ...

  2. Android自己定义控件2-简单的写字板控件

    概述 上一篇文章我们对自己定义控件进行了一个大体的知识介绍. 今天就来学习自己定义一个简单的写字板控件. 先来看看效果图 就是简单的依据手指写下的轨迹去画出内容 实现 在上一篇文章里提到了androi ...

  3. android 原理 组合控件_Android自定义控件进阶01-自定义控件开发套路与流程

    Android自定义控件进阶01-自定义控件开发套路与流程本章节为什么要叫进阶篇?(虽然讲的是基础内容),因为从本篇开始,将会逐渐揭开自定义View的神秘面纱,每一篇都将比上一篇内容更加深入,利用所学 ...

  4. iOS项目开发实战——使用Xcode6设计自己定义控件与图形

    在iOS开发中,有很多控件都是Xcode默认提供的.使用这些控件是很方便的.可是因为某些须要.须要自己设计控件,那么应该怎么做呢?在Xcode6中提供了这种接口,同意开发人员高速开发自己定义控件,而且 ...

  5. 【Android】自己定义控件——仿天猫Indicator

    今天来说说类似天猫的Banner中的小圆点是怎么做的(图中绿圈部分) 在学习自己定义控件之前,我用的是很二的方法,直接在布局中放入多个ImageView,然后代码中依据Pager切换来改变图片.这样的 ...

  6. 自己定义控件事实上非常easy1/6

    尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 上一节我们粗略地讲了下怎样去实现我 ...

  7. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  8. 自己定义控件-画板,橡皮擦,刮刮乐

    画板效果图 页面代码 public class ActionerView extends View {private Paint mPaint = new Paint();private Path m ...

  9. 定义控件有效 无效EnableWindow

    定义控件有效无效GetDlgItem(IDC_Pre_Name)-> EnableWindow(FALSE); 定义控件有效有效GetDlgItem(IDC_Pre_Name)-> Ena ...

最新文章

  1. 什么都不说,来4波js
  2. notify和wait
  3. Entity Framework 4 in Action读书笔记——第一章:数据访问重载:Entity Framework(2)...
  4. Hibernate查询缓存如何工作
  5. 【牛客 - 317F】小a的子序列(线性dp,tricks)
  6. TensorFlow10-多层神经网络建模,存储和载入
  7. (网络收藏)WIKI
  8. Deploy sahara on openstack-icehouse
  9. python 对列表元素分割_python 列表快速排序和冒泡排序和列表中根据元素取模分割列表...
  10. java删除文件夹及下面的所有文件
  11. 智慧交通day02-车流量检测实现13:基于虚拟线圈法的车辆统计+视频中的车流量统计原理解析
  12. Adobe Flash Builder 4 序列号
  13. ps2021神经ai滤镜无法使用,ps2021没法用神经元滤镜
  14. github 更纱黑体_更纱黑体v0.12.6
  15. (z)无杂散动态范围SFDR--影响通信机性能的因素
  16. 免费下载中国知网、万方学术论文的几种方法(福利合集)
  17. 音视频开发第一课-使用C语言开发视频播放器 650元IT外包开发全程记录
  18. 百度砸120W年薪,只要这个专业,应届生也行!
  19. 用最小二乘法来拟合数据
  20. 服务器window如何设置防火墙开放端口

热门文章

  1. LeetCode(1029)——两地调度(JavaScript)
  2. 如何优雅的起个变量名?
  3. java界面字体大小设置_怎样更改电脑界面的字体大小?
  4. iCloud 是什么
  5. 音乐陶冶情操,怎样让孩子喜欢音乐?
  6. 秋游可以去哪些地方?
  7. 在创业之路上,每个人都会有很多的老师
  8. 赚钱有捷径吗?为什么有的人赚钱很容易
  9. 在淘宝买水果短斤缺两是不是成了潜规则?
  10. 各年龄段都是怎么提加薪的?