Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, January 27, 2013 10:54 PM
Hello everyone,
I am setting up a basic Call/Email Me Back form for a website. I am using ASP.NET 4 with C#.
This is an aggravatingly simple task to accomplish in C#, but I must be overlooking something. All the other code snippet examples I found (including the similar discussion on this site) differ slightly from mine, and when I do modify my code to match that of an example, I still cannot get it to work right. So, here I am.
The basic idea is that the form allows the user to select a contact-back method of either a call back or a follow up email. They can select their desired choice by selecting the appropriate radio button. If they desire a call back, the email address textbox and label is disabled, and the phone number and contact time textbox and label are enabled. If they desire a follow up email, the opposite is true. I want the page to default to the follow up email option.
The code for my form is as follows:
<asp:RadioButton ID="rdoEmail" runat="server" Text="Email Me" GroupName="contactMethod" OnCheckedChanged="rdoEmail_CheckedChanged" />
<asp:RadioButton ID="rdoPhone" runat="server" Text="Call Me" GroupName="contactMethod" OnCheckedChanged="rdoPhone_CheckedChanged"/>
<br />
<asp:Label ID="lblEmailAddress" runat="server" Text="What is the email address we should use to contact you?"></asp:Label>
<br />
<asp:TextBox ID="txtEmailAddress" runat="server" Width="500px"></asp:TextBox>
<br />
<asp:Label ID="lblPhoneNumber" runat="server" Text="What phone number should we use to contact you?"></asp:Label>
<br />
<asp:TextBox ID="txtPhoneNumber" runat="server" Width="500px"></asp:TextBox>
<br />
<asp:Label ID="lblCallBackTime" runat="server" Text="What time would you like us to call you?"></asp:Label>
<br />
<asp:TextBox ID="txtCallBackTime" runat="server" Width="500px"></asp:TextBox>
My (now very bloated) C# code is as follows:
protected void Page_Load(object sender, EventArgs e)
{
rdoEmail.Checked = true;
lblPhoneNumber.Enabled = false;
txtPhoneNumber.Enabled = false;
lblCallBackTime.Enabled = false;
txtCallBackTime.Enabled = false;
lblEmailAddress.Enabled = true;
txtEmailAddress.Enabled = true;
}
protected void rdoEmail_CheckedChanged(object sender, EventArgs e)
{
if (rdoEmail.Checked == true)
{
lblPhoneNumber.Enabled = false;
txtPhoneNumber.Enabled = false;
lblCallBackTime.Enabled = false;
txtCallBackTime.Enabled = false;
lblEmailAddress.Enabled = true;
txtEmailAddress.Enabled = true;
}
else
{
lblPhoneNumber.Enabled = true;
txtPhoneNumber.Enabled = true;
lblCallBackTime.Enabled = true;
txtCallBackTime.Enabled = true;
lblEmailAddress.Enabled = false;
txtEmailAddress.Enabled = false;
}
}
protected void rdoPhone_CheckedChanged(object sender, EventArgs e)
{
if (rdoPhone.Checked == true)
{
lblPhoneNumber.Enabled = true;
txtPhoneNumber.Enabled = true;
lblCallBackTime.Enabled = true;
txtCallBackTime.Enabled = true;
lblEmailAddress.Enabled = false;
txtEmailAddress.Enabled = false;
}
else
{
lblPhoneNumber.Enabled = false;
txtPhoneNumber.Enabled = false;
lblCallBackTime.Enabled = false;
txtCallBackTime.Enabled = false;
lblEmailAddress.Enabled = true;
txtEmailAddress.Enabled = true;
}
I guess the most maddening aspect about this is that this is such a novice-level task, and I have done more complex things than this without this much hassle; and the fact that I probably looked right at the answer without realizing it.
I will tinker with it and if I happen to find the correct way to do this on my own I will post a follow up.
Thank you,
-Nate
All replies (3)
Monday, January 28, 2013 2:25 AM âś…Answered
Solved!
The problem with my above code samples was that as the end user would change their selection from either Phone or Email, the radio buttons were not set to AutoPostBack="True", therefore, the webpage did not "know" that the user had actually changed their selection. In addition to this, regardless of if I had AutoPostBack set to true or false, the C# code was not placed correctly to allow the dynamic change.
Now, had I stepped through the code line by line, I should have picked up on this a lot sooner than I did. Instead, I fumbled around using trial and error.
So, how did I correct the issue?
I needed the webpage to recognize that a change had been made in how the user would like to be contacted. Therefore, I put two radio buttons on my aspx page, and set AutoPostBack to true. I wanted the Email option selected by default, so I set the Checked property to true.
<asp:RadioButton ID="rdoEmail" Text="Email" runat="server" GroupName="grpContactMethod" AutoPostBack="True" Checked="True"/>
<asp:RadioButton ID="rdoPhone" Text="Phone" runat="server" GroupName="grpContactMethod" AutoPostBack="True" />
Now, the when the user initially selects or changes his or her desired preference, the page will post back. As this happens, the very firs place it looks in regards to your code behind file is the Page_Load event handler. What better place than to instruct the page what text boxes to enable and disable? That is where I will declare what I want to happen, as shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (rdoEmail.Checked == true)
{
txtEmailAddress.Enabled = true;
txtPhoneNumber.Enabled = false;
txtCallBackTime.Enabled = false;
}
else if (rdoPhone.Checked == true)
{
txtEmailAddress.Enabled = false;
txtPhoneNumber.Enabled = true;
txtCallBackTime.Enabled = true;
}
}
So now, low and behold, as the user makes his or her selection, the webpage will respond to his or her input exactly the way I want.
I swear, sometimes I am the smartest fool I know!
Monday, January 28, 2013 12:51 AM
In finding another example, I have modified my C# code to the following:
public partial class contactUs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rblContactMethod_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (rblContactMethod.SelectedItem.Text == "Email")
{
lblPhoneNumber.Enabled = false;
txtPhoneNumber.Enabled = false;
lblCallBackTime.Enabled = false;
txtCallBackTime.Enabled = false;
lblEmailAddress.Enabled = true;
txtEmailAddress.Enabled = true;
}
else if (rblContactMethod.SelectedItem.Text == "Phone")
{
lblPhoneNumber.Enabled = true;
txtPhoneNumber.Enabled = true;
lblCallBackTime.Enabled = true;
txtCallBackTime.Enabled = true;
lblEmailAddress.Enabled = false;
txtEmailAddress.Enabled = false;
}
}
}
In addition, the code on the webpage for this has also been modified as follows:
<asp:RadioButtonList ID="rblContactMethod" runat="server" OnCheckedChanged="radioButton_CheckChanged">
<asp:ListItem Selected="True">Email</asp:ListItem>
<asp:ListItem>Phone</asp:ListItem>
</asp:RadioButtonList>
Unfortunately, this still is not yielding the desired effect.
Monday, January 28, 2013 8:36 AM
Hi Nate,
Thank you for sharing your solution here.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.