2015-08-28 58 views
0

我有一个asp.net webform网页,其中有2个单选按钮,这些单选按钮选择的值存储在session,并显示我的确认页面上。当用户点击提交按钮时,会发送一封包含所有详细信息的电子邮件(同样所有详细信息均以session显示)。传递会话价值的另一个阶段价值

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

我尝试添加以下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(); 

我想填充我的“会话[” PrefContactRadioButton“]”与“会话[” PhoneRadioButton“] /会话值[ “EmailRadioButton”]”。

+0

“中断”的行似乎没有使用前面的代码中的任何Session变量。 – Magnus

+0

@Magnus对不起,我把这条线注释掉了,并且改变了我的会话名称,但是我已经更新了打断它的那一行 – murday1983

+0

它究竟打破了什么?你得到的错误是什么?我仍然不明白你的问题。 – Magnus

回答

0

不能近一个月的努力排序了这一点后相信的,它只是来找我,只要使用同一个名字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> 

而且finaly更新我的确认页电子邮件正文

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

瞧它作品。

0

这行看起来很危险,我:

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

如果您在字符串连接使用三元运算符,放在括号周围。

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