增加标签和单选按钮并从数据库中检索 [英] increment the labels and radio buttons and retriving from database
问题描述
我在数据库中有一张表,想要检索数据并将其显示在网页中
这就是我所知道的
dr = dt.Rows [k];
Label1.Text = dr [0] .ToString();
RadioButton1.Text = dr [1] .ToString();
RadioButton2.Text = dr [2] .ToString();
RadioButton3.Text = dr [3] .ToString();
RadioButton4.Text = dr [4] .ToString();
请告诉我如何给它加油,以便我不应该一次又一次地复制和粘贴它.请给我代码...帮助我...
i have one table in database and want to retrive the data and display it in web page
so this is what i know
dr = dt.Rows[k];
Label1.Text = dr[0].ToString();
RadioButton1.Text = dr[1].ToString();
RadioButton2.Text = dr[2].ToString();
RadioButton3.Text = dr[3].ToString();
RadioButton4.Text = dr[4].ToString();
please tell me how to increament this so that i should not copy and paste this again and again.please give me the code...help me...
推荐答案
我是否已经向您解释过您不应该在Designer中创建单选按钮?这是没有道理的-手工无条件的无聊的不可靠的工作.而是用代码创建它们:
Did I already explain you that you should not create your radio buttons in Designer? It makes no sense — manual unqualified boring unreliable work. Create them in code instead:
RadioButton[] RadioButtons = new RadioButton[/*...*/];
for (int index = 0; index < RadioButtons.Length; ++index) {
RadioButton rb = new RadioButton();
RadioButtons[index] = rb;
rb.Text = //... from some data file, resource, date set...
rb.Left = //calculate it based on index and/or layout measures
rb.Top = //calculate it based on index and/or layout measures
rb.CheckedChanged += (sender, eventArgs) => {
RaduoButton radioButtonSender = (RaduoButton)sender;
//some handling using radioButtonSender and its status
};
rb.Parent = someRadioButtonParent; //some group or panel
}
如果执行此操作,所有操作将变得容易:
If you do this, all operations become easy:
for (int index = 0; index < RadioButtons.Length; ++index) {
RadioButton[index].Text = dr[index];
//...
}
尝试使用FindControl.
Try to use FindControl.
for (int index = 0; index < 200; index++)
{
Control radioButton=yourContainer. FindControl("RadioButton"+i);
if(radioButton!=Null){
radioButton.Text= dr[i].ToString();
}
}
这篇关于增加标签和单选按钮并从数据库中检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!