1

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 server 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 reading this in my javascript using ajax and assign it as the inner HTML of a div.

How to read this RadioButton list in Codebehind ?

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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?

2 Comments

YEs it is created in server side at runtime
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.