单击图像按钮时设置会话变量?
我的页面上有两个图像按钮。当单击其中任何一个时,需要在页面回发之前设置会话变量 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个逻辑有帮助吗?
Does this logic help?
当您单击 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.