Click here to Skip to main content
15,879,008 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a search app that has 2 pages and uses a master page. A number of asp.net controls reside on the default page and I need to get their selected values to a label on the results page. I am using a session variable to pass the string built (critString) from the control values to the second page. However with the code below it simply jumps& over the 1st step of the foreach.

C#
public void doStuff()
      {
          var props = new[]{
              "txtFacility", "txtCity", "txtZip",
              "rblFacilityType", "cboSubs", "ddlbCounty",
              "ddlStar", "cblProgramOp", "cblAgeCat",
              "cblTrans"};
          foreach (var v in props)
          {
              if (FindControl(v) != null)
                  {
                      switch (v.Substring(0, 3))
                      {
                          case "cbl":
                              critString += fillcbxl((CheckBoxList)FindControl(v));
                              break;
                          case "ddl":
                              critString += fillddl((DropDownList)FindControl(v));
                              break;
                          case "cbo":
                             critString += fillcbx((CheckBox)FindControl(v));
                              break;
                          case "rbl":
                              critString += fillrbl((RadioButtonList)FindControl(v));
                              break;
                          default:
                              critString += fillTextBox((TextBox)FindControl(v));
                              break;
                      }
                  }
              }
          }


I am not sending the method calls at this time because I can't get to them without getting this to work. I figure it maybe a errant FindControl process given there is a master.page involved. But then it could be that with no checks box selected it evaluates null (noting selected scenario).
Posted
Updated 2-Feb-16 17:16pm
v3
Comments
Sergey Alexandrovich Kryukov 2-Feb-16 15:28pm    
The case statement and all those hard-coded "cbl", "ddl"... makes the code so bad that I don't see why it should be even discussed, unless you explain what you want to achieve, ultimately.
—SA
Foothill 2-Feb-16 16:42pm    
I would suggest stepping through the foreach to see if FindControl(v) is returning a value and if so, check to see what v.Substring(0,3) returns. Asp.Net likes to make additions to control names when generating the html is sends to the browser rendering the switch..case pointless.

1 solution

using stringbuilder

http://www.dotnetperls.com/stringbuilder[^]

C#
// Declare a new StringBuilder.
StringBuilder builder = new StringBuilder();
builder.Append(fillcbxl((CheckBoxList)FindControl(v)));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900