_控件名".2.如果有图片之类需要路径的请用:/a/b/c.jpg3.需要调用用户控件事件的话需要写一个委托。以下面做一个查询为例html代码<%@ControlLangua" />

如何实现用户自己定义控件。

系统 1437 0
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 >< 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 窗体设计器生成的代码

        
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);
            }

        }

    }

}

如何实现用户自己定义控件。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论