我遇到的问题是我使用母版页设置会话变量,然后使用这些会话变量来处理占位符中的内容。但是,会话变量未在内容中显示为已更新。我在下面做了一个小例子。
我需要在母版页和内容中更新会话变量。现在,好像内容代码隐藏在母版页之前或同时运行。我假设主页代码隐藏首先运行,从而设置会话变量。任何帮助将不胜感激。谢谢! (使用C#构建.NET 2.0)
testmass.master
<%@ Master Language="C#" inherits="gpworklist.testmass"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head runat="server">
</head>
<body>
<form id="testthisform">
<select id="test1sel" name="test1" runat="server"></select>
<select id="test2sel" name="test2" runat="server"></select>
<input type="submit" value="Submit" />
</form>
<div id="testsess" runat="server"></div>
<asp:ContentPlaceHolder id="cntnt_phldr" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
testmass.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace gpworklist
{
public class testmass: MasterPage
{
public string defProv;
public testmass()
{
}
public void Page_Load()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.MinValue);
HtmlSelect sel1 = (HtmlSelect)this.FindControl("ctl00$test1sel");
HtmlSelect sel2 = (HtmlSelect)this.FindControl("ctl00$test2sel");
HtmlGenericControl div = (HtmlGenericControl)this.FindControl("ctl00$testsess");
string def1;
string def2;
ListItem item;
if (Request["ctl00$test1sel"] != null && Request["ctl00$test1sel"] != "")
{
Session["test1"] = Request["ctl00$test1sel"];
}
if (Request["ctl00$test2sel"] != null && Request["ctl00$test2sel"] != "")
{
Session["test2"] = Request["ctl00$test2sel"];
}
if (Session["test1"] != null)
{
def1 = Session["test1"].ToString();
}
else
{
def1 = "";
}
if (Session["test2"] != null)
{
def2 = Session["test2"].ToString();
}
else
{
def2 = "";
}
for (int i = 0; i <= 10; i++)
{
item = new ListItem(i.ToString(), i.ToString());
if (i.ToString() == def1)
{
item.Selected = true;
}
sel1.Items.Add(item);
}
//Session["test1"] = sel1.Value;
for (int i = 0; i <= 10; i++)
{
item = new ListItem(i.ToString(), i.ToString());
if (i.ToString() == def2)
{
item.Selected = true;
}
sel2.Items.Add(item);
}
Session["test2"] = sel2.Value;
div.InnerHtml = Session["test1"] + " - " + Session["test2"];
}
}
}
testpage.aspx
<%@ Page Language="C#" MasterPageFile="testmass.master" Inherits="gpworklist.testpage"%>
<asp:content ID="contentstuff" contentplaceholderid="cntnt_phldr" runat="server">
<div id="testpagesess" runat="server"></div>
</asp:content>
testpage.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace gpworklist
{
public class testpage: Page
{
public testpage()
{
}
public void Page_Load()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.MinValue);
HtmlGenericControl div = (HtmlGenericControl)this.FindControl("ctl00$cntnt_phldr$testpagesess");
div.InnerHtml = Session["test1"] + " - " + Session["test2"];
}
}
}
答案 0 :(得分:2)
内容页面的事件在同一母版页的事件之前触发。 见http://msdn.microsoft.com/en-us/library/vstudio/dct97kc3(v=vs.100).aspx
答案 1 :(得分:1)
您应该使用Init
的{{1}}事件,而不是MasterPage
事件。这将在Load
之前调用。