如何使用'input type =“从后面的代码提交”OnServerClick ='

时间:2010-07-01 15:41:20

标签: c# asp.net

解决IIS会话状态问题我找到了一个简单的页面用作测试。此页面有效。

<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'的第一个参数(你是否错过了使用指令或程序集引用?)

我需要做些什么来完成这项工作?

1 个答案:

答案 0 :(得分:1)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="YourPageName.aspx.cs" Inherits="ClassNameOfCodeBehindeWithFullNamespace" %>

以上需要在您的aspx页面上

<强>更新
您需要为方法添加protected修饰符。