2013-10-07 75 views
1

我有我的屏幕上2個UpdatePanel的。一個有一個文本框,另一個是gridview。我有一個計時器(在UpdatePanel之外),每5秒刷新一次gridview。已經在它的文本框的UpdatePanel中有一個按鈕(在UpdatePanel以外),按下時在文本框中輸入什麼被添加到數據庫,並在文本框變得清晰了,所有通過的UpdatePanel(AJAX,沒有頁面加載)。ASP.NET的UpdatePanel定時更新失去焦點文本框

我的問題是,當鏈接到計時器並刷新gridview的確實是它偷焦點從文本框離開的事情在UpdatePanel。我可以通過在System.Web.UI.ScriptManager.GetCurrent(this).SetFocus(this.txtNewComment);「但是將光標置於文本框的開頭。如果我在輸入內容的過程中弄亂了我輸入的內容,因爲光標位於開頭而不是結尾。

如何保持光標它的確切位置是在文本框中當定時器觸發UpdatePanel的任何想法?

通常情況下,您將使用文本框的Select()函數,如此處所述(http://msdn.microsoft.com/en-us/library/ms752349.aspx),但Select鍵()似乎不存在於MS按鈕的ASP.NET中。

+0

代碼與GridView中UpdatePanel內的計時器?還是在兩個UpdatePanel之外?如果它在兩者之外,請嘗試使用GridView將其放入UpdatePanel中。 – Bryan

+0

如果我將Timer放置在GridView所在的UpdatePanel中,則不會有任何變化。 – user441521

回答

2

嘗試設置到兩個UpdatePanel的屬性的UpdateMode =「條件」。

在下面的例子中,文本框不會失去焦點。

<asp:UpdatePanel ID="TextBoxUpdatePanel" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <asp:TextBox ID="PanelTextBox" runat="server"></asp:TextBox> 
    </ContentTemplate> 
</asp:UpdatePanel> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <asp:Literal ID="TimeLiteral" runat="server"></asp:Literal> 
     <asp:Timer ID="myTimer" runat="server" OnTick="myTimer_Tick"> 
     </asp:Timer> 
    </ContentTemplate> 
</asp:UpdatePanel> 

背後

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!this.IsPostBack) { 
     PanelTextBox.Focus(); 
     myTimer.Interval = 3000; 
     myTimer.Enabled = true; 
    } 
} 

protected void myTimer_Tick(object sender, EventArgs e) 
{ 
    System.Threading.Thread.Sleep(2000); 
    TimeLiteral.Text = DateTime.UtcNow.Ticks.ToString(); 
} 
+0

所以這是有效的,但爲什麼將UpdateMode設置爲Conditional(這就是我所做的,我沒有更改我的代碼隱藏到你有什麼)。在這種情況下查看焦點和光標位置?我不明白。 – user441521

+0

當計時器滴答時,默認情況下頁面中的所有UpdatePanels都會刷新。將UpdateMode設置爲有條件可更改此行爲。 –

+0

哦,等等,默認值必須是「Always」,看起來像所有的UpdatePanels在任何一個UpdatePanels中都會被更新。這是導致我的文本框,在不同的更新面板得到更新,因此失去了重點。這似乎很奇怪,他們甚至會有這個「功能」。必須與嵌套的UpdatePanel更相關 – user441521

相關問題