分享
 
 
 

.netWeb用户控件使用技巧。

王朝c#·作者佚名  2007-01-07
窄屏简体版  字體: |||超大  

关于Web用户控件如何读取子控件的值,自定义事件,动态控制子控件状态的应用。

效果图:

/*用户控件界面开始*/

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SearchBar.ascx.cs" Inherits="Singcn.UC.SearchBar" %>

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="coolBar">

<tr>

<td style="WIDTH: 3px; CURSOR: move; height: 26px;"><DIV CLASS="TBHandle"></DIV></TD>

<td style="height: 26px">

&nbsp;查询:<asp:TextBox ID="search_txt" runat="server" CssClass="TextBox" Width="120px"></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="Label1" runat="server" Text="Label"></asp:Label></td>

</tr>

</table>

/*用户控件界面结束*/

/*用户控件代码开始*/

namespace MY.UC

{

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public delegate void BOkClickHandler(object sender, System.EventArgs e); //定义查询按钮委托

//自定义枚举,用户控制查询条显示模式

public enum EBarType { mintype = 1, usertype, xwxxtype };

//1=缺省模式,2=操作员模式,3=新闻管理模式

public partial class SearchBar : System.Web.UI.UserControl

{

public event BOkClickHandler BOkClick;//定义事件

public string SearchTxt//设置文本框内容

{

get { return this.search_txt.Text; }//this.search_txt.Text

set { this.search_txt.Text = value; }

}

private EBarType _BarType;

public EBarType BarType//设置显示模式

{

get { return _BarType; }

set

{

_BarType = value;

switch (value)

{

case EBarType.xwxxtype:

showxwxx();

break;

case EBarType.usertype:

showuser();

break;

default://EBarType.mintype;

showmin();

break;

}

}

}

private bool _TxtVisible;

public bool TxtVisible

{

get { return _TxtVisible; }

set

{

_TxtVisible = value;

this.search_txt.Visible = _TxtVisible;

}

}//控制文本框是否显示

private bool _LXVisible;

public bool LXVisible

{

get { return _LXVisible; }

set

{

_LXVisible = value;

this.search_lx.Visible = _LXVisible;

}

}//控制下拉列表是否显示

//private string _SearchLX;

public string SearchLX

{

get { return this.search_lx.SelectedItem.Value; }//this.search_lx.SelectedItem.Value;_SearchLX

set

{

for (int myi = 0; myi < search_lx.Items.Count ; myi++)

{

if (search_lx.Items[myi].Value == value)

{

search_lx.SelectedIndex = myi;

break;

}

}

}

}//设置下拉列表的值

protected void Page_Load(object sender, EventArgs e)

{

}

#region web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

this.BOk.Click += new System.EventHandler(this.BOk_Click);//这一行很重要

}

#endregion

protected void BOk_Click(object sender, EventArgs e)

{

// this._SearchTxt = this.search_txt.Text;

//this._SearchLX = this.search_lx.SelectedItem.Value;

if (BOkClick != null)

BOkClick(this, e);

}

private void showxwxx()//新闻管理模式的界面处理

{

Label1.Visible = false;

search_txt.Visible = true;

search_lx.Visible = true;

search_lx.Items.Clear();

ListItem myitem = new ListItem();

myitem.Text = "标题";

myitem.Value = "1";

search_lx.Items.Add(myitem);

myitem = new ListItem();

myitem.Text = "作者";

myitem.Value = "2";

search_lx.Items.Add(myitem);

search_lx.SelectedIndex = 0;

}

private void showuser()//操作员模式的界面处理

{

Label1.Visible = false;

search_txt.Visible = true;

search_lx.Visible = true;

search_lx.Items.Clear();

ListItem myitem = new ListItem();

myitem.Text="用户ID";

myitem.Value="1";

search_lx.Items.Add(myitem);

myitem = new ListItem();

myitem.Text = "用户名称";

myitem.Value = "2";

search_lx.Items.Add(myitem);

search_lx.SelectedIndex = 0;

}

private void showmin()//缺省模式的界面处理

{

Label1.Visible = true;

search_txt.Visible = true;

search_lx.Visible = false;

search_lx.Items.Clear();

}

}

}

/*用户控件代码结束*/

/*测试页界面开始*/

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchBarTest.aspx.cs" Inherits="Control_SearchBarTest" %>

<%@ Register Src="SearchBar.ascx" TagName="SearchBar" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>无标题页</title>

<link href="../CSS/system.css" _fcksavedurl=""../CSS/system.css"" _fcksavedurl=""../CSS/system.css"" _fcksavedurl=""../CSS/system.css"" rel="stylesheet" type="text/css" />

<link href="../CSS/searchbar.css" rel="stylesheet" type="text/css" />

</head>

<body>

<form id="form1" runat="server">

<div>

<uc1:SearchBar ID="SearchBar1" runat="server" BarType="usertype" />

<asp:Label ID="Label1" runat="server" Text="查询结果"></asp:Label><br />

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="缺省" />

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="操作员界面" />

<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="新闻管理界面" />

<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="隐藏下拉框" /></div>

<br />

</form>

</body>

</html>

/*测试页界面结束*/

/*测试页代码开始*/

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using MY.UC;

public partial class Control_SearchBarTest : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

#region web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

//注册查询条的按钮事件

this.SearchBar1.BOkClick += new BOkClickHandler(this.SearchBar1_BOkClick);

}

#endregion

protected void SearchBar1_BOkClick(object sender, EventArgs e)

{

//查询按钮事件

Label1.Text = "查询内容:"+this.SearchBar1.SearchTxt +" 选择列表:"+ this.SearchBar1.SearchLX;

}

protected void Button1_Click(object sender, EventArgs e)

{

this.SearchBar1.BarType = EBarType.mintype;

}

protected void Button2_Click(object sender, EventArgs e)

{

this.SearchBar1.BarType = EBarType.usertype;

}

protected void Button3_Click(object sender, EventArgs e)

{

this.SearchBar1.BarType = EBarType.xwxxtype;

}

protected void Button4_Click(object sender, EventArgs e)

{

this.SearchBar1.LXVisible = false;

}

}

/*测试页代码结束*/

如需转载请注明出处:http://blog.csdn.net/stbrine

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有