可以,你可以在母窗体中设计公用的界面和控件
然后子窗体从这个母窗体派生,(注:如果母窗体的控件不是通过编码写出来,而是在Aspx文件当中的,那么子窗体必须保证在Aspx文件中有这些控件)母窗体可以把控件声明成protected,然后可以写这些控件的事件,子窗体只要不重新声明控件就可以公用这些事件代码。

解决方案 »

  1.   

    可以的,实现的方法有很多,请看这里:
    http://aspalliance.com/PaulWilson/Articles/?id=1
      

  2.   

    >>>>可以,你可以在母窗体中设计公用的界面和控件, 然后子窗体从这个母窗体派生Mr.  timmy3310(tim):
     Have you tried it? can you show us an example on how to subclass an aspx page?
      

  3.   

    继承cs弄成的Page还可以。
    基本是Template Method的模式。例如:
    <%@Page Inherits='Website.SharedGridPage" %>
    <script>
    override protected string SetSelectCommandText()//abstract method of Website.SharedGridPage
    {
        return "Select * FROM Employees";
    }
    </script>
    <FROM runat='server'>
    <datagrid id="PageDataGrid' runat=server>
    ..custom style here..
    </datagrid>
    </FORM>按经验来说,我更加喜欢把按纽和DATAGRID集合一起做成控件.而不是做在PAGE里。如果是要继承.aspx生成的PAGE类。那可真的没有必要大费工夫。
      

  4.   

    我做过的两个非界面的。:
    http://expert.csdn.net/Expert/TopicView1.asp?id=1299781
    http://expert.csdn.net/Expert/TopicView1.asp?id=1488797
      

  5.   

    今天刚好规划网站..:using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;namespace Website
    {
    public class SitePage:SqlScopePage
    {
    override protected void OnInit(EventArgs e)
    {
    base.OnInit(e);
    Page.RegisterClientScriptBlock("SitePage.res",string.Format(
    @"<script src='{0}/sitepage.js'><!--/**/--></script>
    <link rel='stylesheet' href='{0}/sitepage.css'/>
    "
    ,V.GetResDir()
    ));
    }
    override protected void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    } public DataRow GetMember()
    {
    BeginTransaction();
    try
    { HttpCookieCollection cookies=Request.Cookies;
    object omid=Session["MemberID"]; if(omid!=null)
    {
    int mid=(int)omid;
    BeginTransaction();
    try
    {
    using(SqlCommand cmd=CreateCommand(
      @"
    SELECT 用户.*
    FROM 用户
    WHERE 用户.用户ID=@UID
    "
      ))
    {
    cmd.Parameters.Add("@UID",mid);
    using(SqlDataAdapter sda=new SqlDataAdapter(cmd))
    {
    DataSet ds=new DataSet();
    sda.Fill(ds);
    if(ds.Tables[0].Rows.Count>0)
    return ds.Tables[0].Rows[0];
    }
    }
    }
    finally
    {
    Commit();
    }
    } HttpCookie logon=cookies["Logon"];
    if(logon==null)
    throw(new Exception("Not_Logon"));
    string LogonID=logon["LogonID"];
    string Password=logon["Password"];
    if(LogonID==null||Password==null)
    throw(new Exception("Not_Logon"));
    if(LogonID.Length==0||Password.Length==0)
    throw(new Exception("Not_Logon"));
    try
    {
    LoginAS(int.Parse(LogonID),Password);
    }
    catch(Exception x)
    {
    throw(new Exception("Not_Logon",x));
    } return GetMember(); }
    finally
    {
    Commit();
    }
    }
    public void LoginAS(int LogonID,string Password)
    {
    BeginTransaction();
    try
    {
    using(SqlCommand cmd=CreateCommand(
      @"
    SELECT 用户登陆.用户登陆ID, 用户.用户ID, 用户登陆.登陆密码, 用户登陆.是否有效, 
          用户登陆.登陆名称
    FROM 用户登陆 INNER JOIN
          用户 ON 用户登陆.用户ID = 用户.用户ID
    WHERE 用户登陆.用户登陆ID=@LID AND 用户登陆.是否有效 = 1
    "
      ))
    {
    cmd.Parameters.Add("@LID",LogonID);
    using(SqlDataReader sdr=cmd.ExecuteReader())
    {
    if(!sdr.Read())
    throw(new Exception("没有登陆记录"));
    if(Password!=V.MD5(sdr["登陆密码"].ToString()))
    throw(new Exception("密码不同"));
    Session["MemberID"]=Convert.ToInt32(sdr["用户ID"]);
    }
    }
    }
    finally
    {
    Commit();
    }
    }
    }
    }using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    namespace Website
    {
    public class SiteMainPage : SitePage
    {
    public SiteMainPage()
    {
    Controls.Add(new LiteralControl(
    string.Format(
    @"<html><META content='text/html;charset=gb2312' http-equiv='Content-Type'>
    <script src='{0}/MainPage.js'><!--/**/--></script>
    <link rel='stylesheet' href='{0}/MainPage.css'/>
    <body scroll='auto'>
    <div style='width:984px;' align='center'>
    <table border=0 cellpadding=0 cellspacing=0 width='100%'>
    <tr><td>
    <div style='width:100%;height:24px;border:1px solid red;text-align:center'>PopupPage</div>
    </td></tr><tr><td>
    "
    ,V.GetResDir()
    )
    ));
    }
    override protected void OnInit(EventArgs e)
    {
    Controls.Add(new LiteralControl(
    @"
    </td></tr><tr><td>
    <div style='width:100%;height:24px;border:1px solid red;text-align:center'>PopupPage</div>
    </td></tr>
    </table>
    </body></html>"
    ));
    base.OnInit(e);
    }
    }
    }
    --其他的SitePopupPage,SiteModalPage类似.
      

  6.   

    应该没有问题的。
    一个aspx就是一个class。是class 当然可以派生或继承
      

  7.   

    WEBFORM窗体好象是不能被继承的
      

  8.   

    仅仅派生class没有用,aspx页面上必须要有控件描述信息,不然是空白的,我刚刚试了,继承下来的类只能继承逻辑,样式还是需要由aspx来描述
      

  9.   

    ..
    楼上。.你以为ASPX能做的,单纯写代码不能??ASPX不就是用代码写的么?例子:
    xxx.cs:using System;
    using System.Collections;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;using System.Data;
    using System.Data.SqlClient;
    namespace Website
    {
    public abstract class MyList:Page
    {
    protected DataGrid dg;
    public MyList()
    {
    Controls.Add(new LiteralControl(@"<HTML><TITLE>MYLIST</TITLE><BODY STYLE=background-color:ghostwhite;color:navy><DIV ALIGN=center>"));
    HtmlForm form=new HtmlForm();
    Controls.Add(form); dg=new DataGrid();
    dg.HeaderStyle.BackColor=Color.Orange;
    dg.HeaderStyle.ForeColor=Color.White;
    dg.AllowPaging=true;
    dg.PagerStyle.BackColor=Color.Tomato;
    dg.PagerStyle.ForeColor=Color.Cyan;
    dg.PageSize=4; form.Controls.Add(dg); Controls.Add(new LiteralControl(@"</DIV></BODY></HTML>"));
    }
    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    dg.PageIndexChanged+=new DataGridPageChangedEventHandler(dg_Page);
    }
    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    if(!IsPostBack)
    BindDG();
    }
    virtual protected void BindDG()
    {
    DataSet ds=new DataSet();
    using(SqlDataAdapter sda=new SqlDataAdapter(CommandText,"server=(local);database=northwind;trusted_connection=true"))
    {
    sda.Fill(ds);
    }
    dg.DataSource=new DataView(ds.Tables[0]);
    dg.DataBind();
    }
    virtual protected void dg_Page(object sender,DataGridPageChangedEventArgs e)
    {
    dg.CurrentPageIndex=e.NewPageIndex;
    BindDG();
    }
    abstract protected string CommandText
    {
    get;
    }
    }
    }
    ----employeeslist.aspx:<%@Page Inherits="Website.MyList"%>
    <script language=C# runat=server>
    override protected string CommandText
    {
    get
    {
    return "select firstname,lastname from employees";
    }
    }
    </script>
      

  10.   

    volitant2002(Volitant):你没看清题目,谁都知道你可以继承类,但人家要继承网页,而不是你写的Page类
      

  11.   

    我同意思归的看法,web窗体可以继承吗,不可想像
      

  12.   

    ...我那段是针对5drush写的嘛..下面是继承*.aspx的方法:using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.Collections;
    using System.Reflection;
    using System.Web;
    using System.Web.UI;using Microsoft.CSharp;namespace Website
    {
    public abstract class InheritsPage:Page,IHttpHandler
    {
    abstract protected string PageUrl
    {
    get;
    }
    abstract protected string GetCSharpCode(string BaseTypeFullName); static private Hashtable cachedTypes=new Hashtable(); private void ExecuteExists(string filedictname)
    {
    Type t=(Type)cachedTypes[filedictname];
    IHttpHandler handler=(IHttpHandler)Activator.CreateInstance(t);
    handler.ProcessRequest(Context);
    }
    void IHttpHandler.ProcessRequest(System.Web.HttpContext Context)
    {
    string requestfilepath=Context.Server.MapPath(PageUrl);
    if(!System.IO.File.Exists(requestfilepath))
    throw(new Exception(Context.Request.FilePath+"指定的PageUrl,不存在或..."));
    string filedictname=PageUrl+":"+System.IO.File.GetLastWriteTime(requestfilepath).Ticks.ToString()+":"+System.IO.File.GetLastWriteTime(Context.Request.PhysicalPath).Ticks.ToString();
    if(cachedTypes.Contains(filedictname))
    {
    ExecuteExists(filedictname);
    return;
    }
    lock(PageUrl)
    {
    if(cachedTypes.Contains(filedictname))
    {
    ExecuteExists(filedictname);
    return;
    }
    IHttpHandler hh;
    try
    {
    hh=PageParser.GetCompiledPageInstance(PageUrl,Context.Server.MapPath(PageUrl),Context);
    }
    catch(Exception x)
    {
    throw(new Exception(Context.Request.FilePath+"指定的PageUrl,不能加载",x));
    }
    Type hhType=hh.GetType();
    string typeName=hhType.FullName;
    string code=GetCSharpCode("C"+Math.Abs(DateTime.Now.Ticks)+":"+typeName);
    using(CSharpCodeProvider cp=new CSharpCodeProvider())
    {
    string filepath=System.IO.Path.GetTempPath()+"/asm"+DateTime.Now.Ticks.ToString()+".dll";
    ICodeCompiler cc=cp.CreateCompiler();
    CompilerParameters option=new CompilerParameters();
    foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    {
    option.ReferencedAssemblies.Add(asm.Location);
    }
    option.GenerateExecutable=false;
    option.GenerateInMemory=true;
    option.IncludeDebugInformation=false;
    option.OutputAssembly=filepath; CompilerResults cr=cc.CompileAssemblyFromSource(option,code); foreach(CompilerError error in cr.Errors)
    {
    if(!error.IsWarning)
    {
    throw(new Exception(error.ToString()));
    }
    } foreach(Type resType in cr.CompiledAssembly.GetTypes())
    {
    if(resType.IsSubclassOf(hhType))
    {
    try
    {
    IHttpHandler hh2=(IHttpHandler)Activator.CreateInstance(resType);
    cachedTypes.Add(filedictname,hh2.GetType());
    hh2.ProcessRequest(Context);
    return;
    }
    catch(Exception x)
    {
    throw(new Exception("类不能创建..",x));
    }
    return;
    }
    }
    throw(new Exception("没有继承类??"));
    }
    }
    }
    }
    }
    ----------------/truepage.aspx
    <%@Import Namespace="System.Data.SqlClient"%>
    <%@Import Namespace="System.Data"%>
    <script runat="server">
     
     virtual protected string CommandText
     {
    get{throw new NotImplementedException();}
     }
     
    override protected void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    if(!IsPostBack)
    BindDataGrid();
    }
    void BindDataGrid()
    {
    DataSet ds=new DataSet();
    using(SqlDataAdapter sda=new SqlDataAdapter(CommandText,"server=(local);database=northwind;trusted_connection=true"))
    {
    sda.Fill(ds);
    }
    DataGrid1.DataSource=new DataView(ds.Tables[0]);
    DataGrid1.DataBind();
    }
    void PageIndexChanged(object s,DataGridPageChangedEventArgs  e)
    {
    DataGrid1.CurrentPageIndex=e.NewPageIndex;
    BindDataGrid();
    }
    </script><form runat="server">
    <asp:DataGrid id=DataGrid1 OnPageIndexChanged="PageIndexChanged" runat="server" AllowPaging="True" BorderStyle="None" BorderWidth="1px" BorderColor="#CCCCCC" BackColor="White" CellPadding="3" Width="100%"> <FooterStyle ForeColor="#000066" BackColor="White"/>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"/>
    <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"/>
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"/>
    <ItemStyle ForeColor="#000066"/></asp:DataGrid></FORM>----------------------/truepage_child.aspx
    <%@Page Inherits="Website.InheritsPage" %>
    <script runat=server>
    override protected string PageUrl
    {
    get
    {
    return "/truepage.aspx";
    }
    }
    override protected string GetCSharpCode(string typeName)
    {
    return new StringBuilder()
    .Append(
    @"
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;public class 
    "
    )
    .Append(typeName)
    .Append(
    @"
    {
    override protected void OnInit(EventArgs e)
    {
    base.OnInit(e);
    DataGrid1.PageSize=4;//直接使用父类的DataGrid1字段.
    }
    override protected string CommandText //从父类重写
    {
    get
    {
    return ""select firstname,lastname,title from employees"";
    }
    }}"
    )
    .ToString();
    }
    </script>
      

  13.   

    虽然那段代码还有很多BUG..
    但是测试过。
    truepage_aspx的确生成了一个继承truepage.aspx的动态类,并且运行它的..
      

  14.   

    当然可以继承了,因这每个页面也是继承了Page的
      

  15.   

    我说的不是你这种情况,你将所有的界面都写进逻辑里面了,我说了逻辑可以被继承,但是与楼主要求的是两码事,昨天我问过Scott这个问题了,他给我的建议是采用用户控件(*.ascx)
      

  16.   

    to 5drush(五农民出狗狗) :
       用户控件可以实现这样吗?
       定义了一个.ascx文件,里面的button是定死的阿,如何在.ascx文件下面在加button或事件阿??就是怎么继承这个.ascx文件阿??