将一个会话值传递给另一个会话值

Pass A Session Value To Another Session Value

我有一个 asp.net webform 页面,其中有 2 个单选按钮,这些单选按钮将所选值存储在 session 中,并显示在我的确认页面上。当用户单击提交按钮时,将发送一封包含所有详细信息的电子邮件(所有详细信息同样由 session 显示)。

我在获取电子邮件中所选单选按钮的值时遇到问题。我能看到的唯一选择是有 2 个相同的字段标签,其中一个为空,另一个显示会话中的选定值。

我尝试将以下 IF 添加到我的电子邮件正文中,但它从我的电子邮件中删除了所有详细信息。

我想我可以在我的 .cs 文件中使用 IF statement 来检查其中一个单选按钮不是 null,然后创建另一个 session variable。我不知道如何传递 IF 条件中的 session 值来填充新的 session.

HTML 单选按钮打开的页面

<div class="form-group">
     <asp:Label ID="PrefContactLabel" class="col-sm-3 control-label" runat="server" Text="Preferred method of contact *" style="padding-top: 0px"></asp:Label>
     <div class="col-sm-3">
          <asp:Label runat="server" class="radio-inline" style="padding-top: 0px">
               <asp:RadioButton runat="server" id="PhoneRadioButton" value="Phone" GroupName="prefcontact"/> Phone
          </asp:Label>
               <asp:Label runat="server" class="radio-inline" style="padding-top: 0px">
               <asp:RadioButton runat="server" id="EmailRadioButton" value="Email" GroupName="prefcontact"/> Email
          </asp:Label>
     </div>
     <div class="col-sm-offset-3 col-sm-9">
          <asp:CustomValidator id="custPrefContact" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please select a preferred method of contact." ClientValidationFunction="PrefContact_ClientValidate" OnServerValidate="PrefContact_ServerValidate" />
     </div>
</div>

打开单选按钮的页面隐藏代码

protected void Step01SubmitButton_Click(object sender, EventArgs e)
{
     Session["PhoneRadioButton"] = PhoneRadioButton.Checked ? "Phone" : "";
     Session["EmailRadioButton"] = EmailRadioButton.Checked ? "Email" : "";
}

这是我添加到上述代码中的 IF 语句的开始

if (Session["PhoneRadioButton"].ToString() == "Phone" || Session["EmailRadioButton"].ToString() == "Email")
{
     Session["PrefContactRadioButton"] = Step01HiddenPrefField.Text;
}

这是在我的确认页面上破坏我的电子邮件的代码行

Label4.Text + " " + Session["PhoneRadioButton"].ToString() == "Phone" ? Session["PhoneRadioButton"].ToString() : Session["EmailRadioButton"].ToString();

我想用 "Session["PhoneRadioButton"]/Session["EmailRadioButton"]".

中的值填充我的 "Session["PrefContactRadioButton"]"

这条线对我来说很危险:

Label4.Text + " " + Session["PhoneRadioButton"].ToString() == "Phone" ? Session["PhoneRadioButton"].ToString() : Session["EmailRadioButton"].ToString();

如果您在字符串连接中使用三元运算符,请在其两边加上括号。

Label4.Text + " " + (Session["PhoneRadioButton"].ToString() == "Phone" ? Session["PhoneRadioButton"].ToString() : Session["EmailRadioButton"].ToString());

无法相信经过将近一个月的尝试解决这个问题,我突然想到,只需为两个单选按钮使用相同的 session 名称,然后猜猜它的作用。

已更新 .cs 代码

    if (PhoneRadioButton.Checked)
    {
        Session["PrefContactRadioButton"] = PhoneRadioButton.Checked ? "Phone" : "";
    }
    else if (EmailRadioButton.Checked)
    {
        Session["PrefContactRadioButton"] = EmailRadioButton.Checked ? "Email" : "";
    }

然后更新我的确认页面以调用我的新 session 变量

    <div class="form-group">
        <asp:Label ID="Step01ContactMethod" class="col-xs-6 col-sm-3 control-label" runat="server" Text="Preferred method of contact:"></asp:Label>
        <div class="col-xs-6 col-sm-9 form-control-static">                            
            <%=Session["PrefContactRadioButton"] %>
        </div>
    </div>

最后更新了我的确认页面电子邮件正文

msg.Body = Step01ContactMethod.Text + " " + Session["PrefContactRadioButton"].ToString();

瞧,它起作用了。