主题:请问C#里可不可以实现刷新页面
ahnan
[专家分:0] 发布于 2006-11-01 15:17:00
请问C#里在没有可以实现刷新页面的代码,如果有请各位大虾发发好心给我一个,谢谢
回复列表 (共3个回复)
沙发
yizhinantian [专家分:640] 发布于 2006-11-01 19:08:00
好像没有直接刷新页面的方法,可以重新定向到当前页面来实现
板凳
tujun [专家分:1190] 发布于 2006-11-02 15:02:00
MSDN里面的:
使用客户端脚本的回发的示例请参见
生成用于回发的客户端脚本语言
C#
Visual Basic
全部显示
以下示例创建了一个自定义链接按钮,它通过客户端脚本(JScript、JavaScript)启动回发。若要生成该示例,请参见服务器控件示例中的说明。
有关与本例类似但从 WebControl 派生控件的示例,请参见呈现服务器控件示例。
[C#]
using System;
using System.Web.UI;
using System.Collections;
namespace CustomControls {
public class MyLinkButton: Control, IPostBackEventHandler{
// Defines the Click event.
//
public event EventHandler Click;
// Invokes delegates registered with the Click event.
//
protected virtual void OnClick(EventArgs e) {
if (Click != null) {
Click(this, e);
}
}
// Method of IPostBackEventHandler that raises change events.
//
public void RaisePostBackEvent(string eventArgument){
OnClick(new EventArgs());
}
protected override void Render(HtmlTextWriter output) {
output.Write("<a id=\"" + this.UniqueID + "\" href=\"javascript:" + Page.GetPostBackEventReference(this) +"\">");
output.Write(" " + this.UniqueID + "</a>");
}
}
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web.UI
Imports System.Collections
Namespace CustomControls
Public Class MyLinkButton
Inherits Control
Implements IPostBackEventHandler
' Defines the Click event.
'
Public Event Click As EventHandler
' Invokes delegates registered with the Click event.
'
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
' Method of IPostBackEventHandler that raises change events.
'
Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
OnClick(New EventArgs())
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write(("<a id=""" & Me.UniqueID & _
""" href=""javascript:" & _
Page.GetPostBackEventReference(Me) & """>"))
output.Write((" " & Me.UniqueID & "</a>"))
End Sub
End Class
End Namespace
在页上使用控件
以下 ASP.NET 页使用上一示例中创建的自定义链接按钮。
[C#]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="C#" runat=server>
private void Button_Click(Object sender, EventArgs e) {
TextBox.BackColor = System.Drawing.Color.LightGreen;
TextBox.Text = "The link button caused postback.";
}
</script>
<html>
<body>
<form runat=server>
Here is the custom link button.<br>
<Custom:MyLinkButton Id = "Link" OnClick = "Button_Click" runat=server/>
<br><br>
<asp:TextBox id = "TextBox" Text = "Click the link" Width = "200" BackColor = "Cyan" runat=server/>
<br>
</form>
</body>
</html>
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="VB" runat=server>
Private Sub Button_Click(sender As Object, e As EventArgs)
TextBox.BackColor = System.Drawing.Color.LightGreen
TextBox.Text = "The link button caused postback."
End Sub
</script>
<html>
<body>
<form runat=server>
Here is the custom link button.<br>
<Custom:MyLinkButton Id = "Link" OnClick = "Button_Click" runat=server/>
<br><br>
<asp:TextBox id = "TextBox" Text = "Click the link" Width = "200" BackColor = "Cyan" runat=server/>
<br>
</form>
</body>
</html>
3 楼
yunzhongyu [专家分:0] 发布于 2006-11-03 10:16:00
首先你提的问题并不明确,有两种理解:
1.你指的是用C#实现的ASP.NET WEB页面,需要在后台代码中进行刷新,如果是客户端主动刷新,也就是由浏览器发起的刷新请求,其实现方法简单的我都不好意思说了。如果是想从服务器上PUSH一个刷新请求,使所有的客户端同时进行刷新,我还没找到实现方法,具我了解现在的BS结构是无法实现的。
2.你指的是WINFORM(WINDOWS 应用程序)需要刷新一下页面,比如说一个操作的MessageBox已经提示,后台在进行数据运算,这时你需要将前台页面的MessageBox刷新掉等,可以使用this.Refresh()方法。
我来回复