单击图像按钮时设置会话变量?

发布于 2024-12-23 00:22:15 字数 1750 浏览 1 评论 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;
    }

I have two image buttons on the page. When either is clicked it needs to set the session variables yesID and NoId before the page is posted back. I tried putting the code in the click events for each button but the page loads again before it runs that code in the click events. How can i set the 2 session variables when either image is clicked?

  <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>

Code Behind Page

   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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深空失忆 2024-12-30 00:22:15

这个逻辑有帮助吗?

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"]);
}

Does this logic help?

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"]);
}
最佳男配角 2024-12-30 00:22:15

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

When you click on the asp.net image button, It usually fires the click event and calls the method if autopostback is not disabled. If you really want to change the session values before the autopostback being hit, Invoke a client side javascript function on the onclientclick event and from that, make an ajax call to a server page where you update the session variable values. Once you get the response from the ajax call (success event) , return true so that the web page continues with the form post back event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文