我有一个小聊天应用程序,当任何消息到达时,自动更新没有发生

时间:2016-02-12 11:50:31

标签: c# asp.net

当消息显示在同一台机器上而不是在不同的机器上时,我有简单的div。我想每1秒更新或刷新div

<script language="javascript" type="text/javascript">
function popup() {
    var a = window.open("index.aspx",'width=600,height=500,left=350,top=120')
}
</script>

<asp:TextBox ID="TextBox1" runat="server" Height="200px" Width="353px" TextMode="MultiLine" ReadOnly="true"></asp:TextBox>
<br />
<br />
Message: <asp:TextBox ID="TextBox3" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="Message cannot blank" ControlToValidate="TextBox3" runat="server" />
<br />
<br />
<div align="center">
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>

背后的代码

protected void Button1_Click(object sender, EventArgs e)
{
   string name = Session["AdminId"].ToString();

    string message = TextBox3.Text;
    string my = name + "::" + message;

    Application["msg"] = Application["msg"] + my + Environment.NewLine;

    TextBox1.Text = Application["msg"].ToString();
    TextBox3.Text = "";
}

1 个答案:

答案 0 :(得分:0)

使用webforms执行此操作的最简单方法是将div放在updatepanel中,并使用timer每隔x秒触发一次事件。

$data = array(
    array( 'item' => 'Server', 'cost' => 10000, 'approved by' => 'Joe'),
    array( 'item' => 'Mt Dew', 'cost' => 1.25, 'approved by' => 'John'),
    array( 'item' => 'IntelliJ IDEA', 'cost' => 500, 'approved by' => 'James')
);

$fp = fopen('file.csv', 'w');
$i = 0;
foreach ($data as $fields) {
    if($i == 0){
        fputcsv($fp, array_keys($fields));
    }
    fputcsv($fp, array_values($fields));
    $i++;
}

fclose($fp);

然而,就像有人在评论中写道,值得看一下<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> <ContentTemplate> <!-- html --> </ContentTemplate> </asp:UpdatePanel> <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick"> </asp:Timer>