我是指aspx页面,类似Delphi中的窗体继承。如果可以实现,能否给出示例?

解决方案 »

  1.   

    在基于Window界面的应用中,例如在C#里,有一个添加继承窗体的选项,在ASP.net里如何继承已存在的Web窗体?
      

  2.   

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

  3.   

    aspx页面继承,我试过一次,没有成功。特来学习
      

  4.   

    可以,所以后台代码用vc.net写都可以
      

  5.   

    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>