在asp.net和c#中动态创建按钮(Dynamically created button in asp.net and c#)

   IT问题网   2021-01-20 00:00:00

问 题

嗨,

我正在asp.net创建在线考试项目amp; c#。

我想在.aspx网页底部显示每个问题的按钮,这样每当学生点击按钮时,就会显示该按钮的问题。

我动态创建按钮:

  void  createbuttons()
{
for int i = 1 ;我lt; 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);

}
}


并获得问题

  void  btn_click( object  sender,eventargs e)
{
}




现在我想在radiobutton列表中选择答案后立即更改每个按钮的颜色。

在radiobutton列表的selectedindex上,我无法访问动态创建的按钮。



任何人都可以让我知道如何更改动态创建按钮的颜色radiobuttonlist选择的活动。





[edit]已添加代码块 -originalgriff [/ edit]

解决方案

这有点复杂,但也不算太糟糕:

1)将此行添加到createbuttons方法:



 btn.width = unit.pixel( 50 ); 
btn.id = string .format(" butdynamic {0}",i); // 为按钮指定唯一id
pnlbuttons.controls.add(btn);


2)在你的页面添加一个隐藏字段:

 lt;   asp:hiddenfield     id   ="  pressedbutton"    runat   ="  server"    /  gt;  


3)将此代码添加到button处理程序:

  void  btn_click( object  sender,eventargs e)
{
button b = sender as 按钮;
if (b!= null
{
// 保存上次按下的id以供日后使用
pressedbutton.value = b.id;
}
}


4)将此代码添加到您的selectedindexchanged事件:

  if (pressedbutton.value!=  null 
{
按钮b =(按钮)(pnlbuttons.findcontrol(( string )pressedbutton.value));
if (b!= null
{
b。 backcolor = system.drawing.color.red;
}
}


请记住,当radiolistbox失去焦点时,您只能获得选定的索引更改事件 - 不是当用户进行选择时。

问题出在事件处理程序中。



试试这段代码:



 private void button1_click(object sender,eventargs e)
{
int distance = 0;
eventhandler evh = new eventhandler(btn_click);

for(int i = 0; ilt; 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(距离,0);
距离=距离+50;
this.controls.add(btn);
btn.click + = evh;
}

}

void btn_click(object sender,eventargs e)
{
string a = string.empty;
}




干杯

如果你得到答案,请标记向上

标签:以及动态创建创立按钮



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

文件上传问题,找不到文件(File Upload problem, file not found)

problem hi, from my asp.net form, i am trying ...

请在下拉列表中解决错误(please solve error in dropdown list)

problem please solve the error i have 2 dropdownlist. in ...

更改属性上的ReadOnly属性不起作用(Change ReadOnly attribute on property doesn't work)

problem i''ve tried to change a readonly attr ...

我如何分割我的表格(how do i split my form)

problem ok i want to split my form in half so on the left ...

将templatefield动态添加到gridview(adding templatefield dynamically to gridview)

problem give the tutorial to add template field, controls ...

在c#的帮助下导出pdf文件(Export pdf File with the help of c#)

problem how to convert more then one pdf file ...

计算中间日期并在gridview中显示它(calculate inbetween date and show it in gridview)

problem hello friends, by using date time pi ...

为它的学生项目(Project for an it student)

problem hi, i m an bsc it student ,as many of you will kn ...

单击更改图标通知(Change Icon Notify on Click)

problem is possible changer notifyicon1 on click the prog ...

如果用户已经登录,则在同一浏览器中重定向到主页asp .net c#(Redirect to Home page if user already logged in, in same browser asp .net c#)

problem i want to make application that if user in logged ...

需要帮助来使用MySqlTransaction(Need help in using MySqlTransaction)

problem hi all i would like to use mysqltransaction in my ...

如何使用VBscript导航Windows注册表树(How to navigate Windows registry tree with VBscript)

problem does anyone know how to go about navigating the w ...

silverlight内存泄漏(silverlight memory leak)

problem hi, i was creating a dependencyproperty changed w ...

如何在json中获取单个元素(How to get single element in json)

problem in wcf service i am getting json response in that ...

双击文件打开程序,如何在c#中实现(double click on file opens program, how to implement in c#)

problem usually when a program in installed, you can clic ...

如何在我添加的现有数据表中添加新列,但它是bofore行的结尾(how add new column in existing datatable i added but its is come end of the bofore rows)

problem my datatable contans two columns id value ----- ...

如何在c#.net中的现有页面中打开新网址(How to open new url in existing page in c#.net)

problem hi to all, this is mdsiddiqali.i added new page n ...

DeleteObject方法不会删除任何内容 - 请帮忙。(DeleteObject method doesn't delete anything - please help.)

problem hello. i am trying to create method that will del ...

项目模板类型在C#中使用?(PROJECT TEMPLATE TYPE TO USE IN C#)

problem edit; okay i am creating a dock for programs to r ...

如何在我添加的现有数据表中添加新列,但它是bofore行的结尾(how add new column in existing datatable i added but its is come end of the bofore rows)

problem my datatable contans two columns ; id value ---- ...