【问题标题】:How to disable the button如何禁用按钮
【发布时间】:2013-08-02 10:52:54
【问题描述】:

如果检索到的日期小于当前日期,我想停用 button6,我为此使用了以下代码,但它不起作用。请帮我找出错误。

protected void Button6_Click1(object sender, EventArgs e)
{
    MySqlConnection connection = new MySqlConnection("server=localhost; database=e-learningsystem; uid=root; password=123;port=3307;");
    connection.Open();
    try
    {
        MySqlCommand cmd = new MySqlCommand("SELECT Date FROM fundamentals of is WHERE ChapNo=Chapter 1", connection);
        string date = Convert.ToString(cmd.ExecuteScalar());
        //date = cmd;
        if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)
        {
            DownLoadFileFromServer("~/NewFolder1/" + "Fundamentals of IS.pdf");
        }
        else
        {
            Button6.Enabled = false;
        }
    }  
    catch (Exception ex)
    {
        // file IO errors

    }
}

这是serverMapPath 代码

public static string ServerMapPath(string path)
{
    return HttpContext.Current.Server.MapPath(path);
}

public static HttpResponse GetHttpResponse()
{
    return HttpContext.Current.Response;
}
public static void DownLoadFileFromServer(string fileName)
{
    //This is used to get Project Location.
    try
    {
        string filePath = ServerMapPath(fileName);
        //This is used to get the current response.
        HttpResponse res = GetHttpResponse();
        res.Clear();
        res.AppendHeader("content-disposition", "attachment; filename=" + filePath);
        res.ContentType = "application/octet-stream";
        res.WriteFile(filePath);
        res.Flush();
        res.End();
    }
    catch (Exception ex)
    {

    }
}

【问题讨论】:

  • 您确定按钮单击是要禁用该按钮吗?难道你不想在页面加载时这样做吗?
  • 为什么要禁用刚刚点击的按钮?
  • @oGJo,例如防止其他点击,直到操作完成?!
  • 你得到了什么作为输出。您的代码是否成功运行但按钮未禁用或您是否收到任何错误???
  • 您很可能会遇到一个被 catch(Excpetion ex){} 吞噬的异常...检查我的答案

标签: c# asp.net mysql


【解决方案1】:

MSDN 答案:

我认为这会对你有所帮助

int result = DateTime.Compare(date1, date2);

string relationship;
if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

【讨论】:

    【解决方案2】:
    MySqlCommand cmd = new MySqlCommand("SELECT Date FROM fundamentals of is WHERE ChapNo=Chapter 1", connection);
    string date = Convert.ToString(cmd.ExecuteScalar());
    //date = cmd;
    if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)
    

    你确定要去Convert.ToDateTime(cmd)吗?

    你不想Convert.ToDateTime(date)吗?

    【讨论】:

    • 这也 nt wrking MySqlCommand cmd = new MySqlCommand("SELECT Date FROM basics of is WHERE ChapNo=Chapter 1", connection);字符串日期 = Convert.ToString(cmd.ExecuteScalar()); if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now)
    • 您在dateConvert.ToDateTime(date) 中有什么价值?
    • date 是保存在 DB Convert.ToDateTime(date) 中的字符串值
    • Convert.ToDateTime(date).CompareTo(System.DateTime.Now) > 0 ?你想知道,Button6.Enabled = false; 已执行,但按钮仍处于启用状态?
    • 如果你告诉我们,date is the value saved in DB 看起来很适合你,否则你骗了我们
    【解决方案3】:

    以下行可能存在逻辑错误,因为您将cmd 变量转换为DateTime 而不是您的命令返回的date

      if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)
    

    改用它可能会解决您的问题:

      if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0)
    

    【讨论】:

      【解决方案4】:

      如果要在检索到的日期小于当前日期时停用按钮,则应反转 if 语句条件,并使用 date 字符串而不是 cmd

      if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) < 0)
      

      if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) >= 0)
      

      如果日期大于或等于当前日期,您可以将文件下载到服务器,如果日期小于当前日期,则禁用该按钮。

      或者只是反转整个 if :

          if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0)
          {
              Button6.Enabled = false;
          }
          else
          {
              DownLoadFileFromServer("~/NewFolder1/" + "Fundamentals of IS.pdf");
          }
      

      【讨论】:

      • 这样对吗??? "从 IS WHERE ChapNo=Chapter 1 的基础中选择日期"
      【解决方案5】:

      您可能想要转换 date:

      if (Convert.ToDateTime(date).CompareTo(System.DateTime.Now) < 0)
      

      每次点击该行时都会引发异常。

      【讨论】:

        【解决方案6】:

        一个选项是在 javascript 中禁用它(在点击事件中) 另一种选择是在 Page_Onload 中编写相同的代码(并检查 ISPostback)

        【讨论】:

          【解决方案7】:

          好吧,除了大家提到的if (Convert.ToDateTime(cmd).CompareTo(System.DateTime.Now) &lt; 0) //need to use date instead of cmd 的明显问题外,代码还包含一个反模式,它使用了过于笼统的catch 块。

          代码正在捕获一个异常来处理 IO 异常。但是如果还有其他类型的例外呢?我开始怀疑由于某种原因Convert.ToDateTime 正在抛出一个错误,该错误被您认为捕获only IO 异常的catch 语句所吞噬。事实上,它会捕获任何类型的异常,包括可能通过调用Convert.ToDateTime 引发的FormatException

          但它可能是SqlException,我接下来会解释:

          这行也有无效的SQL:

          MySqlCommand cmd = 
              new MySqlCommand("SELECT Date FROM fundamentals of is WHERE ChapNo=Chapter 1",
              connection);
          

          我认为应该是:

          MySqlCommand cmd = 
              new MySqlCommand("Select Date from fundamentals where ChapNo='Chapter 1'",
              connection); //that is, if ChapNo is a STRING/TEXT!
          

          执行此命令将引发一个错误,由于您的 catch 语句,您将无法检测到该错误。您需要使您的 catch 语句尽可能具体。

          【讨论】:

            【解决方案8】:

            Button6.Enabled = false; 位于 try catch 中,而 throw an Exception 却没有。尝试将其复制到您的方法顶部,如下所示,以查看它是否有效:

            protected void Button6_Click1(object sender, EventArgs e)
            {
                Button6.Enabled = false;
                MySqlConnection connection = new MySqlConnection("server=localhost;     ..
            ..
            }
            

            如果有效,则在 try catch 内的语句中查找错误。

            还要仔细检查您的aspx code。也许您引用了一个名为 Button6_Click 的方法,而不是发布的 Button6_Click1(可能是 Button6_Click 的副本)

            【讨论】:

              猜你喜欢
              • 2015-02-10
              • 1970-01-01
              • 2011-05-22
              • 2012-11-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多