ASP.NET 中页面获取所用用户控件的事件

本文介绍了一个用户控件的实现方式,通过两个用户控件之间的事件传递来更新文本框的内容。具体实现了点击一个用户控件中的按钮后,将该用户控件中的TextBox值复制到另一个用户控件的对应TextBox中。
其实一开始我对于这个问题也很迷茫,经过几次“撞车后,以及请教优快云前辈后
将所学得的分享给有文之士:

用户控件代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="UserControl_WebUserControl" %>
<table style="width: 100%;">
    <tr>
        <td>
            address1</td>
        <td>
           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            address2</td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </td>
        <td>
           
        </td>
    </tr>
</table>

在一个aspx页面中连续两次运用这个用户控件 要求在点击其中一个用户控件的Button后,将其中的TextBox值对应的显示到另外一个用户控件的对应的TextBox中

实现代码如下:
用户控件中代码如下:
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class UserControl_WebUserControl : System.Web.UI.UserControl
{
    public EventHandler myClick;

    public string Text1
    {
        set { TextBox1.Text = value; }
        get { return TextBox1.Text; }
    }

    public string Text2
    {
        set { TextBox2.Text = value; }
        get { return TextBox2.Text; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (myClick != null)
        {
            myClick(this, e);
        }
    }
}


aspx页面的实现代码如下:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebUserControl1.myClick += new EventHandler(click1);
        WebUserControl2.myClick += new EventHandler(click2);
    }

    protected void click1(Object sender, EventArgs e)
    {
        WebUserControl2.Text1 = WebUserControl1.Text1;
        WebUserControl2.Text2 = WebUserControl1.Text2;
    }

    protected void click2(Object sender, EventArgs e)
    {
        Response.Write(WebUserControl2.Text1 + "   " + WebUserControl2.Text2);
    }
}

好了就是这样得了 看似很简单,也确实很简单
但有很多细节需要注意的

而我们普通的情况下 如在控制台应用程序下 实现委托和事件的方法可能如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public delegate void runing(string r,int key);
    public class Program
    {
        public runing myclick;

        static void Main(string[] args)
        {
            Program pa = new Program();
            pa.getClick();

            pa.myclick("Program", 1);
        }

        public void getClick()
        {
            if (myclick == null)
            {
                myclick += new runing(regText);
            }
        }

        public void regText(string r, int key)
        {
            Console.WriteLine("我已被调用");
        }
    }
}

这样的用法 我想大家都不会他陌生

用户控件:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值