C#中button事件的问题?

cqnucsmoon 2008-07-10 08:35:32
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = "正在执行操作....";
DoIt(Server.MapPath("/"));
Button1.Text = "操作执行完毕....";
}

我的问题是:

点击button1后,第一句 Button1.Text = "正在执行操作...."; 根本就不会改变button1的Text,而是直接显示最后一句:Button1.Text = "操作执行完毕....";

好象在WEB中不会象在WIN32的程序一样显示执行的过程??应该如何做才能实现???
谢谢!
...全文
259 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlong224 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 fellowcheng 的回复:]
给你个异步的吧

C# codeusing System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;

public partial class ICallbackEventHandlerTest : System.Web.UI.Page, ICal…
[/Quote]
zryStar 2008-07-11
  • 打赏
  • 举报
回复
高手果然厉害,学习 这种做法确实不错
请叫我低调 2008-07-11
  • 打赏
  • 举报
回复
真是强悍 为什么 老是感觉 自己掌握的跟鄙人的差距 好大的
cqnucsmoon 2008-07-11
  • 打赏
  • 举报
回复
谢谢fellowcheng 。
fellowcheng 2008-07-10
  • 打赏
  • 举报
回复
给你个异步的吧
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;

public partial class ICallbackEventHandlerTest : System.Web.UI.Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.GetCallbackEventReference(this, "ShowReceipt", "ShowReceiptList", "context");
}

#region 异步调用btn

public string strCallback;
public string GetCallbackResult()
{
return this.Button1.ID;
}

public void RaiseCallbackEvent(string eventArgument)
{
Button1_Click(null, null);
}

protected void Button1_Click(object sender, EventArgs e)
{
DoIt(Server.MapPath("/"));
Thread.Sleep(2000);
//ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('后台方法');</script>");
}
#endregion
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ICallbackEventHandlerTest.aspx.cs" Inherits="ICallbackEventHandlerTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
//失效时间
var nTime=2000;
function SetBtnDisabled(s_id)
{
var btn = document.getElementById(s_id);
if(btn)
{
btn.disabled=true;
// setTimeout("SetBtnEnabled('" + s_id + "')",nTime);
}
}

function SetBtnEnabled(ch_id)
{
var btn = document.getElementById(ch_id);
if(btn)
{
btn.disabled=false;
btn.value= "操作执行完毕....";
}
}

function buttonClick()
{
var bt = event.srcElement;
if(bt)
{
bt.value = "正在执行操作...."; alert(bt.value);
SetBtnDisabled(bt.id)
GetServerReceipt(bt.id);
}
}

//调用后台方法
function GetServerReceipt(receiptID)
{
var context = '';
WebForm_DoCallback('__Page',receiptID,ShowReceiptList,context,null,false);
}

//后台回调方法
function ShowReceiptList(ShowReceipt, context)
{
SetBtnEnabled(ShowReceipt);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="点我" OnClientClick="return buttonClick()"/></div>
</form>
</body>
</html>
fellowcheng 2008-07-10
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
//失效时间
var nTime=2000;
function SetBtnDisabled(s_id)
{
var btn = document.getElementById(s_id);
if(btn)
{
btn.disabled=true;
setTimeout("SetBtnEnabled('" + s_id + "')",nTime);
}
}
function SetBtnEnabled(ch_id)
{
var btn = document.getElementById(ch_id);
if(btn)
{
btn.disabled=false;
btn.value= "操作执行完毕....";
}
}

function buttonClick()
{
if(!confirm("是否回发服务器?"))
return false;
else
{
var bt = event.srcElement;
if(bt)
{
bt.value = "正在执行操作...."; alert(bt.value);
SetBtnDisabled(bt.id)
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="divToolbar" runat="server">
<asp:Button ID="Button1" runat="server" Text="点我" OnClientClick="return buttonClick()" OnClick="Button1_Click" /></div>
</form>
</body>
</html>


后台
 protected void Button1_Click(object sender, EventArgs e)
{
DoIt(Server.MapPath("/"));

}
cqnucsmoon 2008-07-10
  • 打赏
  • 举报
回复
好象还是看不出效果来。。。。
sharpblade 2008-07-10
  • 打赏
  • 举报
回复
System.Threading.Thread.Sleep(5000);
在Button1.Text = "正在执行操作...."; 加上面个,睡他个5s
fellowcheng 2008-07-10
  • 打赏
  • 举报
回复
语句已经执行完了,看不出来的
如果是webForm的话,可以在前台可以用js的setTimeout函数实现类似效果
cherry_j 2008-07-10
  • 打赏
  • 举报
回复
Button1.Text = "正在执行操作....";
DoIt(Server.MapPath("/"));
Button1.Text = "操作执行完毕....";

这三句执行的时间太短
你放一个Sleep可能会看到效果
amandag 2008-07-10
  • 打赏
  • 举报
回复
web应用程序是基于请求响应的,而不是像CS是交互式的
cqnucsmoon 2008-07-10
  • 打赏
  • 举报
回复
谢谢大家,是web。
能不能讲解详细点,初次学习呀。

很多都不懂。
dotnet东哥 2008-07-10
  • 打赏
  • 举报
回复
哦,你也可以用javascript实现
lawbc 2008-07-10
  • 打赏
  • 举报
回复
操作速度快的话就看不出来的
一般都可以用ajax做
可以根据ajax的状态值显示你要的文本
比如4表示数据接收完成
greystar 2008-07-10
  • 打赏
  • 举报
回复
你这个是web?
如果是的话,你可以使用一些ajax的控件,可以达到你的效果.

如果是winform,那不显示第一句话也可理解.那是因为处理的速度太快,UI来不及反应,就执行到最后一句代码了.

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧