--- main.aspx------
<%@ Page Inherits=johnapp.johnmain Src="main.aspx.cs"%>
<%@ Register TagPrefix="john" TagName="login" Src="login.ascx" %>
<html>
<head>
</head>
<body>
<form runat="server">
<john:login id="control1" runat="server"></john:login>
<asp:Button id="button1" runat="server" Text="fun"></asp:Button>
</form>
</body>
</html>
------- main.aspx.cs--------------
namespace johnapp {
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class johnmain : Page
{
public johnmain() {}
public Button button1;
void button1_click(Object sender, EventArgs e )
{
control1.fun(); // 为什么这里调用不了自定义的控件的fun函数
}
}
}
----------用户控件login.ascx--------------
<%@ Control Language="c#" Inherits="JohnControl.Login" Src="Login.ascx.cs" %>
----------login.ascx.cs--------------------
namespace JohnControl {
using System;
using System.Web.UI;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class Login : UserControl
{
public Login() {}
public void fun()
{
}
}
}// 那位大哥可不可以把修改后的代码贴上来,谢谢了
<%@ Page Inherits=johnapp.johnmain Src="main.aspx.cs"%>
<%@ Register TagPrefix="john" TagName="login" Src="login.ascx" %>
<html>
<head>
</head>
<body>
<form runat="server">
<john:login id="control1" runat="server"></john:login>
<asp:Button id="button1" runat="server" Text="fun"></asp:Button>
</form>
</body>
</html>
------- main.aspx.cs--------------
namespace johnapp {
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class johnmain : Page
{
public johnmain() {}
public Button button1;
void button1_click(Object sender, EventArgs e )
{
control1.fun(); // 为什么这里调用不了自定义的控件的fun函数
}
}
}
----------用户控件login.ascx--------------
<%@ Control Language="c#" Inherits="JohnControl.Login" Src="Login.ascx.cs" %>
----------login.ascx.cs--------------------
namespace JohnControl {
using System;
using System.Web.UI;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class Login : UserControl
{
public Login() {}
public void fun()
{
}
}
}// 那位大哥可不可以把修改后的代码贴上来,谢谢了
解决方案 »
- GridView如何获取当前DataRow对象赋值给模板列中的用户控件
- 怎么获取html里面有多少组单选按钮
- 谁能给我讲讲.net的solution、project是什么样的关系
- JS调用后台海量数据,“高手请教”!在线等!!
- 自定义控件事件问题
- 急!急!本人求1個操作(包括添加,刪除,更新,選擇功能的!)XML文件的留言簿之類的程序的源代碼!一定要可以 運行的!謝謝!
- 看了一些相关datagrid分页的文章,觉得微软怪怪的(没有分了)
- 我想弄个asp.net的html编辑器控件,那里有啊?
- Entity Framework(EF) async await 异步操作数据问题,高手都进来啊!!
- 问一下,同一个解决方案里的不同项目中的页面如何传递参数啊?
- 急问大家一个关于学习ASP.NET的问题
- 如何利用ItemCreate事件添加控件??? 为什么添加不上TextBox
[email protected]
谢谢
{
protected JohnControl.Login control1;
public Button button1;
void button1_click(Object sender, EventArgs e )
{
control1.fun(); // 为什么这里调用不了自定义的控件的fun函数
}
}
}
错误提示
The type or namespace name 'JohnControl' could not be found (are you missing a using directive or an assembly reference?)是不是要把login.ascx.cs先编译放在bin文件夹中阿?
下载地址
http://www.keywhere.com/john/file/source.rar
-----------------
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="WebApplication5.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Label id="Label1" runat="server" Width="240px"></asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>WebUserControl1.ascx.cs
--------------------
namespace WebApplication5
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; /// <summary>
/// WebUserControl1 的摘要说明。
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1; private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
this.Label1.Text = "control onload";
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion public void Button1_Click(object sender, System.EventArgs e)
{
this.Label1.Text = this.TextBox1.Text;
}
}
}
------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication5.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 224px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>
WebForm1.aspx.cs
-----------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebApplication5
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected WebApplication5.WebUserControl1 WebUserControl11; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
this.WebUserControl11.Button1_Click(sender,e);
}
}
}