Sunday, March 11, 2012

RadioButtonList Within AJAX UpdatePanel is not firing SelectedIndexChage Event

Try the following code

<%@. Page Language="C#" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server"
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedItem.Value == "Yes")
{
DropDownList1.Enabled = true ;
TextBox1.Enabled = true ;
}else if(RadioButtonList1.SelectedItem.Value == "No")
{
DropDownList1.Enabled = false;
TextBox1.Enabled = false;
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue;
}
</script
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManagerID="ScriptManager1"runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Height="232px"Width="456px">
<asp:RadioButtonList ID="RadioButtonList1" runat="server"OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
Width="140px" AutoPostBack="True">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="DropDownList1" runat="server" Enabled="False"Width="142px"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"AutoPostBack="True">
<asp:ListItem>test1</asp:ListItem>
<asp:ListItem>test2</asp:ListItem>
<asp:ListItem>test3</asp:ListItem>
<asp:ListItem>test4</asp:ListItem>
<asp:ListItem>test5</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"Enabled="False"></asp:TextBox></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


Thanksanishdevasia,

But I do not see any difference in your code over mine, except yours is in C# and I am using VB.

The problem is when I change from default "No" in the RadioButtonList to "Yes" my other controls become enabled, but if a user changes their mind and changes their selection on the RadioButtonList back to "No" after changing to "Yes" my controls do not Disable like my code behind instructs.

If I have these controls outside my UpdatePanel they work perfectly.


Ok I got it working, for some reason if I change my code from SelectedValue toSelectedItem.Value the controls will enable and disable if a user changes their selection multiple times, not sure why but it works.

Thanks,

No comments:

Post a Comment