可以,你可以在母窗体中设计公用的界面和控件
然后子窗体从这个母窗体派生,(注:如果母窗体的控件不是通过编码写出来,而是在Aspx文件当中的,那么子窗体必须保证在Aspx文件中有这些控件)母窗体可以把控件声明成protected,然后可以写这些控件的事件,子窗体只要不重新声明控件就可以公用这些事件代码。
然后子窗体从这个母窗体派生,(注:如果母窗体的控件不是通过编码写出来,而是在Aspx文件当中的,那么子窗体必须保证在Aspx文件中有这些控件)母窗体可以把控件声明成protected,然后可以写这些控件的事件,子窗体只要不重新声明控件就可以公用这些事件代码。
解决方案 »
- asp.net mvc
- 为什么使用 AutoPostBack之后页面呈现空白
- 如何修改Oracle数据表中的字段类型?
- .net中实现对数据库的备份和还原
- 问一个Page.IsPostBack,请高手指点
- 大家帮我看看我的这个Oracle存储过程,怎么会报对象必须实现 IConvertible的错误
- 启动时出错,帮我看看这个出错提示是什么意思?
- 在打开包括MediaPlayer控件的页面时,网页总是死,只有再打开一次才行。
- 怎样将C#和VB做的工程融合到一个工程中?
- asp.net启动word的奇怪问题?(原来好用,突然不行了)
- 极具魅力的题目!千万不能错过哦!高手才行啊!!
- .net与oracle 9i连接问题,急!
http://aspalliance.com/PaulWilson/Articles/?id=1
Have you tried it? can you show us an example on how to subclass an aspx 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类。那可真的没有必要大费工夫。
http://expert.csdn.net/Expert/TopicView1.asp?id=1299781
http://expert.csdn.net/Expert/TopicView1.asp?id=1488797
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类似.
一个aspx就是一个class。是class 当然可以派生或继承
楼上。.你以为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>
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>
但是测试过。
truepage_aspx的确生成了一个继承truepage.aspx的动态类,并且运行它的..
用户控件可以实现这样吗?
定义了一个.ascx文件,里面的button是定死的阿,如何在.ascx文件下面在加button或事件阿??就是怎么继承这个.ascx文件阿??