简体   繁体   中英

how to disable a linkButton in aspx when a certain condition is set

Let me explain my problem, I have a LinkButton:

<asp:LinkButton ID="ForceUpdate" runat="server" OnClick="ForceUpdateBtn_Click" Text="<%$ Resources:Resource1, ForceUpdate%>" />

when I click on this LinkButton I set a command and then I check with JQuery if the button is clicked to show a message to wait until the machine is online to get the updated data and then disable the button:

window.setInterval(function () {
        $.ajax({
            async: true,
            type: 'POST',
            url: "../../WS/IOT_WebService.asmx/GetUpdateStatusStats",
            data: { mccid: <%=mccIdToJavascript%>, language: '<%=currentCulture%>' }, // mccid: ID machine
            cache: false,
            beforeSend: function () {
            },
            success: function (txt) {                    
                var string = xmlToString(txt);
                string = string.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://tempuri.org/\">", "");
                string = string.replace("<string xmlns=\"http://tempuri.org/\">", "");
                string = string.replace("</string>", "");
                console.log('check is ', <%=checkClick%>);

                var check = <%=checkClick%>;
                if (check) {
                    $('#status-force-update').text(string);
                } else {
                    $('#status-force-update').text('----------');
                }
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            }
        });
    }, 3000);

Here's the method from the webservice where I check if in the db a certain data is set (CMD_Date_hour_Ok != null) to update the message that the machine is online and enable back the button.

  [WebMethod]
  public string GetUpdateStatusStats(string mccid, string language)
  {
    String strResponse = String.Empty;
    CultureInfo currentCulture = new CultureInfo(language);
    Thread.CurrentThread.CurrentCulture = currentCulture;
    Thread.CurrentThread.CurrentUICulture = currentCulture;

    try
    {
      MCC_Machine mcc = MCC_Machine.Retrieve(Convert.ToInt32(mccid));
      CMD_Command cmd = CMD_Command.RetrieveByMCCType(mcc, 14); // 14 means that a ForceUpdate were launched
      if (cmd == null)
      {
        cmd = CMD_Command.RetrieveByMCCType(mcc, 14);
      }

      if (cmd.CMD_Date_hour_Ok != null)
      {
        // machine is online
        strResponse = Resources.Resource1.ForceUpdateStatsOnline.ToString();
      }
      else
      {
        // machine is offline
        strResponse = Resources.Resource1.ForceUpdateStatsOffline.ToString();
      }
    }
    catch
    {
      strResponse = Resources.Resource1.ForceUpdateStatsOffline.ToString();
    }

    return strResponse;
  }

Now I need to disable the LinkButton and maybe change the color to grey to get the idea that it is disabled when I click it and enable it when the machine is online. How can I do that? Thank you

You need to change disabled attribute of ForceUpdate at every interval as following:

window.setInterval(function () {
        $.ajax({
            async: true,
            type: 'POST',
            url: "../../WS/IOT_WebService.asmx/GetUpdateStatusStats",
            data: { mccid: <%=mccIdToJavascript%>, language: '<%=currentCulture%>' }, // mccid: ID machine
            cache: false,
            beforeSend: function () {
            },
            success: function (txt) {                    
                var string = xmlToString(txt);
                string = string.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://tempuri.org/\">", "");
                string = string.replace("<string xmlns=\"http://tempuri.org/\">", "");
                string = string.replace("</string>", "");
                console.log('check is ', <%=checkClick%>);

                var check = <%=checkClick%>;
                if (check) {
                    $('#status-force-update').text(string);
                } else {
                    $('#status-force-update').text('----------');
                }

                if(string =="Online"){ //check is machine online then set disable false
                    $("#<%=ForceUpdate.ClientID %>").attr("disabled", false);
                }else{ // else mean machine is offline
                    $("#<%=ForceUpdate.ClientID %>").attr("disabled", true);
                }
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            }
        });
    }, 3000);

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