迭代地改变控件 [英] Iteratively change the controls

查看:102
本文介绍了迭代地改变控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是每次使用if(i == 3)然后{radiobuttionlist3被选中}如何为循环表示像radiobuttionlist [i]那样的东西???



Instead of using every time if (i==3) then {radiobuttionlist3 is selected } how to standize that for the loop something like radiobuttionlist[i] ???

for (int i = 1; i <= 25; i++)
                {

                    SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from     questions where tname='Prelim' and questionno='" + i + "'", con);
                    SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                    DataSet dss = new DataSet();
                    daa.Fill(dss, "questions");

                    DataRow DRR = dss.Tables[0].Rows[0];

                    if (i == 1)
                    {
                        RadioButtonList1.Items.Clear();
                        RadioButtonList1.Items.Add(DRR[0].ToString());
                        RadioButtonList1.Items.Add(DRR[1].ToString());
                        RadioButtonList1.Items.Add(DRR[2].ToString());
                        RadioButtonList1.Items.Add(DRR[3].ToString());
                    }
                    else if (i == 2)
                    {
                        RadioButtonList2.Items.Clear();
                        RadioButtonList2.Items.Add(DRR[0].ToString());
                        RadioButtonList2.Items.Add(DRR[1].ToString());
                        RadioButtonList2.Items.Add(DRR[2].ToString());
                        RadioButtonList2.Items.Add(DRR[3].ToString());
                    }
                    else if (i == 3)
                    {
                        RadioButtonList3.Items.Clear();
                        RadioButtonList3.Items.Add(DRR[0].ToString());
                        RadioButtonList3.Items.Add(DRR[1].ToString());
                        RadioButtonList3.Items.Add(DRR[2].ToString());
                        RadioButtonList3.Items.Add(DRR[3].ToString());
.....


}





而不是每次都使用if(i == 3)然后{radiobuttionlist3被选中}如何为循环表示像radiobuttionlist那样的东西[i] ???



代码块添加[/ Edit]



Instead of using every time if (i==3) then {radiobuttionlist3 is selected } how to standize that for the loop something like radiobuttionlist[i] ???

Code block added[/Edit]

推荐答案

可以通过各种方式来标准化此代码..



my建议是:



1.编写一个函数,填充RadioButton列表。



There can be various way to standardize this code..

my suggestion would be:

1. Write a function which will populate the RadioButton list.

public void FillRadioButton(RadioButtonList rbList, DataRow dr)
{
    rbList.Items.Clear();
    rbList.Items.Add(dr[0].ToString());
    rbList.Items.Add(dr[1].ToString());
    rbList.Items.Add(dr[2].ToString());
    rbList.Items.Add(dr[3].ToString());
}







2.更新foreach循环以填充为所有人调用此方法radiobutton list






2. Update the foreach loop to fill the invoke this method for all the radiobutton list

for (int i = 1; i <= 25; i++)
            {

                SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname='Prelim' and questionno='" + i + "'", con);
                SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                DataSet dss = new DataSet();
                daa.Fill(dss, "questions");

                DataRow DRR = dss.Tables[0].Rows[0];

                RadioButtonList rbList = this.FindControl("RadioButtonList" + i.ToString()) as RadioButtonList;
                FillRadioButton(rbList, DRR);
            }







希望这有帮助....




Hope this helps....


这篇关于迭代地改变控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆