开发者

Setting Focus on a TextBox

开发者 https://www.devze.com 2023-03-24 14:50 出处:网络
I have a small ASP.net web app for sales, when the \"Make New Sale\" page loads, I want the TextBox for the Barcode to be in focus, so that the user doesn\'t have to navigate to it and click it then e

I have a small ASP.net web app for sales, when the "Make New Sale" page loads, I want the TextBox for the Barcode to be in focus, so that the user doesn't have to navigate to it and click it then enter the Barcode, how c开发者_C百科an I do that?

PS: I am using Visual Web Developer 2010 and VB.net.


You can do this either in your code behind, or directly in JavaScript on your page. To do it in your code behind, just call the Focus() method on the control:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    TextBox1.Focus()
End Sub

To do it in JavaScript, create a function that selects the textbox and calls its .focus() method, and call the function in the page's onLoad event:

function SetFocus() {
    var textbox = document.getElementById("<%= TextBox1.ClientID %>");
    if(textbox != null) {
        textbox.focus();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消