在ASP.NET和C#中动态创建的按钮 [英] Dynamically created button in asp.net and c#
问题描述
我正在asp.net&中创建在线考试项目. c#.
我想在.aspx网页的底部显示每个问题的按钮,以便每当学生单击按钮时,都会显示该按钮的问题.
我正在以动态方式创建按钮:
Hi,
I am creating online examination project in asp.net & c#.
I want to display buttons for each question at the bottom of my .aspx web page, so that whenever student clicks button, question for that button will be displayed.
I am creating buttons dynamically as :
void CreateButtons()
{
for (int i = 1; i < 70; i++)
{
Button btn = new Button();
btn.Text = i.ToString();
btn.Font.Bold = true;
btn.ForeColor = System.Drawing.Color.Black; ;
btn.BackColor = System.Drawing.Color.LightSkyBlue;
btn.Font.Size = 10;
btn.Width = Unit.Pixel(50);
pnlButtons.Controls.Add(btn);
pnlButtons.Visible = true;
btn.Click += new EventHandler(btn_Click);
}
}
并在
中获取问题
and getting the questions in
void btn_Click(object sender, EventArgs e)
{
}
现在,我想在单选按钮列表中选择答案后立即更改每个按钮的颜色.
在单选按钮列表的selectedindex上,我无法访问动态创建的按钮.
谁能让我知道如何更改单选按钮列表中选定事件的动态创建按钮的颜色.
[edit]已添加代码块-OriginalGriff [/edit]
Now i want to change color of each button as soon as answer is selected in radiobutton list.
On selectedindex of radiobutton list, i m not able to access dynamically created buttons.
Can anyone let me know how to change colour of Dynamically created button on radiobuttonlist selected event.
[edit]Code block added - OriginalGriff[/edit]
推荐答案
有点复杂,但还算不错:
1)将此行添加到您的CreateButtons方法中:
It''s a little complicated, but not too bad:
1) Add this line to your CreateButtons method:
btn.Width = Unit.Pixel(50);
btn.ID = string.Format("butDynamic{0}", i); // Give the button a unique ID
pnlButtons.Controls.Add(btn);
2)将隐藏"字段添加到您的页面:
2) Add a Hidden field to your Page:
<asp:HiddenField ID="PressedButton" runat="server" />
3)将此代码添加到您的Button处理程序中:
3) Add this code to your Button handler:
void btn_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (b != null)
{
// Save the last pressed ID for later
PressedButton.Value = b.ID;
}
}
4)将此代码添加到您的SelectedIndexChanged事件中:
4) Add this code to your SelectedIndexChanged event:
if (PressedButton.Value != null)
{
Button b = (Button) (pnlButtons.FindControl((string) PressedButton.Value));
if (b != null)
{
b.BackColor = System.Drawing.Color.Red;
}
}
请记住,只有在RadioListBox失去焦点时才收到选定索引"更改事件,而不是在用户做出选择时才发生.
Do bear in mind that you only get the Selected Index changed event when the RadioListBox loses the focus - NOT when the user makes a selection.
问题出在事件处理程序中.
尝试以下代码:
The problem is in the event handler.
Try this code:
private void button1_Click(object sender, EventArgs e)
{
int distance = 0;
EventHandler evh = new EventHandler(btn_Click);
for (int i = 0; i < 5; i++)
{
Button btn = new Button();
btn.Text = i.ToString();
btn.ForeColor = System.Drawing.Color.Black; ;
btn.BackColor = System.Drawing.Color.LightSkyBlue;
btn.Width = 50;
btn.Location = new Point(distance, 0);
distance = distance + 50;
this.Controls.Add(btn);
btn.Click += evh;
}
}
void btn_Click(object sender, EventArgs e)
{
string a = string.Empty;
}
欢呼声
Cheers
如果你得到答案,请标记
If u got answer please mark up
这篇关于在ASP.NET和C#中动态创建的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!