Can You Use An If Statement For .Body For Email Functionality Onclick Of A Button

末鹿安然 提交于 2020-02-08 04:12:07

问题


I have a button on a page when sends the details to me in an email. This is working fine but i have an issue as one of the questions during my process is a raido button selection. The selected option is working fine on my confirmation page but as its a radio button i'll need an if statement for my email as one of the id's will be blank.

Does anyone know how i can do this.

Below is the code for my .body of the email

msg.Body = Label1.Text + " " + Session["pg1input"].ToString()
           + Environment.NewLine.ToString() +
           Label2.Text + " " + Session["pg1dd"].ToString()
           + Environment.NewLine.ToString() +
           Label3.Text + " " + Session["pg2"].ToString()
           + Environment.NewLine.ToString() +
           Label4.Text + " " + Session["pg2Yes"].ToString()
           + Environment.NewLine.ToString() +
           Label4.Text + " " + Session["pg2No"].ToString();

From the code above, the following is the radio button row so either 'pg2Yes' will be blank or 'pg2No' will be blank depending on the radio button selected

Label4.Text + " " + Session["pg2Yes"].ToString()
+ Environment.NewLine.ToString() +
Label4.Text + " " + Session["pg2No"].ToString();

Id prefer if i just have one label4 displayed and the radio button selected.

I did try

Label4.Text + " " + Session["pg2Yes"].ToString() + Session["pg2No"].ToString();

But this casued the email not to be sent.

The HTML below is what i have used on the confirmation page to display the relevent radio button selected

<div class="form-group">
     <div class="col-xs-12">
          <asp:Label ID="Label4" class="col-md-3 control-label" runat="server" Text="Kids?"></asp:Label>
          <div class="col-md-3 form-control-static">                            
               <% if (Session["pg2Yes"] == "Yes")
               {%>
                    <%=Session["pg2Yes"] %>
               <%}

              if (Session["pg2No"] == "No")
              {%>
                    <%=Session["pg2No"] %>
              <%} %>
          </div>
      </div>
 </div>

回答1:


You can use the ?: conditional operator to set which radio button value you intend to concat with the Label4.Text.

Label4.Text + " " + Session["pg2Yes"].ToString() == "Yes" 
    ? Session["pg2Yes"].ToString() 
    : Session["pg2No"].ToString();



回答2:


Can't belive after nearly a month of trying sort this out, its just come to me, Just use the same session name for both radio buttons, and guess what it worked.

Updated .cs code

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

Then updated my confirmation page to call my new session variable

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

And finaly updated my confirmation page email body

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

And voila it works.



来源:https://stackoverflow.com/questions/31747419/can-you-use-an-if-statement-for-body-for-email-functionality-onclick-of-a-butto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!