[I have an application where , i have a radiobuttonlist having 2 radio buttons radiobuttons RB1 & RB2
and there are 6 checkBoxes CB1 thru CB6
If RB1 is selected then CB6 shld be disable /invisible and if RB2 is selected then all the CBs shld be enabled. How do i do this?

protected void rb_OnIndexChanged(object sender, System.EventArgs e)
	{				
if (rb.SelectedValue=="A")
	{
	cb6.Enabled=true;
	}
	}

i get an error Object reference not set to an instance of an object
plz help.
thank you

Dani AI

Generated

Common causes of the "Object reference not set to an instance of an object" seen by are a mismatched control ID, a control missing runat="server", the event not being wired up, or the wrong event type (RadioButtonList uses SelectedIndexChanged, single RadioButton uses CheckedChanged). ’s per-radio handlers show the idea, but the RB2 handler actually disables all checkboxes (contrary to the requirement to enable all when RB2 is chosen).

A clean server-side pattern when using a RadioButtonList:

  • Markup: set runat="server" on the list and checkboxes and AutoPostBack="true" on the radio list (or wire the event in code).
  • In the handler cast sender to RadioButtonList and null-check it.
  • Change only the needed checkbox (CB6) and avoid repeating code by iterating a container (Panel/PlaceHolder) that holds the checkboxes.

Example server-side handler:

protected void RadioChoices_SelectedIndexChanged(object sender, EventArgs e)
{
    var rbl = sender as RadioButtonList;
    if (rbl == null) return;

    bool hideCb6 = rbl.SelectedValue == "A"; // RB1
    cb6.Visible = !hideCb6;

    foreach (Control c in checkboxPanel.Controls)
    {
        var cb = c as CheckBox;
        if (cb == null) continue;
        if (cb.ID == "cb6")
            cb.Enabled = !hideCb6;
        else
            cb.Enabled = true; // RB2 makes all enabled
    }
}

For a snappier UX, do this client-side with JavaScript: add onclick/onchange to the radio buttons and toggle the disabled or style.display of the checkboxes using their ClientID. That avoids postbacks entirely.

Quick troubleshooting checklist: confirm runat="server", verify IDs match the code-behind, set AutoPostBack="true" (if server-side), only bind lists inside if (!IsPostBack), and inspect the exception stack trace to see exactly which variable is null.

Recommended Answers

All 2 Replies

try this :

private void RB1_CheckedChanged(object sender, System.EventArgs e)
{
	if (RB1.Checked == true)
	{
		C1.Enabled = true;
		C2.Enabled = true;
		C3.Enabled = true;
		C4.Enabled = true;
		C5.Enabled = true;
		C6.Enabled = false;
	}
}

private void RB2_CheckedChanged(object sender, System.EventArgs e)
{
	if (RB2.Checked == true)
	{
		C1.Enabled = false;
		C2.Enabled = false;
		C3.Enabled = false;
		C4.Enabled = false;
		C5.Enabled = false;
		C6.Enabled = false;
	}
}

thnx Jx_Man. i will surely try this tomorrow. and let u know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.