I want to change the DropDownList1 value if Checkbox1 is checked in my Web Application.
The Default Value on the DropDownList1 when the page loads should be "0" When the Checkbox1 is clicked, the DropDownList1 value should be "1"
The following code which I have done throws a runtime error.
My aspx snippet:
<asp:CheckBox ID="Checkbox1" runat="server"
class="itemCheck" AutoPostBack="True" CausesValidation="True" />
</td>
<td >
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"
Width="138px">
<asp:ListItem Selected="True">0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
My code behind file:
protected void Page_Load(object sender, EventArgs e)
{
string val = "1";
ListItem checkItem = DropDownList1.Items.FindByValue(val);
if (checkItem != null)
{
DropDownList1.ClearSelection();
checkItem.Selected = true;
}
}
Your listitems are missing a value attribute.