set focus on textbox popup asp.net
How can i set focus on the first textbox control in a popup (modal)? It's a login window. I trie开发者_运维百科d javascript, but that failed.
Try AjaxControlToolkit.Utility.SetFocusOnLoad(YouTextBoxID);
Here is the sample
In MARKUP:
<asp:ModalPopupExtender runat="server" BehaviorID="UserSearchModalBehavior" ID="mpeSearch" PopupControlID="pUserSearch" TargetControlID="btnFindUser"
OkControlID="btnUserSearchClose" CancelControlID="ibClose" />
In JavaScript:
<script language="javascript" type="text/javascript">
function pageLoad() {
var modalPopup = $find("UserSearchModalBehavior");
if (modalPopup != null) {
modalPopup.add_shown(OnPopupShow);
}
}
function OnPopupShow() {
var tb = $get("tbSearchQuery");
tb.focus();
}
</script>
in the body onload
you can add some javascript
. and set focus to textbox you want
E.g:
<script type="text/javascript">
function setFocus() {
document.getElementById('TextBox2').focus();
}
</script>
<body onload="setFocus();">
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick='ani1();' />
</form>
</body>
The TextBox2
get focused once page is loaded
Just use focus()
in the onload
event in your ASP.NET page which declares your TextBox:
<body onload="document.getElementById('<%= yourTextBoxID.ClientID %>').focus();"></body>
You can use the <body>
tag with content placeholders or forms.
if you want to call the popup from js, use this:
function openPopup() {
$find('BehaviorID_of_your_popup').show();
document.getElementById('<%= theTextBox.ClientID %>').focus();
}
if you want to call it form server side, this should work:
protected void btnShow_Click(object sender, EventArgs e)
{
popup.Show();
theTextBox.Focus();
}
精彩评论