---  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()
    {
    }
}
}// 那位大哥可不可以把修改后的代码贴上来,谢谢了

解决方案 »

  1.   

    如果可以的话可以把原码发给我
    [email protected]
    谢谢
      

  2.   

    public class johnmain : Page
    {
        protected  JohnControl.Login control1;
        public Button button1;
       
        void button1_click(Object sender, EventArgs e )
        {
             control1.fun();  // 为什么这里调用不了自定义的控件的fun函数
        }
    }
    }
      

  3.   

    我增加protected  JohnControl.Login control1;后还是不行
    错误提示
    The type or namespace name 'JohnControl' could not be found (are you missing a using directive or an assembly reference?)是不是要把login.ascx.cs先编译放在bin文件夹中阿?
      

  4.   

    谢谢你
    下载地址
    http://www.keywhere.com/john/file/source.rar
      

  5.   

    WebUserControl1.ascx
    -----------------
    <%@ 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;
    }
    }
    }
      

  6.   

    WebForm1.aspx
    ------------
    <%@ 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);
    }
    }
    }
      

  7.   

    谢谢你kenMoxi(薪水一点,事儿一堆)