1

Here is the complete code. I want to display a radiogroup when I select 1 in the dropdown list box. I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'.

         namespace HostelRoomManagement 
         {
          public partial class WebForm1 : System.Web.UI.Page
          {
          protected void Page_Load(object sender, EventArgs e)
           {

            }



    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedValue == "1")
        {
            RadioButton rb1 = new RadioButton();
            rb1.ID = "RadioButton1";
            rb1.Text = "C block";
            rb1.GroupName = "BlockGroup";
            RadioButton rb2 = new RadioButton();
            rb2.ID = "RadioButton2";
            rb2.Text = "C block";
            rb2.GroupName = "BlockGroup";
            Page.Controls.Add(rb1);
            Page.Controls.Add(rb2);



        }


    }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {

    }


}
}

I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'

Srini Vas
  • 115
  • 1
  • 10

3 Answers3

0

You have created two radio button but where have you added them on the page?

Start by creating a place holder for you radio-button lists and add these controls over there.
The dynamically created control will be lost on the post back. This means you will have to manage you dynamically created controls.

Here is a good [example]: 1 ASP.NET dynamically created controls and Postback.

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • I added these 2 lines and still no change. Page.Controls.Add(rb1); Page.Controls.Add(rb2); – Srini Vas Jan 05 '13 at 07:52
  • @krshekar System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server is the error I get – Srini Vas Jan 05 '13 at 07:56
0

I suspect your problem is that you are referencing to SelectedValue whereas you want to be refering to selectedindex.

Hope this helps.

Brett
  • 3,296
  • 5
  • 29
  • 45
0

You can add the radio buttons you want the user to see based on his choice to the page and set the Visibility of them to false. Then, once the user choose a value change the visibility of the radio buttons you want to true. It might be easier.

Laila
  • 417
  • 1
  • 3
  • 14