This is an old post, however I do not think that he fully answered. First, in ASP.NET WebForms, you send an HTTP GET
to a web server that processes your request and outputs client-side HTML code to render the browser.
When you interact with a server control, the values ββare contained in a hidden VIEWSTATE
input VIEWSTATE
for properties (such as a boolean value for Enabled
).
When you click the button, it sends an HTTP POST
request to the web server on the same page. This is why the Page_Load
event is fired when a button is clicked.
After the HTTP POST
request has been processed, it will then return the HTML code to re-render the browser. For this reason, if you have the following code in the Page_Load
event:
if (Page.IsPostBack) { Button3.Enabled = false; }
It will not show the user that it has been disconnected until the HTTP POST
request has been processed and the updated client-side code has been returned.
From the initial question, it turned out that the server received a few seconds to return the answer, so when you clicked the button again, it would be possible to trigger several postback events. A simple (but annoying) way to solve your problem would be to have a regular HTML button
that performs a function
in JavaScript, which disables it and fires the onclick event on the server side. Then the problem with this would be that when the HTTP POST
request returns a response, it would display the regular HTML button
as being included. To solve this problem, you can simply disable it in JavaScript using the built-in ASP.NET code. Here is an example:
.Aspx file
<button id="clientButton" onclick="javascript:update();" /> <asp:Button ID="serverButton" OnClick="serverButton_OnClick" runat="server" /> <script type="text/javascript"> <% if (Page.IsPostBack) { %> document.getElementById("clientButton").enabled = false; <% } %> function update() { document.getElementById("clientButton").enabled = false; document.getElementById("<%= serverButton.ClientID %>").click(); } </script> <style type="text/css"> #<%= serverButton.ClientID %> { visibility: hidden; display: none; } </style>
.ASPX.CS File
protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) {
Nathangrad
source share