Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态web用户控件由计时器事件刷新,所以我必须在pageload事件中重新创建它们,但如何在textbox1中保持焦点?_C#_Webforms - Fatal编程技术网

C# 动态web用户控件由计时器事件刷新,所以我必须在pageload事件中重新创建它们,但如何在textbox1中保持焦点?

C# 动态web用户控件由计时器事件刷新,所以我必须在pageload事件中重新创建它们,但如何在textbox1中保持焦点?,c#,webforms,C#,Webforms,我正在开发聊天应用程序。当用户单击联机用户链接时,弹出窗口(动态控件)将显示在更新面板中。但由于计时器事件,回发后动态创建的控件将从页面中删除 因此,在回发事件中,我将再次使用相同的id创建它们。但由于这一点,每当我试图在用户控件中键入文本框时。页面刷新后,文本框中的焦点将被转移。所以我不得不一次又一次地点击tetxbox来输入一些东西 计时器事件用于查找数据库中当前用户是否收到新消息 <%@ Control Language="C#" AutoEventWireup="true" Cod

我正在开发聊天应用程序。当用户单击联机用户链接时,弹出窗口(动态控件)将显示在更新面板中。但由于计时器事件,回发后动态创建的控件将从页面中删除

因此,在回发事件中,我将再次使用相同的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&nbsp;&nbsp;<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> 

这更倾向于设计问题。我建议您将键入文本框排除到其他用户控件中。此
聊天窗口
仅用于显示聊天信息。因此,当它刷新时,它不会干扰输入文本框


希望有帮助。

我尝试了你的解决方案,但没有解决我的问题,我认为问题是由于在pageload事件期间重新创建了动态控件。我必须进一步调查你的
Timer1\u Tick
事件,你能在这里发布你的编码吗?我怀疑您正在重新加载整个UserControl,而不是只更新聊天信息。
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);
        }
    }