3
Answers

RadioButtonList.SelectedIndexChanged not raised when the ListItem has Selected="True" not working on IE8

Ajay Gautam

Ajay Gautam

14y
8.3k
1
I have problem with radiobuttonlist.

I have two radiobuttons in radiobuttonlist and based on selection, i am enabling and disabling the text box.

First time i used date radiobutton and i took date in corresponding textbox and i saved it into database and then when i came back on same page i saw the date radiobutton is checked has proper value then i clicked on another radiobutton and this time page is postback and clear my all the data which was in my other controls(I have 5-6 text boxed on my page). I dont know why?, It supposed to select my other radiobutton and enable the corresponding text box

On firefox it is working fine.

This is my code:-
<tr>

<th style="height: 57px">
<asp:RadioButtonList id="rbExpiration" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChangedMethod"
Tooltip="Please select either Expiration (In Days) or Expiration Date "
Height="42px" Width="167px">
<asp:ListItem Text="Expiration (In Days)" Value="rbExpirationInDays" Selected="True"/>
<asp:ListItem Text="Expiration Date" Value="rbExpirationDate" />
</asp:RadioButtonList>
</th>


<td style="height: 57px">
<br />
<asp:TextBox ID="txtExpirationDays" runat="server" CssClass="txtRequired" MaxLength="4"
ToolTip="Number of days until this credential will expire after submission"
Width="40px" ValidationGroup="vModifyCredType"></asp:TextBox>
 
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtExpirationDays"
Display="Dynamic" ErrorMessage="Expiration (in days) is required." Font-Size="8pt"ValidationGroup="vModifyCredType"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtExpirationDays"
Display="Dynamic" ErrorMessage="Please enter an integer value greater than 0 and less than 10,000."
Font-Size="8pt" SetFocusOnError="True" Type="Integer" MinimumValue="0" MaximumValue="9999"
ValidationGroup="vModifyCredType"></asp:RangeValidator>
<br />

<asp:TextBox ID="txtExpirationDate" runat="server" CssClass="txtRequired" MaxLength="10" ToolTip="Expiration date for this credential will expire after submission" Width="80px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtExpirationDate"
Display="Dynamic" ErrorMessage="Expiration date is required." Font-Size="8pt"
ValidationGroup="vModifyCredType"></asp:RequiredFieldValidator>
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtExpirationDate"
Format="MM/dd/yyyy" CssClass="smcalendar" Enabled="True" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtExpirationDate"
Display="Dynamic" ErrorMessage="Invalid date. Select a date or enter in the format mm/dd/yyyy"
Font-Size="Small"
ValidationExpression="([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20|21|22|23|24|25)\d\d"></asp:RegularExpressionValidator>
</td>
</tr>

CS Code: -
Page_Load()
if (!Page.IsPostBack)
{
if (gvCredentialTypes.SelectedIndex >= 0)
{
CredentialType credType = CredentialType.FindCredentialType(gvCredentialTypes.SelectedValue.ToString());
if (!String.IsNullOrEmpty(credType.CredentialTypeName)) //If exists, load data
{
String expDays = credType.ExpirationLength.ToString();
DateTime expDate = Convert.ToDateTime(credType.ExpirationDate);
// Assign the value to the controls
LoadExpirationDaysDateValue(expDays, expDate);
}
}
}

private void LoadExpirationDaysDateValue(String expDays, DateTime expDate)
{
// Determines whether value for Days or Date is present
Boolean IsDays = false;

// Check if days value is present
if (String.IsNullOrEmpty(expDays) || expDays.Equals("0"))
{
txtExpirationDays.Text = String.Empty;
}
else
{
txtExpirationDays.Text = expDays;
IsDays = true;
}

if (expDate.ToShortDateString().Equals ("1/1/0001"))
{
txtExpirationDate.Text = String.Empty;
}
else
{
txtExpirationDate.Text = expDate.ToShortDateString();
IsDays = false;
}

// Set the radio button value based on whether Date or Days value is present
if (IsDays)
{
// Get ref to the 1st radio item
ListItem liExpirationInDays = rbExpiration.Items.FindByValue("rbExpirationInDays");
if (liExpirationInDays != null)
{
liExpirationInDays.Selected = true;
rbExpiration.Items.FindByValue("rbExpirationDate").Selected = false;
txtExpirationDate.Text = string.Empty;
txtExpirationDays.Text = expDays;

// Enable or disable the controls based on the radio button selection
txtExpirationDays.Enabled = true;
RequiredFieldValidator1.Enabled = true;
RangeValidator1.Enabled = true;

txtExpirationDate.Enabled = false;
RegularExpressionValidator1.Enabled = false;
CalendarExtender2.Enabled = false;
//compDateValidator.Enabled = false;
RequiredFieldValidator2.Enabled = false;
}
}
else
{
// Get ref to the 2nd radio item
ListItem liExpirationDate = rbExpiration.Items.FindByValue("rbExpirationDate");
if (liExpirationDate != null)
{
liExpirationDate.Selected = true;
rbExpiration.Items.FindByValue("rbExpirationInDays").Selected = false;
txtExpirationDays.Text = string.Empty;
txtExpirationDate.Text = expDate.ToShortDateString();

// Enable or disable the controls based on the radio button selection
txtExpirationDate.Enabled = true;
RegularExpressionValidator1.Enabled = true;
RequiredFieldValidator2.Enabled = true;
CalendarExtender2.Enabled = true;
//compDateValidator.Enabled = true;

txtExpirationDays.Enabled = false;
RequiredFieldValidator1.Enabled = false;
RangeValidator1.Enabled = false;
}
}
}
private void EnableCorrespondingTextbox()
{
switch (rbExpiration.SelectedItem.Value)
{
case "rbExpirationInDays":
txtExpirationDays.Enabled = true;
RequiredFieldValidator1.Enabled = true;
RangeValidator1.Enabled = true;
txtExpirationDate.Text = String.Empty;
txtExpirationDate.Enabled = false;
RegularExpressionValidator1.Enabled = false;
CalendarExtender2.Enabled = false;
RequiredFieldValidator2.Enabled = false;
//compDateValidator.Enabled = false;
break;

case "rbExpirationDate":
txtExpirationDate.Enabled = true;
RegularExpressionValidator1.Enabled = true;
RequiredFieldValidator2.Enabled = true;
CalendarExtender2.Enabled = true;
//compDateValidator.Enabled = true;
txtExpirationDays.Text = String.Empty;
txtExpirationDays.Enabled = false;
RequiredFieldValidator1.Enabled = false;
RangeValidator1.Enabled = false;
break;
}

}
protected void OnSelectedIndexChangedMethod(object sender, EventArgs e)
{
EnableCorrespondingTextbox();
}
Answers (3)