[英]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
. 如果附加到
button
的Click
事件,它将始终执行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.