I'm creating a web form, and I'd like the users to be able to check a radio button to indicate whether or not they are a company employee. If the user is an employee, I'd like a textbox that I'm using to input the employee ID to become writeable and to change the background color from gray to white--vice versa for a 'No' indication. I've felt that posting back to do this and making the user endure a page refresh is a bit much. As such, I wanted to use an UpdatePanel. I used to roll my own async functions with Java servlets, javascript, and html. Then, we switched to Silverlight, for which the datalayer was inherently asynchronous. However, I'm VERY new to .NET Ajax.
I have the following in my XAML and code behind (see below my description), and the event handlers in my code behind simply change the Color and ReadOnly attributes of the textbox. However, when I run the code and click the radiobuttons, I end up with a second textbox on the page (with background color and read/write settings set appropriately according to the radio button checked). As I toggle between the radio buttons, each subsequent time I click the "No" radio button, a comma is concatenated to the Text Attribute of the textbox control, until, finally, I get a script error. The behavior makes me think I'm generating a null pointer that eventually ends up attempting to write to a "dangerous" memory location, thus causing a crash. I'm just not sure what's going on here or what I'm doing wrong. Any help is much appreciated.
TIA,
MC
<table>
<tr>
<td align='left'>
<b>Are you a [Company Name] Employee ?</b>
</td>
<asp:UpdatePanel runat="server" ID="UpdatePanelEmpIDBox" UpdateMode="Conditional">
<ContentTemplate>
<td align="left">
<asp:RadioButton runat='server' ID='rbEmployeeYes' GroupName='EmployeeRadioButtons' OnCheckedChanged="rbEmployeeYes_CheckedChanged" AutoPostBack="true" />
<b>Yes</b>
<asp:RadioButton runat='server' ID='rbEmployeeNo' GroupName='EmployeeRadioButtons' OnCheckedChanged="rbEmployeeNo_CheckedChanged" AutoPostBack="true" />
<b>No</b>
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="UpdatePanelEmpIDBox2" UpdateMode="Conditional">
<ContentTemplate>
<tr>
<td align='left'>
<b>If yes, please enter your Employee ID #: </b>
</td>
<td align='left'>
<asp:TextBox ID="tbEmpID" runat="server" BorderColor="Black" BorderStyle="Groove" BackColor="Gray" OnTextChanged="tbEmpID_TextChanged" AutoPostBack="true" ReadOnly="true" />
</td>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rbEmployeeYes" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="rbEmployeeNo" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
</tr>
</table>
Code behind:
protected void rbEmployeeYes_CheckedChanged(object sender, EventArgs e)
{
if (rbEmployeeYes.Checked == true)
{
tbEmpID.ReadOnly = false;
tbEmpID.BackColor = System.Drawing.Color.White;
}
else
{
tbEmpID.ReadOnly = true;
tbEmpID.BackColor = System.Drawing.Color.Gray;
}
}
protected void rbEmployeeNo_CheckedChanged(object sender, EventArgs e)
{
if (rbEmployeeNo.Checked == true)
{
tbEmpID.ReadOnly = true;
tbEmpID.BackColor = System.Drawing.Color.Gray;
}
else
{
tbEmpID.ReadOnly = false;
tbEmpID.BackColor = System.Drawing.Color.White;
}
}