如何在Asp.net中创建动态按钮数组? [英] how to create dynamic button array in Asp.net?

查看:145
本文介绍了如何在Asp.net中创建动态按钮数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我们如何在网页上包含一组按钮?这些按钮组必须动态加载到页面中.如何为此目的创建动态按钮数组?

请引导我.

在此先感谢.

Hi All,

How can we include a set of buttons to a web page? These set of buttons must load in to the page dynamically.How can we create a dynamic button array for this purpose?

Please Guide me.

Thanks in Advance.

推荐答案

使用动态按钮的最佳方法是DataList使用包含Button元素的项目模板制作一个DataList.将数据列表与按钮数据收集的dataSource绑定.作为
The Best way to use Dynamic Buttons is DataList Make a DataList with item template containing a Button element. Bind the datalist with dataSource of buttons data collection. As
<asp:datalist id="dlButtons" runat="Server" onitemcommand="dlButtons_Command" xmlns:asp="#unknown">
      <itemtemplate>
              <asp:button id="cmdSubmit" text=''<%#   Eval( "Text" ) %>'' runat="server" commandname=''<%# Eval("Command") %>'' />
      </itemtemplate>




现在在页面加载事件的服务器端处理只需将数据源与数据列表绑定为As




Now Handling on server Side on Page Load Event just bind the data source with Data List like As

 List<Button> buttons = new List<Button>();
 Button b1=new Button();
 b1.Text="Previous";
 b1.Command="Prev";
 buttons.Add(b1);

 Button b2=new Button();
 b2.Text="Next";
 b2.Command="Next";
 buttons.Add(b2);

dlButtons.DataSource=buttons;
dlButtons.DataBind();



现在您可以在此处编写按钮操作代码



Now You can Write Button Action Code here

 private void dlButtons_Command(object sender , DataListCommandEventArgs e)
{
       if(e.Command == "Prev")
       {
             //Write your code here
       }
      if(e.Command == "Next")
      {
             //Write Your Code here
      }
}




询问是否有任何问题....




Ask if any issue comes ....


下面的代码可能会帮助您



Listlist =新的List();
for(int运行= 0;运行< = 10;运行++)
{
list.Add(new Button());
}
按钮[] btnArray = list.ToArray();

谢谢
Faisal
Below code Might Help You



Listlist = new List();
for (int runs = 0; runs <= 10; runs++)
{
list.Add(new Button());
}
Button [] btnArray = list.ToArray();

Thanks
Faisal


使用使用System.Web.UI.WebControls;"


按钮button1 = new Button();
button1.ID = btndynamic//创建一个按钮ID
Use "using System.Web.UI.WebControls;"


Button button1 = new Button();
button1.ID = btndynamic //create a buttonid


这篇关于如何在Asp.net中创建动态按钮数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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