积极答复者
ASPX页面的生命周期中,给页面title添加文本的代码应该放到哪个生命周期方法中?

问题
-
我自定义了基页,基页检索页面的title是否为默认或者空,如果是则抛出异常。
代码片段如下:
if (this.Title == "Untitled Page" || this.Title == string.Empty)
{
throw new Exception("Page title cannot be \"Untitled Page\" or null.");
}
我新建一个页面基于此基页,在页面的code Behind里从LINQ数据对象提取文本作为页面的title
代码片段如下:
protected void Page_Load(object sender, EventArgs e)
{
int reviewId = Convert.ToInt32(Request.QueryString.Get("ReviewId"));
Review myReview;
using (PlanetWroxDataContext db = new PlanetWroxDataContext())
{
myReview = (from r in db.Review
where r.Id == reviewId
select r).Single();
lblTitle.Text = myReview.Title;
this.Title = myReview.Title;
}
可是运行报错:
Page title cannot be "Untitled Page" or null.
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Exception: Page title cannot be "Untitled Page" or null.
解决这个问题从哪里入手?我已经确定从数据库中成功取出字段。
我最后再补充一下, 我将页面的title修改成Untitled Page运行正常,但是空着就不行。
答案
-
Page_load里设置应当是可以的
关键是myReview.Title;是否有值。你可以这样测试
this.Title ="要覆盖的值";
using (PlanetWroxDataContext db = new PlanetWroxDataContext())
{
myReview = (from r in db.Review
where r.Id == reviewId
select r).Single();
lblTitle.Text = myReview.Title;
this.Title = myReview.Title;
}
另外一个问题就是:你的
if (this.Title == "Untitled Page" || this.Title == string.Empty)
{
throw new Exception("Page title cannot be \"Untitled Page\" or null.");
}
在什么位置判断的,如果在你进行判断的时候还没有赋值,则肯定是会报错了,你保证你的this.Title == "Untitled Page" || this.Title == string.Empty判断不能早于赋值
孟宪会- 已标记为答案 Jarone 2009年3月16日 4:02
-
这么写没有问题啊
public class MyPage : Page { protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (this.Title == "Untitled Page" || this.Title == string.Empty) { throw new Exception("Page title cannot be \"Untitled Page\" or null.您所请求的页面不符合规格。"); } } }
孟宪会- 已标记为答案 Jarone 2009年3月16日 3:59
全部回复
-
Page_load里设置应当是可以的
关键是myReview.Title;是否有值。你可以这样测试
this.Title ="要覆盖的值";
using (PlanetWroxDataContext db = new PlanetWroxDataContext())
{
myReview = (from r in db.Review
where r.Id == reviewId
select r).Single();
lblTitle.Text = myReview.Title;
this.Title = myReview.Title;
}
另外一个问题就是:你的
if (this.Title == "Untitled Page" || this.Title == string.Empty)
{
throw new Exception("Page title cannot be \"Untitled Page\" or null.");
}
在什么位置判断的,如果在你进行判断的时候还没有赋值,则肯定是会报错了,你保证你的this.Title == "Untitled Page" || this.Title == string.Empty判断不能早于赋值
孟宪会- 已标记为答案 Jarone 2009年3月16日 4:02
-
仍然没有想明白这个问题。
if (this.Title == "Untitled Page" || this.Title == string.Empty)
{
throw new Exception("Page title cannot be \"Untitled Page\" or null.");
}
上面这个代码片段在基页的 Page_PreRender方法里。
而我按照您的建议在测试页面的 Code Behind 里的 Page_Load 方法里写如下代码片段:
this.Title ="TestTitle";
using (PlanetWroxDataContext db = new PlanetWroxDataContext())
{
myReview = (from r in db.Review
where r.Id == reviewId
select r).Single();
lblTitle.Text = myReview.Title;
this.Title = myReview.Title;
}
结果仍然是抛出基页里定义好的异常。
因为运行前测试页面的 Title = "" , 只要添上随便的一个值就运行正常了。
测试页面正常显示了从LINQ对象里得到的Title内容,这就排除了LINQ对象为空值的推测。
我很不理解这种现象。我去查了查ASP.NET页面的生命周期。
Page_Load方法是在Page_PreRender方法之前被服务器执行的。
按常理运行时应该是先执行Page_Load方法给Title赋值,然后执行Page_PreRender方法判断Title的值然后呈现页面。
为什么我在Page_Load方法里定义Title会失效呢?
我补充完整方法代码:
基页的Page_PreRender方法代码:
private void Page_PreRender(object sender, EventArgs e)
{
if (this.Title == "Untitled Page" || this.Title == string.Empty)
{
throw new Exception("Page title cannot be \"Untitled Page\" or null.您所请求的页面不符合规格。");
}
}
测试页面的Page_Load方法代码:
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "TestTitle";
int reviewId = Convert.ToInt32(Request.QueryString.Get("ReviewId"));
Review myReview;
using (PlanetWroxDataContext db = new PlanetWroxDataContext())
{
myReview = (from r in db.Review
where r.Id == reviewId
select r).Single();
lblTitle.Text = myReview.Title;
lblSummary.Text = myReview.Summary;
lblBody.Text = myReview.Body;this.Title = myReview.Title;
测试页面的头部代码:
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MasterPage.master" AutoEventWireup="true" CodeFile="ViewDetails.aspx.cs" Inherits="ViewDetails" %>
如果我将上面的代码里的 Title 赋予一个默认值,比如"Untitled Page" 就能正常运行了,否则抛出我在Page_PreRender方法里定义的异常
问题虽然通过这种方式解决了,但是仍然想不明白这个现象,到底我哪里有错误? -
这么写没有问题啊
public class MyPage : Page { protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (this.Title == "Untitled Page" || this.Title == string.Empty) { throw new Exception("Page title cannot be \"Untitled Page\" or null.您所请求的页面不符合规格。"); } } }
孟宪会- 已标记为答案 Jarone 2009年3月16日 3:59