关于Web用户控件如何读取子控件的值自定义事件动态控制子控件状态的应用
效果图
/*用户控件界面开始*/
<%@ Control Language=C# AutoEventWireup=true CodeFile=SearchBarascxcs Inherits=SingcnUCSearchBar %>
<table width=% border= cellpadding= cellspacing= class=coolBar>
<tr>
<td ><DIV CLASS=TBHandle></DIV></TD>
<td >
查询<asp:TextBox ID=search_txt runat=server CssClass=TextBox Width=px></asp:TextBox>
<asp:DropDownList ID=search_lx runat=server>
</asp:DropDownList>
<asp:Button ID=BOk runat=server CssClass=Button Text=查询 OnClick=BOk_Click/>
<asp:Label ID=Label runat=server Text=Label></asp:Label></td>
</tr>
</table>
/*用户控件界面结束*/
/*用户控件代码开始*/
namespace MYUC
{
using System;
using SystemData;
using SystemConfiguration;
using SystemCollections;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
public delegate void BOkClickHandler(object sender SystemEventArgs e); //定义查询按钮委托
//自定义枚举用户控制查询条显示模式
public enum EBarType { mintype = usertype xwxxtype };
//=缺省模式=操作员模式=新闻管理模式
public partial class SearchBar : SystemWebUIUserControl
{
public event BOkClickHandler BOkClick;//定义事件
public string SearchTxt//设置文本框内容
{
get { return thissearch_txtText; }//thissearch_txtText
set { thissearch_txtText = value; }
}
private EBarType _BarType;
public EBarType BarType//设置显示模式
{
get { return _BarType; }
set
{
_BarType = value;
switch (value)
{
case EBarTypexwxxtype:
showxwxx();
break;
case EBarTypeusertype:
showuser();
break;
default://EBarTypemintype;
showmin();
break;
}
}
}
private bool _TxtVisible;
public bool TxtVisible
{
get { return _TxtVisible; }
set
{
_TxtVisible = value;
thissearch_txtVisible = _TxtVisible;
}
}//控制文本框是否显示
private bool _LXVisible;
public bool LXVisible
{
get { return _LXVisible; }
set
{
_LXVisible = value;
thissearch_lxVisible = _LXVisible;
}
}//控制下拉列表是否显示
//private string _SearchLX;
public string SearchLX
{
get { return thissearch_lxSelectedItemValue; }//thissearch_lxSelectedItemValue;_SearchLX
set
{
for (int myi = ; myi < search_lxItemsCount ; myi++)
{
if (search_lxItems[myi]Value == value)
{
search_lxSelectedIndex = myi;
break;
}
}
}
}//设置下拉列表的值
protected void Page_Load(object sender EventArgs e)
{
}
#region web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
baseOnInit(e);
}
private void InitializeComponent()
{
thisBOkClick += new SystemEventHandler(thisBOk_Click);//这一行很重要
}
#endregion
protected void BOk_Click(object sender EventArgs e)
{
// this_SearchTxt = thissearch_txtText;
//this_SearchLX = thissearch_lxSelectedItemValue;
if (BOkClick != null)
BOkClick(this e);
}
private void showxwxx()//新闻管理模式的界面处理
{
LabelVisible = false;
search_txtVisible = true;
search_lxVisible = true;
search_lxItemsClear();
ListItem myitem = new ListItem();
myitemText = 标题;
myitemValue = ;
search_lxItemsAdd(myitem);
myitem = new ListItem();
myitemText = 作者;
myitemValue = ;
search_lxItemsAdd(myitem);
search_lxSelectedIndex = ;
}
private void showuser()//操作员模式的界面处理
{
LabelVisible = false;
search_txtVisible = true;
search_lxVisible = true;
search_lxItemsClear();
ListItem myitem = new ListItem();
myitemText=用户ID;
myitemValue=;
search_lxItemsAdd(myitem);
myitem = new ListItem();
myitemText = 用户名称;
myitemValue = ;
search_lxItemsAdd(myitem);
search_lxSelectedIndex = ;
}
private void showmin()//缺省模式的界面处理
{
LabelVisible = true;
search_txtVisible = true;
search_lxVisible = false;
search_lxItemsClear();
}
}
}
/*用户控件代码结束*/
/*测试页界面开始*/
<%@ Page Language=C# AutoEventWireup=true CodeFile=SearchBarTestaspxcs Inherits=Control_SearchBarTest %>
<%@ Register Src=SearchBarascx TagName=SearchBar TagPrefix=uc %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title>无标题页</title>
<link _fcksavedurl=/CSS/systemcss _fcksavedurl=/CSS/systemcss _fcksavedurl=/CSS/systemcss rel=stylesheet type=text/css />
<link rel=stylesheet type=text/css />
</head>
<body>
<form id=form runat=server>
<div>
<uc:SearchBar ID=SearchBar runat=server BarType=usertype />
<asp:Label ID=Label runat=server Text=查询结果></asp:Label><br />
<asp:Button ID=Button runat=server OnClick=Button_Click Text=缺省 />
<asp:Button ID=Button runat=server OnClick=Button_Click Text=操作员界面 />
<asp:Button ID=Button runat=server OnClick=Button_Click Text=新闻管理界面 />
<asp:Button ID=Button runat=server OnClick=Button_Click Text=隐藏下拉框 /></div>
<br />
</form>
</body>
</html>
/*测试页界面结束*/
/*测试页代码开始*/
using System;
using SystemData;
using SystemConfiguration;
using SystemCollections;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
using MYUC;
public partial class Control_SearchBarTest : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
}
#region web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
baseOnInit(e);
}
private void InitializeComponent()
{
//注册查询条的按钮事件
thisSearchBarBOkClick += new BOkClickHandler(thisSearchBar_BOkClick);
}
#endregion
protected void SearchBar_BOkClick(object sender EventArgs e)
{
//查询按钮事件
LabelText = 查询内容+thisSearchBarSearchTxt + 选择列表:+ thisSearchBarSearchLX;
}
protected void Button_Click(object sender EventArgs e)
{
thisSearchBarBarType = EBarTypemintype;
}
protected void Button_Click(object sender EventArgs e)
{
thisSearchBarBarType = EBarTypeusertype;
}
protected void Button_Click(object sender EventArgs e)
{
thisSearchBarBarType = EBarTypexwxxtype;
}
protected void Button_Click(object sender EventArgs e)
{
thisSearchBarLXVisible = false;
}
}
/*测试页代码结束*/