简体   繁体   English

如何在ASP.NET C#中向按钮动态添加AutoPostBack = true

[英]How to dynamically add AutoPostBack = true to button in ASP.NET C#

I have the following code. 我有以下代码。 My question is how can I dynamically add the autopostback property to the button? 我的问题是如何动态地将autopostback属性添加到按钮?

Button button = new Button();
button.ID = "Button" + i;
button.Text = "Save";
button.Click += SaveButton_Click;
PlaceHolder1.Controls.Add(button);

Something like this button.autoPostBack = true; 像这样的东西button.autoPostBack = true;

By default an asp.net button control has AutoPostBack property equal to true . 默认情况下,asp.net button控件的AutoPostBack属性等于true Though this property doesn't appear in button properties. 虽然此属性不会出现在button属性中。 If a Click event attached to a button , it will always do a server-trip . 如果附加到buttonClick事件,它将始终执行server-trip

As a side note! 作为旁注! if you want that you button don't do a server-trip then you can disable its AutoPostBack property by removing its Click event and you could add OnClientClick event to run client-side code 如果你想要你的button不执行server-trip那么你可以通过删除它的Click事件来禁用它的AutoPostBack属性,你可以添加OnClientClick事件来运行client-side代码

<asp:Button ID="Button1" runat="server" OnClientClick="myJSfunction(); return false" Text="Button" />

You can use Attributes.Add in button. 您可以在按钮中使用Attributes.Add。 ie

button.Attributes.Add("AutoPostback","true");

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

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