Community discussion forum

ASP.NET/C# Dynamic controls on page problems

  • 4 years ago

    so. I'm having a problem with a C# .net page I'm working on.


    Basically its an online survey thingamy....
    Initially the page calls a DB and gets a list of "sections" and "questions" for each section.


    It then display these in a table with a set of radio buttons for each bit:
    (THE OOO are radio buttons)




    Section 1
    1) Question 1  OOO
    2)Question 2  OOO
    3)Question 3  OOO
    4)Question4  OOO
    5)Question 5  OOO


    Section 2
    1)Question 1 OOO
    2)Question 2 OOO
    3)Question 3 OOO





    I then have a button which works out which radio is clicked for each question and writes results to DB.



    My Problem is that when the page starts, the questions etc are dynamically added to a placeholder on the page, but when the button calls its on_click function, it basically refreshes the page and all the questions/user responses disapear.


    Any ideas how to stop this or perhaps another way of doing it to avoid the problem??



    My second problem is getting the radio button status.
    Normally for a radio I just do something like:


    if(myRadio.Checked){
    do something
    }


    but because the radio buttons are dynamically created I can't refer to them by name. I am able to get to them via:


    Control myControl = findControl("myRadio")


    but then I am unable to access the checked/unchecked status because when writing the code it doesn't know if it is a check box.



    All help gratefully accepted.

  • 4 years ago
    Hello,

    Here is the solution for the first problem you got.

    Check for the Page.Ispostback inside the Page_load.

    Ex:


                                private void Page_Load(object sender, System.EventArgs e)
           {
               if(!Page.IsPostBack)
               {
                   ArrayList a1=new ArrayList();
                   a1.Add("hello1");
                   a1.Add("hello2");
                   a1.Add("hello3");
                   a1.TrimToSize();
                   RadioButtonList1.DataSource = a1;
                   RadioButtonList1.DataBind();
                   RadioButtonList1.Items[0].Selected = true    ;
               }
           }

    Hope this can be helpful to you.

    Thanks & Regards
    Dotnet Buddy
  • 4 years ago
    You need to loop through the list of radio buttons and check each one individually.

    for each x in RadioButtonList
    if x.checked ...

    I hope this helps and it is only one way to do it.
  • 4 years ago

    Hello,


           "Ispostback" is the perfect solution of your first issue.  For checking the radio buttons and get it value(weather it is in checked status or not) u can use the javascript function and call that function in button click event. Here is the code,


        ...javascript code


           document.forms[0].elements.length  ---> This will give the total elements such as, textboxes and redio buttons etc., in a particular form.


          By using that value u just construct one for loop and check the each and every element inside that  loop.


        For example... Here I am sending the code for getting the textboxes in a form.


            function getval(){
                var i=0;
                for(i=0;i<=document.forms[0].].elements.length;i++ ){
                    if(document.forms[0].].elements.type="text"){  
                    // Here u give like if(document.forms. ].elements.type="radio")
                     //inside of this if statement u can get all the  dynamic controls and it values.
                          alert( document.forms[0].].elements.value);
                     }
                }
            }



  • 1 year ago

     I'm having a similar problem, but I'm stuck thanks to the page lifecycle problem meaning that all my radio buttons look like they're unchecked, but then when they're displayed on the page, one of them is.

     

     "I am able to get to them via:

    Control myControl = findControl("myRadio")

    but then I am unable to access the checked/unchecked status because when writing the code it doesn't know if it is a check box."

     

    You need to cast findControl to be a radio button, like so:

    RadioButton myControl = (RadioButton)FindControl("myRadio");

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback