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[]{};
}
}
}