C# 无法启用/禁用ASP.net按钮,是否在计时器勾选方法中动态使用后端中的C?

C# 无法启用/禁用ASP.net按钮,是否在计时器勾选方法中动态使用后端中的C?,c#,asp.net,timer,C#,Asp.net,Timer,在下面的代码中,我希望按钮1被禁用,直到计时器倒计时到“sdur”分钟,比如说3分钟。但是按钮的行为没有改变。请帮忙。我还尝试了Button1。Enabled=true;仍然没有效果。提前谢谢 protected void timer1_tick(object sender, EventArgs e) { int rem_minutes = (Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now

在下面的代码中,我希望按钮1被禁用,直到计时器倒计时到“sdur”分钟,比如说3分钟。但是按钮的行为没有改变。请帮忙。我还尝试了Button1。Enabled=true;仍然没有效果。提前谢谢

protected void timer1_tick(object sender, EventArgs e)
{
    int rem_minutes = (Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes;
    if (sdur <rem_minutes)
        Button1.Attributes.Add("Enabled", "False");

    else
        Button1.Attributes.Add("Enabled", "True");
    if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString())))
    {
        lblTimer.Text = string.Format("Time Left: 00:{0}:{1}", ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).Seconds).ToString());

    }
    else
    {
        timer1.Enabled = false;
        Response.Redirect("Logout.aspx");
    }      
}
protectedvoid timer1\u tick(对象发送方,事件参数e)
{
int rem_minutes=(Int32)DateTime.Parse(Session[“timeout”].ToString()).Subtract(DateTime.Now).TotalMinutes;
if(sdur DateTime.Compare(DateTime.Now,DateTime.Parse(Session[“timeout”].ToString()))
{
lblTimer.Text=string.Format(“左时间:00:{0}:{1}”,((Int32)DateTime.Parse(Session[“timeout”].ToString()).Subtract(DateTime.Now.TotalMinutes).ToString(),((Int32)DateTime.Parse(Session[“timeout”].ToString()).Subtract(DateTime.Now).Seconds.ToString());
}
其他的
{
timer1.Enabled=false;
重定向(“Logout.aspx”);
}      
}
我也试过用java脚本

    htmlTable.Append(" <script> var seconds = " + ttime + "; function secondPassed() { var minutes = Math.round((seconds - 30) / 60);  var remainingSeconds = seconds % 60; if (remainingSeconds < 10) { remainingSeconds = '0' + remainingSeconds; }  document.getElementById('countdown').innerHTML = 'Time Left :- ' + minutes + ':' + remainingSeconds; if (seconds == 0) { clearInterval(countdownTimer) ; document.getElementById('Button1').click() ; } else  { seconds--; } if(minutes<=" + sdur + ") { document.getElementById('Button1').disabled=false; }  } var countdownTimer = setInterval('secondPassed()', 1000); </script>");

htmlTable.Append(“var seconds=“+ttime+”;函数secondPassed(){var minutes=Math.round((seconds-30)/60);var remainingSeconds=seconds%60;if(remainingSeconds<10){remainingSeconds='0'+remainingSeconds;}document.getElementById('countdown')。innerHTML='Time Left:-'+minutes+':'+remainingSeconds;if(秒==0){clearInterval(countdownTimer);document.getElementById('Button1')。单击();}否则{seconds--;}如果(分钟使用自动回发的计时器的OnTick事件:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" OnTick="CodeBehindFunction" >
</asp:Timer>
这对我有用


但最好是使用客户端编程(javascript或任何适合您的东西)因此您不必太频繁地发回。

使用调试器并逐步完成代码。
timer1\u tick事件和间隔触发计时器。
也许您可以看到在逐步完成代码时出错的地方。
我认为您应该将按钮放在内容模板中,以便在更新面板已更新。如果您使用的是updatepanel
,则此代码将仅在服务器端运行,并且在刷新页面之前不会影响客户端。我使用了调试器,逻辑工作正常。我认为正如Rick所说,在刷新页面之前不会影响。那么@Rick的解决方案是什么?您使用的是MVC还是Web表单?因为有不同的刷新方式取决于您可能希望在javascript中执行此操作。您仍然需要检查服务器上是否允许此操作,因为聪明的用户可以操纵javascript来更改超时和按钮可用性。
protected void CodeBehindFunction(object sender, EventArgs e)
{
    if(Get_timeElapsed() > timeOut)
        btnMAJ.Enable = false;
}