【发布时间】: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){}吞噬的异常...检查我的答案