【问题标题】:how to disable a linkButton in aspx when a certain condition is set设置特定条件时如何禁用aspx中的linkBut​​ton
【发布时间】:2020-05-21 07:32:57
【问题描述】:

让我解释一下我的问题,我有一个链接按钮:

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

当我单击此 LinkBut​​ton 时,我设置了一个命令,然后我使用 JQuery 检查是否单击了该按钮以显示一条消息,等待机器联机以获取更新的数据,然后禁用该按钮:

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);

这是来自网络服务的方法,我在其中检查数据库中是否设置了某个数据(CMD_Date_hour_Ok != null) 来更新机器在线的消息并启用返回按钮。

  [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;
  }

现在我需要禁用 LinkBut​​ton 并且可能将颜色更改为灰色,以便在我单击它时它被禁用并在机器在线时启用它。 我怎样才能做到这一点? 谢谢

【问题讨论】:

  • 机器在线时是否退出区间?
  • @SelimYıldız 不,我没有

标签: asp.net asplinkbutton


【解决方案1】:

您需要在每个间隔更改ForceUpdate的禁用属性,如下所示:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2011-08-27
    相关资源
    最近更新 更多