views:

41

answers:

5

Hi,

How can i set focus on the first textbox control in a popup (modal)? It's a login window. I tried javascript, but that failed.

A: 

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

anishmarokey
where exactly would i be putting this code? the page in which the user is atm or the usercontrol?
Yustme
+1  A: 

Try AjaxControlToolkit.Utility.SetFocusOnLoad(YouTextBoxID);

banupriya
Where? page_load? Didn't work. its a modelpopupextender.
Yustme
Try in modalpopup extender's load event
banupriya
Modal pop up itself has a load event. protected void YourModalPopUpID_ModalPopupExtender_Load(object sender, EventArgs e) { AjaxControlToolkit.Utility.SetFocusOnLoad(YouTextBoxID); }
banupriya
The event gets fired, but the textbox control is not getting the focus. this is the modelpopup code: <act:ModalPopupExtender ID="popup" runat="server" TargetControlID="btnLogIn" PopupControlID="pnlLoginOverlay" BehaviorID="MPE" CancelControlID="btnCancel" />
Yustme
A: 
<body onload="form1.txtInput1.focus( )" >
</body>
Pranay Rana
+1  A: 

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.

Brissles
A: 

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();
}
y34h