0
我必須執行服務器端事件,當用戶點擊文本框的。到目前爲止,我還可以管理呼叫使用onfocus事件JavaScript函數,但如果我嘗試刪除聚焦狀態,做了一些代碼,放在一個死循環再次聚焦回到了控制然後重新觸發onfocus事件。下面的示例...保持注意力集中onfocus__doPostBack後在asp.net
的Html
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" onfocus="CallServer(this);"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" onfocus="CallServer(this);"></asp:TextBox>
的Javascript
function CallServer(obj) {
if (obj != "") {
var control = document.getElementById(obj.id)
__doPostBack(obj.id, "onfocus");
}
}
代碼隱藏
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
Dim target As String = Page.Request.Params.Get("__EVENTTARGET")
Dim eventarg As String = Page.Request.Params.Get("__EVENTARGUMENT")
Dim PostControl As Control = Nothing
If target <> "" Then
PostControl = Page.FindControl(target)
End If
If eventarg = "onfocus" Then
CType(PostControl, TextBox).Attributes("onfocus") = "null"
...do some code
Page.SetFocus(PostControl)
CType(ctrl, TextBox).Attributes.Add("onfocus", "CallServer(this)")
End If
End If
End Sub