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. Page.ClientScript.RegisterStartupScript() 方法与Page.ClientScript.RegisterClientScriptBlock() 方法...
  2. Dubbo的Provider配置
  3. su su - sudo
  4. html:(40):块级元素和内联块级元素
  5. 最通俗易懂的图神经网络(GCN)原理详解
  6. 手机内存占用超过一半会不会卡?
  7. verilog之按键消抖的理解
  8. mybatis中的#{}与${}在原理上的区别
  9. C++类成员函数的传参问题
  10. Shell(一) 入门到复杂 自己做的各种脚本实例与解释
  11. 数据集:男女身高体重(二维)
  12. html网页设计实验原理,网页设计实验报告
  13. P1195口袋的天空
  14. iPhone下mp4视频无法播放和部分手机只有声音没有画面
  15. VR虚拟线上展馆之水资源绿色发展博览会
  16. Qt 5.7设置调试器
  17. 力扣 345. 反转字符串中的元音字母
  18. sql中向下取整怎么取_SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN
  19. 微积分——什么是导数
  20. 九零后程序员心塞:“30岁,月薪还没过万,是我的问题吗”

热门文章

  1. react native 组件汇总整理,点击链接至GitHub
  2. 浅谈面向对象编程与面向过程编程
  3. You may use special comments to disable some warnings. Use // eslint-disable-next-line……
  4. Vue项目报错:This relative module was not found
  5. jQuery实现影院选座订座效果
  6. JavaScript学习(二十)—DOM中常用的属性
  7. 今天来谈谈CSS有哪些布局
  8. 利用Navicat Premium将SQL Server数据库转为My SQL数据库(解决Mac无法打开SQL Server 脚本文件的方法)
  9. 80×86汇编常用指令
  10. 手机正在录音怎么隐藏