I am new to this c# coding.
I am trying to write a code using checkboxes. Below is code in design flow
<table>
<tr>
<td align="right">
<asp:Label ID="lblOwn" runat="server" Text="Owned by You :"></asp:Label>
</td>
<td align="left">
<asp:CheckBoxList ID="chbxOwn" runat="server"
OnSelectedIndexChanged="onAckTypeChanged3" AutoPostBack="true">
<asp:ListItem>2 wheeler</asp:ListItem>
<asp:ListItem>4 wheeler</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label1" runat="server" Text="2 w"></asp:Label>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label2" runat="server" Text="4 w"></asp:Label>
</td>
</tr>
</table>
And following is codebehind
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Hid1();
Hid2();
}
protected void onAckTypeChanged3(object sender, EventArgs e)
{
if (chbxOwn.SelectedItem.Text == "2 wheeler")
{
Vis1();
}
if (chbxOwn.SelectedItem.Text == "4 wheeler")
{
Vis2();
}
}
private void Hid1()
{
Label1.Visible = false;
}
private void Hid2()
{
Label2.Visible = false;
}
private void Vis1()
{
Label1.Visible = true;
}
private void Vis2()
{
Label2.Visible = true;
}
}
When “2 wheeler” checkbox is checked “2 w” label is visible. But when i check “4 wheeler” it doesnot shows both the labels. i want to know where i am making a mistake. Also when i de-select both the checkboxes there is an Null reference exception generated.
can you please help me out with these problems
1. Checking both the checkbox should display both the labels.
2. De-selecting should not produce null reference exception.
Thanks in advance
Regards,
Abhishek
This is what you want:
It works like a charm..!!
Here is the working demo: DEMO