2015-08-28 73 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());