zoukankan      html  css  js  c++  java
  • web.net用户控件

    1.它是以 Control注册 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApp.WebUserControl1" %>

    2、它的后缀名为.ASCX
    3、它本身不能直接执行
    4、它可以由用户自定义它的属性和事件

    5.它继承System.Web.UI.UserControl

    用户控时间创建是相对比较简单的,直接使用控件可以创建复杂的功能

    定义属性时可以用以下无数设置

    [Browsable(true)]//在属性窗口中是否可见
    [Category("Appearance")]//属性的分类,如,行为,外观,大家可以在属性窗口看见这样的分类
    [DefaultValue("支付方式")]//默认值
    [Description("支付方式")]//这些是显示在属性窗口底下的

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApp.WebUserControl1" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    页面代码

    在.ascx.cs添加属性,事件

    public partial class WebUserControl1 : System.Web.UI.UserControl
        {
            
            private string _myText;
    
            [Browsable(true)]
            public String MyText
            {
                get {
                    return _myText;
                }
                set {
                    _myText = value;
                    TextBox1.Text = _myText;
                }
            }
            
        }
    添加属性
    我们知道,使用事件,就可以将用户控件所知道的信息通过EventArgs来传递给父页面,然后父页面根据这些信息进行处理。那么,首先我们就来定一个EventArgs类。
    public class AddInfoClickEventArgs : EventArgs {
            public string Name { get; set; }
        }
    public event EventHandler<AddInfoClickEventArgs> AddInfoClick;
    我们可以在button单击事件触发这个事件
    protected void Button1_Click(object sender, EventArgs e)
            {
                if (AddInfoClick != null) {
                    var ex = new AddInfoClickEventArgs
                    {
                        Name = TextBox1.Text
                    };
                    AddInfoClick(this, ex);
                }
                
            }
    在页面的html视图里,就可以给这个控件加一个事件处理的代码
    <uc1:WebUserControl1 ID="WebUserControl11" MyText="自定属性" OnAddInfoClick="UCL_Add"  runat="server" />
    在主页后台
    public void UCL_Add(object sender, AddInfoClickEventArgs e) {
                Label1.Text = e.Name;
            }
    注册事件
    protected void Page_Load(object sender, EventArgs e)
            {
                this.WebUserControl11.AddInfoClick+=new EventHandler<AddInfoClickEventArgs>(UCL_Add)
            }
  • 相关阅读:
    8VC Venture Cup 2016
    8VC Venture Cup 2016
    8VC Venture Cup 2016
    HDU 5627 Clarke and MST &意义下最大生成树 贪心
    HDU 5626 Clarke and points 平面两点曼哈顿最远距离
    《花开物语》观后感
    Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp
    Codeforces Round #146 (Div. 1) A. LCM Challenge 水题
    Educational Codeforces Round 7 E. Ants in Leaves 贪心
    Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
  • 原文地址:https://www.cnblogs.com/ymh2013/p/3554414.html
Copyright © 2011-2022 走看看