Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Casting and Refactoring

Just looking at this again I want to refactor it, but it is out of my hands.

I created this to extract some common tasks that would be shared between newsletter and other mailing request forms. It is amazing how ugly code looks when you are about to post it online (AddRange Doh!) .

I had to build the ArrayList from the UI elements and pass it to the QA.Save method.

ArrayList questionAnswer = QuestionAnswerUIHelper.GetProfileAnswer(BrochureID,QuestionAnswerUIHelper.FindWebControls(this,"QuestionID"));
QuestionAnswerManager.Current.SaveQuestionAnswer(questionAnswer);

The method GetQuestionAnswerObject relates directly to my question about casting.

sealed public class QuestionAnswerUIHelper

      {

            private QuestionAnswerUIHelper(){}

            /// <summary>

            /// Get the Value from the control, return a null string if it is empty

            /// </summary>

            /// <param name="source"></param>

            /// <returns></returns>

            static public string GetControlValue(Control source,string defaultValue)

            {

                  if (source is TextBox)

                  {

                        return GetControlValue((TextBox)source,defaultValue);

                  }

                  if (source is ListControl)

                  {

                        return GetControlValue((ListControl)source,defaultValue);

                  }

                  return null;

 

            }                

            static public string GetControlValue(TextBox source,string defaultValue)

            {

                  return GetStringWithDefault(source.Text,null);

            }          

            static public string GetControlValue(ListControl source,string defaultValue)

            {

                  return GetStringWithDefault(source.SelectedValue,null);

            }    

            static private string GetStringWithDefault(string value,string defaultValue)

            {

                  string input= value.Trim();

                  if(input.Length==0 || input == String.Empty)

                  {

                        return defaultValue;

                  }

                  return value;

            }

            static public ArrayList GetProfileAnswer(string subscriptionProfileId,params Control[] inputControls)

            {

 

                  ArrayList questionAnswerList = new ArrayList();

           

                  string userID = PublicUserContext.Current.UserID;

                  QuestionAnswerObject qa = null;

                 

                  foreach (WebControl webControl in inputControls)

                  {

                        if(webControl.Attributes["QuestionID"]!=null)

                        {

                              qa = GetQuestionAnswerObject(webControl);

                             

                              if (qa !=null)

                              {

                                    qa.UserId = userID;

                                   

                                    qa.SubscriptionProfileId = subscriptionProfileId;

 

                                    string descriptiveTextControlID = webControl.Attributes["DescriptiveTextControl"];

                                    if (qa.AnswerId == 99 && descriptiveTextControlID!=null)

                                    {

                                          Control descriptiveTextControl = webControl.NamingContainer.FindControl(descriptiveTextControlID) ;

                                          if(descriptiveTextControl!=null)

                                          {

                                                qa.Description = GetControlValue(descriptiveTextControl,null); 

                                          }

                                    }    

 

                                    questionAnswerList.Add(qa);

                              }                                  

                        }

                  }

                  return questionAnswerList;

            }

            static public QuestionAnswerObject GetQuestionAnswerObject(WebControl control)

            {

                  if(control is ListControl)

                  {

                        return GetQuestionAnswerObject((ListControl) control);

                  }

                  if(control is CheckBox)

                  {

                        return GetQuestionAnswerObject((CheckBox) control);

                  }

                  return null;

 

            }          

            static public QuestionAnswerObject GetQuestionAnswerObject(ListControl listControl)

            {

                  if(listControl.SelectedIndex > -1)

                  {

                        QuestionAnswerObject questionAnswerObject = new QuestionAnswerObject();

                 

                        int questionID = Convert.ToInt32(listControl.Attributes["QuestionID"]);

                        int answerID = Convert.ToInt32(listControl.Attributes["AnswerID"]);

                        questionAnswerObject.QuestionId = questionID;

                        questionAnswerObject.AnswerId = answerID ;

                        return questionAnswerObject;

                  }

                  return null;

            }

            static public QuestionAnswerObject GetQuestionAnswerObject(CheckBox checkbox)

            {

                  if(checkbox.Checked)

                  {

                        QuestionAnswerObject questionAnswerObject = new QuestionAnswerObject();

                 

                        int questionID = Convert.ToInt32(checkbox.Attributes["QuestionID"]);

                        int answerID = Convert.ToInt32(checkbox.Attributes["AnswerID"]);

                        questionAnswerObject.QuestionId = questionID;

                        questionAnswerObject.AnswerId = answerID ;

                        return questionAnswerObject;

                  }

                  return null;

            }

           

            static public Control[] FindWebControls(Control namingContainer,string requiredAttribute )

            {

                  ArrayList controlList = new ArrayList();

                  WebControl current;

                  foreach(Control childControl in namingContainer.Controls)

                  {

                        current = childControl as WebControl;

                        if(current!=null)

                        {

                              if(current.Attributes[requiredAttribute]!=null)

                              {

                                    controlList.Add(current);

                              }

                        }

                        if(childControl.HasControls())

                        {

                              Control[] childControlList = FindWebControls(childControl,requiredAttribute);

                              foreach(Control childMatch in childControlList)

                              {

                                    controlList.Add(childMatch);

                              }

                        }

                  }

 

                  if(controlList.Count > 0)

                  {

                        Control[] controls = (Control[])controlList.ToArray(typeof(Control));

                        return controls;

                  }

                  else

                  {

                        return new Control[]{};

                  }

            }

      }

 

 

1 Comment

  • Hi,
    I have problem with two type for searching control. I have 5 User Controls i one page, and I try to find label for error message in 1 user control.
    First step:
    this.Parent.NamingContainer.Controls[0].TemplateControl.Controls[7]
    Second step:
    this.Parent.NamingContainer.FindControl("ctl00").TemplateControl.FindControl("lblErrorMain")
    In debug mode, I see that everything is ok with contolID, I mean Controls[0] has controlId "ctl00", and Controls[7] - "lblErrorMain", but in second step I have error:"System.NullReferenceException: Object reference not set to an instance of an object".

    Please write me your solution, tnx

Comments have been disabled for this content.