Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Passing dynamically added C# control values to querystring

  • 15-08-2006 10:29AM
    #1
    Closed Accounts Posts: 80 ✭✭


    C# if possible. I have a webform which reads 30 values from a database and adds a RadioButton for each one to the form. The user is then invited to select a number of these values. This I have done and can do.

    What I have not done and need to program is the following: When the form is submitted redirect to another form with value pairs for the radiobutton selections in the querystring. Like so:

    newpage.aspx?Value1=0&Value2=1&Value3=0..........&Value30=1

    Where 1 is checked and 0 is unckecked, or something similar.

    I know this seems like a simple request but given that I have little c# experience and have inherited a project without much training I am having difficulty. I wont go into my attempts thus far but they have been significant and frustrating.

    Also can I control the names of the radiobuttons that are added they are being named _ctl0 to _ctl29?

    Finally and I hope I am not pushing my luck here, but how can I write events for the dynamically added controls?

    Any help is appreciated.
    Jim


Comments

  • Registered Users, Registered Users 2 Posts: 1,454 ✭✭✭Smoggy


    I presume its your code that is dynamically placing the controls and naming them ? well isn't it as simple as :

    1.) when the form is submitted reading the values out of the form , as you know the names going in.
    2.) when your dynamically making the controls , make the events hook up to your static sub to deal with the event.


  • Closed Accounts Posts: 80 ✭✭jimbozo


    1.) when the form is submitted reading the values out of the form , as you know the names going in.

    I dont know how many controls will be on the form, this may vary with the data. I had tried looping through the controls in the form checking for radiobuttons but was unable to see if they were checked as the code below failed when I try to access the Checked property of a Control object. note also an attempt to 'cast' the control to a radiobutton.
    
    foreach(Control  ctl in this.Controls  )
    {
    	if (ctl.GetType () == typeof(System.Web.UI.WebControls.RadioButton ))
    	{
    		i = i + 1;
    
    		if ((System.Web.UI.WebControls.RadioButton)ctl.Checked == true)
    		{
    			sQueryString = sQueryString + "/&Q" + i + "=1";
    		}
    		else
    		{
    			sQueryString = sQueryString + "/&Q" + i + "=0";
    		}
    	}
    }
    

    How would you fix/replace the code above?
    2.) when your dynamically making the controls , make the events hook up to your static sub to deal with the event.

    Thanks for this. I don't know how to do this but its is something I can lookup myself and is less important that the main issue.


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    I don't know if this is of any use, but check this out. As far as I know, Radio buttons don't appear in the Request.Form collection unless they have been checked. So, if you loop through the radio buttons, only the selected ones will appear.
    private void cmdSubmit_Click(object sender, System.EventArgs e)
    		{
    			foreach (string sVar in Request.Form)
    			{
    				Response.Write(sVar + "<BR>");
    			}
    		}
    

    By the way, checkboxes may be more appropriate if you can select multiple ones, rather than just one of the 30 or so


  • Closed Accounts Posts: 80 ✭✭jimbozo


    Eoin your code has done the job to perfection. Thank you. I also take your point that checkboxes would suit my purposes better.

    Cheers Jim


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    jimbozo wrote:
    Eoin your code has done the job to perfection. Thank you. I also take your point that checkboxes would suit my purposes better.

    Cheers Jim

    Brilliant stuff :)


  • Advertisement
Advertisement