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
>
</
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);
????????????}
????????}
????}
}
2. 跟一般網頁一樣放控件。
3. 在事件里面寫處理過程。
4. 拖到aspx頁面就可以用了。
過程注意的地方。
1.如果用到javascript ,用document.getElementById()來查找服務器控件需要在控件名前面加上 "<%= this.ClientID %>_控件名".
2.如果有圖片之類需要路徑的請用:/ a / b / c.jpg
3.需要調用用戶控件事件的話需要寫一個委托。
以下面做一個查詢為例
html 代碼


























































































































































2. cs 代碼










































































































































































































































































更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
