I try to develop a dynamically created toolbox control in my website and I need to make that as efficient as possible. I have two conditions like the following:
private void produceControls()
{
if (General.survey_answer_type_id == 3)
{
rbtnList = new RadioButtonList();
rbtnList.ID = "ControlID_3";
SqlDataReader dr_answer = cmd.ExecuteReader();
while (dr_answer.Read())
{
rbtnList.Items.Add(dr_answer["answer"].ToString());
}
PlaceHolder1.Controls.Add(rbtnList);
}
else if (General.survey_answer_type_id == 4)
{
chkBoxList = new CheckBoxList();
chkBoxList.ID = "ControlID_4";
SqlDataReader dr_answer = cmd.ExecuteReader();
while (dr_answer.Read())
{
chkBoxList.Items.Add(dr_answer["answer"].ToString());
}
PlaceHolder1.Controls.Add(chkBoxList);
}
}
These conditions are the same except for the toolbox control name itself. I need to have a method to use in suitable conditions declared as (this is just an abstract):
public void foo(var toolbox_name)
{
toolbox_name = new RadioButtonList();
toolbox_name.ID = "ControlID_3";
SqlDataReader dr_answer = cmd.ExecuteReader();
while (dr_answer.Read())
{
toolbox_name.Items.Add(dr_answer["answer"].ToString());
}
}
And then I want to use it like:
private void produceControls()
{
if (General.survey_answer_type_id == 3)
{
foo(rbtnList);
}
else if (General.survey_answer_type_id == 4)
{
foo(chkBoxList);
}
}
How could I achieve this? Thank you.
I just needed to know what the parent class of CheckBoxlist
and RadioButtonList
is. It is just ListControl
.
For example:
public void showListControl(ListControl lstcontrol)
{
lstcontrol.ID = "ControlID_3";
SqlDataReader dr_answer = cmd.ExecuteReader();
while (dr_answer.Read())
{
lstcontrol.Items.Add(dr_answer["answer"].ToString());
}
}
And then use it like:
RadioButtonList rbtnList = new RadioButtonList();
showListControl(rbtnList);