tags:

views:

309

answers:

5

I have an asp.net page.I am generating a RAdioButton list from server ( an asp.net page ) and load it to a DIV using javascript and ajax.Now i want to read the RadioButton list in my codebhind file.But the VS Intelisense is not showing the control name.Can anyone tell me how to read it ?

My code for generating radio button list in servr side

System.IO.StringWriter swriter=new System.IO.StringWriter(); HtmlTextWriter textWriter = new HtmlTextWriter(swriter);

    string strPriceOutput="";
    RadioButtonList iRadioBtnListPrices = new RadioButtonList();
    iRadioBtnListPrices.RepeatDirection = RepeatDirection.Vertical;
    iRadioBtnListPrices.CssClass = "priceList";
    string strRadios = "";
   if ((this.Vendor != null))
   {
        foreach (VendorQuoteType vendorQuoteType in Vendor.VendorQuoteTypeCollection)
        {        
            iRadioBtnListPrices.Items.Add(new ListItem(" $ " + totalPrice.ToString()" ,vendorID.ToString()));
        }

   }
   iRadioBtnListPrices.RenderControl(textWriter);
   strPriceOutput = swriter.ToString();

and I am using reading this in my javascript using qjax and assign it as the inner HTML of a div.

How to read this radioButton list in Codebehind ?

+1  A: 

You probably need to find with .FindControl() and cast it to a RadioButtonList. If you add it to the contents of a Panel or some other control that narrows down where you need to search for it (maybe even a <div id="myDiv" runat="server"> is sufficient, I don't know...) you can then use this:

RadioButtonList theList = 
    (RadioButtonList)thePanel.FindControl("theIdOfTheRadioButtonList");

theList.WillNowGiveYouIntellisense();
Tomas Lycken
A: 

1st of all, why are you using such a strange method for rendering a radio button list? I guess there are much better ways for achieving what you need. Take for instance a look at the RadioButtonList databinding mechanism: here. I would add the radiobutton list to the page and do it with the databinding mechanism or in the loop as you did. Then I would hide/show (myRadioButtonList.Visible = false/true) the list depending on some conditions.

The reason why you probably don't see your radiobutton list on the codebehind is because you are creating it dynamically at run-time. The intellisense will just show you the elements which you placed on your aspx/ascx code and for which the designer has created appropriate variables in the designer file that is attached to your page/user control. How should it otherwise be able to retrieve it?

Juri
YEs it is created in server side at runtime
Shyju
Yes I saw that, but why did you do that? I'm a prof. web developer and actually I have to do this very rarely. Could you explain me the reason for doing so?
Juri
+1  A: 

Several ways to improve your code:

  • create a RadioButtonList control in aspx, and manually fill it in Page_Load

thus IntelliSense will find the control name

or

  • add the dynamically created RadioButtonList as child of an existing control

  • set the ID property of the RadioButtonList

  • use FindControl with the chosen ID

calling RenderControl is not required in these solutions.

devio
A: 

here you are doing 1 mistake when you creating new RadioButtonList, you should set ID of the control, later you have to use this ID for find control. Like

RadioButtonList iRadioBtnListPrices = new RadioButtonList();

Add this statement as well..

RadioButtonList.ID = "iRadioBtnListPrices";


Try to follow these steps
Make new method name 'PopulateControls()' and put your code of creating radioButton in it.

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
PopulateControls();
}

}

protected override void CreateChildControls()
{
    if (Page.IsPostBack)
    {
        PopulateControls();
    }
}
Muhammad Akhtar
A: 

refer this

adding dynamic checkboxes