简体   繁体   中英

Force a post back when checkbox.checked value is set to true

I have a method that changes the checked attribute of a checkbox like this

checkbox1.checked = true;

and then I have the eventhandler method :

checkbox1_checkedChanged(object obj, EventArgs args)

However, when I perform the changing of checked attribute to true, the event handler does not fire up.

How can I achieve this?

I suspect that your function that does checkbox1.checked = true; is running server side, so could never cause a postback.

If this is the case, and you just wish to invoke the code in your checked changed handler, you could just call the function directly, or refactor the code out into a common function.

您需要在元素上将AutoPostBack设置为true

<asp:CheckBox id="checkbox" runat="server" AutoPostBack="true" />

What you need to do is simply call the method handler you want fired because you're already server-side and so no event is going to fire because of a property value change. So, try this after setting checked to true:

checkbox1_checkedChanged(checkbox1, new EventArgs());

Well, you talk about "post back" so I assume you are creating a web page.

The AutoPostBack property only indicates that the post back event will fire when the control's property is changed through the control itself on the web page since the event handler is bound to the control only.

So, changing the control's property dynamically (with code on the server side) will never trigger the event handler. Instead, you could just call the handler right after the change (like what Justin Harvey said).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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