0

我在页面上有两个图像按钮。单击其中一个时,需要在回发页面之前设置会话变量 yesID 和 NoId。我尝试将代码放入每个按钮的单击事件中,但页面在单击事件中运行该代码之前再次加载。单击任一图像时,如何设置 2 个会话变量?

  <div id="MainPics">
        <div id="RightPic">
            <p>
                <asp:Label ID="FirstPicMemberNameLabel" runat="server" Text="" Font-Bold="True" ForeColor="White"></asp:Label>
            </p>
            <asp:ImageButton ID="FirstPicLink" Width="90%" runat="server" 
                onclick="FirstPicLink_Click" />
        </div>
        <div id="LeftPic">
            <p>
                <asp:Label ID="SecondPicMemberNameLabel" runat="server" Text="" ForeColor="White" Font-Bold="True"></asp:Label>
            </p>
            <asp:ImageButton ID="SecondPicLink" Width="90%" runat="server" 
                onclick="SecondPicLink_Click" />
        </div>
        <div id="skip">
            <asp:LinkButton ID="LBNoChoice" PostBackUrl="~/default.aspx" ForeColor="White" runat="server">Skip - I Can't Choose</asp:LinkButton>
        </div>
    </div>

页面背后的代码

   protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            Session["yesID"] = 0;
            Session["noId"] = 0;
        }
        else
        {
            //Send Session Variables to Database for Storage.

        }    
 }

  protected void FirstPicLink_Click(object sender, ImageClickEventArgs e)
    {
        Session["yesID"] = 1;
        Session["noId"] = 2;
    }

    protected void SecondPicLink_Click(object sender, ImageClickEventArgs e)
    {
        Session["yesID"] =2;
        Session["noId"] = 1;
    }
4

2 回答 2

1

这个逻辑有用吗?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["yesID"] = 0;
        Session["noId"] = 0;
    }
    //else
    //{
        //Send Session Variables to Database for Storage.
    //}
}

protected void FirstPicLink_Click(object sender, ImageClickEventArgs e)
{
    Session["yesID"] = 1;
    Session["noId"] = 2;

    SendSessionVariables(Session["yesID"], Session["noId"]);
}

private void SendSessionVariables(object p, object p_2)
{
    // Your database code here
}

protected void SecondPicLink_Click(object sender, ImageClickEventArgs e)
{
    Session["yesID"] = 2;
    Session["noId"] = 1;

    SendSessionVariables(Session["yesID"], Session["noId"]);
}
于 2011-12-26T01:36:06.843 回答
0

当您单击 asp.net 图像按钮时,它通常会触发单击事件并在未禁用自动回发时调用该方法。如果您真的想在自动回发被击中之前更改会话值,请在 onclientclick 事件上调用客户端 javascript 函数,然后对服务器页面进行 ajax 调用,您可以在其中更新会话变量值。从 ajax 调用(成功事件)获得响应后,返回 true 以便网页继续执行表单回发事件。

于 2011-12-26T01:22:53.850 回答