Reputation: 129
There are DropDownList
and CheckBoxList
in the form.
The value in the DropDownList should to change depending on the choice CheckBoxList
The Value in the DropDownList changes when you select (click) the element,
and if uncheck back doesn't change (it remains as high)
aspx:
Risk:
<asp:UpdatePanel ID="UpRisk" runat="server" UpdateMode="always">
<ContentTemplate>
<asp:DropDownList ID="ddlRiskLevel" runat="server" Enabled="false" AutoPostBack="True">
<asp:ListItem Text="Low" Value="1" Selected="True" />
<asp:ListItem Text="High" Value="2" />
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel24" runat="server" UpdateMode="always">
<ContentTemplate>
Sign:
<asp:CheckBoxList ID="chbList_Risks" runat="server">
<asp:ListItem> Sign1 </asp:ListItem>
<asp:ListItem> Sign2 </asp:ListItem>
<asp:ListItem> Sign3 </asp:ListItem>
</asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
Code:
protected void Page_Load(object sender, EventArgs e)
{
foreach (ListItem item in chbList_Risks.Items)
{
if (item.Selected)
ddlRiskLevel.SelectedValue = "2";
}
}
Upvotes: 0
Views: 35
Reputation: 1455
try this code working fine
foreach (System.Web.UI.WebControls.ListItem item in chbList_Risks.Items)
{
if (item .Selected == true)
{
value = item .Text;
if (value == "ABC")
{
ddlRiskLevel.SelectedValue = "2";
}
}
else
{
value = item .Text;
if (value == "XYZ")
{
ddlRiskLevel.ClearSelection();
}
}
}
Upvotes: 0
Reputation: 790
you are not doing anything for uncheck, try below
foreach (ListItem item in chbList_Risks.Items)
{
if (item.Selected)
{
ddlRiskLevel.SelectedValue = "2";
break;
}
else
{
ddlRiskLevel.ClearSelection();
}
}
Upvotes: 2