首先,在你的Page_Load事件註冊此javascript:在
Public Shared Sub DefaultButton(ByVal objTextControl As Web.UI.WebControls.WebControl, ByVal objDefaultButtonID As String)
objTextControl.Attributes.Add("onkeydown", String.Format("fnTrapKD(document.all.{0})", objDefaultButtonID))
End Sub
然後:
Public Shared Sub jsTrapEnter(ByRef p_Page As System.Web.UI.Page)
Dim sScript As New System.Text.StringBuilder
sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
sScript.Append("function fnTrapKD(btn){" & vbCrLf)
sScript.Append(" if (document.all){" & vbCrLf)
sScript.Append(" if (event.keyCode == 13)" & vbCrLf)
sScript.Append(" { " & vbCrLf)
sScript.Append(" event.returnValue=false;" & vbCrLf)
sScript.Append(" event.cancel = true;" & vbCrLf)
sScript.Append(" btn.click();" & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append("}" & vbCrLf)
sScript.Append("</SCRIPT>" & vbCrLf)
If Not p_Page.ClientScript.IsClientScriptBlockRegistered("ForceDefaultToScript") Then
p_Page.ClientScript.RegisterClientScriptBlock(p_Page.GetType, "ForceDefaultToScript", sScript.ToString)
End If
End Sub
,則此方法補充說,你想有一個默認的按鈕,每個編輯控件的屬性我們的page_load事件註冊每個編輯器的默認按鈕:
DefaultButton(txtStreetNm, btnSearch.ClientID)
DefaultButton(txtSuffix, btnSearch.ClientID)
好吧,我再次嘗試面板解決方案,我工作。謝謝! – nhat 2012-03-05 19:45:18
沒問題,我很高興幫助:)。 – 2012-03-05 19:50:25