熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.net Web用戶控件使用技巧

2022-06-13   來源: .NET編程 

  

  關於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;
    }
}

  /*測試頁代碼結束*/


From:http://tw.wingwit.com/Article/program/net/201311/12234.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.