我正在开发聊天应用程序。当用户点击在线用户链接时,弹出窗口(动态控件)将显示在更新面板中。但是由于计时器事件,在回发后动态创建的控件会从页面中删除。
因此,在回发事件中,我将再次使用相同的 ID 创建它们。但由于这个原因,每当我试图在用户控件中输入文本框时。页面刷新后,文本框的焦点被转移。所以我不得不一次又一次地点击 tetxbox 在里面输入一些东西。
定时器事件用于查找数据库中的当前用户是否有新消息。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChatWindow.ascx.cs" Inherits="ChatWindow" %>
<div style="width: 211px;border:thick solid #ffa800">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Hi <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>..<br />
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Height="172px" Width="186px"></asp:TextBox><br /><br />
<asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick" Enabled="true"> </asp:Timer>
</ContentTemplate></asp:UpdatePanel> </div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="148px"></asp:TextBox>
<asp:Button ID="BtnSend" runat="server" onclick="BtnSend_Click" Text="Send" /> </ContentTemplate>
</asp:UpdatePanel>
更多代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections;
public partial class ChatWindow : System.Web.UI.UserControl
{
int userid, toid;
SqlConnection con = new SqlConnection("Data Source=LN-PUN-D036\\SQLEXPRESS;initial catalog=WCFChat;uid=sa;pwd=imgpoint1*");
string windowID = "";
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
userid = Convert.ToInt32(Session["UserId"].ToString());
this.windowID = this.ID;
toid =Convert.ToInt32( this.windowID.Substring(5));
this.Label1.Text = this.ID.ToString();
}
}
protected void BtnSend_Click(object sender, EventArgs e)
{
string temp = this.TextBox2.Text;
var service = new ChatService.Service();
service.sendmsg(this.TextBox1.Text, userid, toid);
this.TextBox2.Text += "Me : " + this.TextBox1.Text + "\r\n";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
con.Open();
// SqlCommand cmd = new SqlCommand("select Msg,Msg_Id from Message where Msg_Id=(SELECT TOP 1 Msg_Id FROM Message where Msg_To ='" + userid + "' and ReadOrNot='0' ORDER BY Msg_Id DESC) ", con);
SqlCommand cmd = new SqlCommand("select Msg,Msg_Id,Msg_From from Message where Msg_To ='" + userid + "' and ReadOrNot='0' and Msg_From = '" + toid + "'", con);
SqlConnection con2 = new SqlConnection("Data Source=LN-PUN-D036\\SQLEXPRESS;initial catalog=WCFChat;uid=sa;pwd=imgpoint1*");
SqlDataReader dr = cmd.ExecuteReader();
Hashtable hash = new Hashtable();
string name = "";
while (dr.Read())
{
hash = (Hashtable)Application["UserHash"];
foreach (DictionaryEntry de in hash)
{
if (de.Key.ToString().Equals(dr[2].ToString().Trim()))
{
name = de.Value.ToString();
break;
}
}
this.TextBox2.Text += name + " :" + dr[0].ToString() + "\r\n";
string msgid = dr[1].ToString();
con2.Open();
SqlCommand cmd2 = new SqlCommand("update Message set ReadOrNot='1' where Msg_Id='" + msgid + "'", con2);
int i = cmd2.ExecuteNonQuery();
con2.Close();
}
con.Close();
}
}
protected void craetecontrols() // this function is in main page in which i am adding above user control
{
foreach (DictionaryEntry obj in arr)
{
Panel p1 = new Panel();
p1.ID = "PanelChat" + obj.Key.ToString();
ChatWindow mycontrol = (ChatWindow)LoadControl("~/ChatWindow.ascx");
mycontrol.ID = "ChWId" + obj.Key.ToString();
p1.Controls.Add(mycontrol);
p1.Style.Add("position", " absolute");
p1.Style.Add("bottom", "0");
p1.Style.Add("right", obj.Value.ToString());
UpdatePanel4.ContentTemplateContainer.Controls.Add(p1);
}
}