简体   繁体   English

ASP.NET尝试使用ASP Button动态创建按钮

[英]ASP.NET Trying to create buttons dynamically with ASP Button

I was trying to create "asp:buttons" dynamically with special asp tags (<%%>) as I am using a loop in the codebehind to generate x amount of buttons based on the database. 我试图使用特殊的asp标记(<%%>)动态创建“ asp:buttons”,因为我在代码隐藏中使用了一个循环来基于数据库生成x个按钮。 However, when the special tag tries to write out the asp button, the button does not work. 但是,当特殊标记试图写出asp按钮时,该按钮将不起作用。

I know that the problem is due to it not converting when it's being written out. 我知道问题是由于它在写出来时没有转换。

For example, when I view source an asp button is suppose to look like this: 例如,当我查看源代码时,asp按钮应该看起来像这样:

<input type="submit" name="ctl00$ContentPlaceHolder1$Button1" value="Hello" id="ContentPlaceHolder1_Button1" class="button" style="color:White;background-color:Transparent;border-style:None;" />

but the asp button being written on the view source was shown as: 但是在视图源上编写的asp按钮显示为:

<asp:Button ID='Button1' CssClass='button' BackColor='Transparent' runat='server' BorderStyle='None' ForeColor='white' Text='Hello' OnCommand='Button1_Click' CommandArgument='cat0002' />

If there are other ways to create the buttons please do recommend me too as I have to create them dynamically while trying to display them in the individual table data I created. 如果还有其他创建按钮的方法,也请向我推荐,因为我必须动态创建它们,同时尝试在创建的各个表数据中显示它们。 And I would need to pass the values through to the code behind as I have to call the SQL database when its clicked. 而且我需要将值传递给后面的代码,因为单击该数据库时必须调用它。

try it: 试试吧:

protected void Page_Load()

    {
      Button ButtonChange = new Button();

      ButtonChange.Text = "New Button";
      ButtonChange.ID = "btnNew_" + i.ToString();
      ButtonChange.Font.Size = FontUnit.Point(7);
      ButtonChange.ControlStyle.CssClass = "button";
      ButtonChange.Click += new EventHandler(test);
    }

more info : http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx 更多信息: http : //msdn.microsoft.com/zh-cn/library/kyt0fzt1.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM