簡體   English   中英

檢查asp:TextBox是否在Asp.net4(C#)中具有焦點

[英]check if asp:TextBox has focus in Asp.net4 (C#)

我正在構建一個類似於Facebook的小聊天模塊。 這是我的default.aspx文件中的一個小字段集:

<form>
    <`asp:TextBox` ID="InstantMessageContent" runat="server" onkeypress="return EnterEvent(event)" >
    </asp:TextBox>
</form>

在文件后面的C#代碼中,我想檢查文本框是否具有焦點以讀取過去的用戶消息。 我嘗試了類似的方法,但是VS2012在此“ .Focused ”下划線強調為未知:

if (InstantMessageContent.Focused==true)
  {
      changeUnreadStatusMessageToRead(Person1, Person2);
  }

有人可以幫我弄這個嗎。 我在Google上搜索了很多,但發現的所有答案都不適用於我。

您無法在服務器端獲取屬性以標識文本框是否處於焦點狀態。 而是使用客戶端腳本。 在單擊事件或焦點事件時,編寫觸發服務器端方法的Java腳本函數。 為此,您必須將腳本管理器添加到頁面。 還可以使用網絡方法修飾您的方法。

ASPX:-

<form id="form1" runat="server" name="form1">
     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>
         <div>
           <asp:TextBox ID="txtTest" runat="server"  onclick="javascript:PageMethods.ChangeUnreadStatusMessageToRead()"></asp:TextBox>
          </div>
  </form>

CS:-

[System.Web.Services.WebMethod]
public static void ChangeUnreadStatusMessageToRead()
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM