解決IIS會話狀態問題我發現一個簡單的頁面用作測試。此頁面有效。如何使用'input type =「從代碼隱藏中提交」OnServerClick ='
<body>
<form id="form1" runat="server">
<div>
<input type="text"
id="text1"
runat="server"
/>
<input type="Submit"
id="Submit1"
OnServerClick="Session_Add"
value="Add to Session State"
runat="server"
/>
<input type="Submit"
id="Submit2"
OnServerClick="CheckSession"
value="View Session State"
runat="server"
/>
<hr size="1"/>
<font size="6"/><span id="span1" runat="server"/>
</div>
</form>
<script language="C#" runat="server">
void Session_Add(Object sender, EventArgs e)
{
Session["MySession"] = text1.Value;
span1.InnerHtml = "Session data updated! <P> Your session contains: <font color=red>" +
Session["MySession"].ToString() + "</font>";
}
void CheckSession(Object sender, EventArgs e)
{
if (String.IsNullOrEmpty(Session["MySession"].ToString()))
{
span1.InnerHtml = "NOTHING, SESSION DATA LOST!";
}
else
span1.InnerHtml = "Your session contains: <font color=red>" +
Session["MySession"].ToString() + "</font>";
}
</script>
對於比我想那樣做,其他沒有特別的原因,我想在腳本塊的部分移動到代碼隱藏文件。該代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
Submit1.ServerClick += new System.EventHandler(this.Session_Add);
Submit2.ServerClick += new System.EventHandler(this.CheckSession);
}
void Session_Add(Object sender, EventArgs e)
{
Session["MySession"] = text1.Value;
span1.InnerHtml = "Session data updated! <P> Your session contains: <font color=red>" +
Session["MySession"].ToString() + "</font>";
}
void CheckSession(Object sender, EventArgs e)
{
if (String.IsNullOrEmpty(Session["MySession"].ToString()))
{
span1.InnerHtml = "NOTHING, SESSION DATA LOST!";
}
else
span1.InnerHtml = "Your session contains: <font color=red>" +
Session["MySession"].ToString() + "</font>";
}
}
編譯器錯誤消息:CS1061:「ASP.webform1_aspx」不包含關於「Session_Add」的定義和沒有擴展方法「Session_Add」接受型的第一參數「ASP.webform1_aspx '可以找到(是否缺少using指令或程序集引用?)
我必須做的,使這項工作?
不要以爲你可以做到這一點。 嘗試將其更改爲asp:按鈕服務器控件(以便正確連接事件處理程序)。 – RPM1984 2010-07-02 02:04:26