您的位置:首页 > 编程语言 > ASP

asp.net 调用用户控件中的方法

2008-12-13 13:37 344 查看
一、用户控件ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication4.WebUserControl1" %>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
二、用户控件ascx.cs

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

{

protected void Page_Load(object sender, EventArgs e)

{

}

public void jj() {

TextBox1.Text = "aa";

//Response.Write("dd");

}

}
三、Aspx

<%@ Register TagName="aa" TagPrefix="bb" Src="~/WebUserControl1.ascx" %>

<bb:aa ID="ff" runat="server" />

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
四、Aspx.cs

protected void Button1_Click(object sender, EventArgs e)

{

WebUserControl1 gw = (WebUserControl1)(this.FindControl("ff"));

gw.jj();

}
说明:
调用用户控件网上有一些讨论:如反射、委托等,此法找到ASPX页面中的用户控件并转换为用户控件类型,之后调用其公共方法。
欢迎讨论:ikmb@163.com QQ:154179812
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐