• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Force the cursor to remain in a textbox

I have a page which has a textbox and button.  When the page is loaded the cursor is in the textbox.  This was done with this line of code
   txtTrackInfo.Focus();
placed inside the Page_Load event.  
This is the only textbox on the screen.  If the user clicks on the screen the cursor is no longer in the textbox.  What I am hoping to do is force the cursor to stay in the textbox even if the user clicks on the page.

Is there any way I can force the cursor to stay in the textbox?
0
tj_ntwnt
Asked:
tj_ntwnt
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
No.
0
 
David Johnson, CD, MVPOwnerCommented:
If there was some mystical way of forcing the cursor to remain in the text box then there would be absolutely no way of clicking the submit button. As DaveBaldwin stated the answer is :NO
0
 
deepu chandranCommented:
HI,
It is Yes if you use JQuery

Use JQuery to handle the form Click event.

http://www.w3schools.com/jquery/default.asp 
http://api.jquery.com/click/

Deepu
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tj_ntwntAuthor Commented:
Thanks deepudeepam,
I have looked at those to links and cannot find a way to do it.  This is the page that I am working on:
<asp:Content ID="MainContent" ContentPlaceHolderID="Main" runat="server">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<asp:Panel ID="panMain" runat="server" DefaultButton="btnLogin" Width="100%" >
<table>
<tr>
<td colspan="100">
</td>
<td colspan="100">
        <asp:Label ID="lblLoginInstructions" runat="server" Text="Please swipe your card" ></asp:Label>
</td>
</tr>
<tr>
<td colspan="100" align="right">
    <asp:label ID="lblTrackInfo" runat="server" Text="" ></asp:label>
</td>
<td colspan="100">
        <asp:TextBox ID="txtTrackInfo" runat="server" Width="1px" BorderStyle="None"
            BorderWidth="0px" ></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="100">
</td>
<td colspan="100">
        <asp:button ID="btnLogin" runat="server" onclick="btnLogin_Click"
            Visible="true" BackColor="White" BorderStyle="None"></asp:button>
</td>
</tr>
</table>
</asp:Panel>
</asp:Content>

What I am trying to do is make txtTrackInfo be always selected.  I tried adding <div id="ClickRegion" >arround the panel and putting this script above it:
<script type="text/jscript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/jscript" >
    $("#ClickRegion").click(function () {
        $("txtTrackInfo").focus();
    });
</script>
It didn't work.  It seems that the problem that I am having is how to reference txtTrackInfo inside jscript.

Can you help me with that?

Thanks.
0
 
johny_bravo1Commented:
Hi,

It sounds interesting. Tried a few things for you.

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {

            $("#<%= txtTrackInfo.ClientID %>").focus();

            $("#<%= txtTrackInfo.ClientID %>").blur(function () {
                $("#<%= txtTrackInfo.ClientID %>").focus();
            })

            $(".myDiv").click(function () {
                $(".myDiv").css("border", "3px solid red");
                $("#<%= txtTrackInfo.ClientID %>").focus();
            })
        })

   
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <div class="myDiv">
        <asp:Panel ID="panMain" runat="server" DefaultButton="btnLogin" Width="100%">
            <table>
                <tr>
                    <td colspan="100">
                    </td>
                    <td colspan="100">
                        <asp:Label ID="lblLoginInstructions" runat="server" Text="Please swipe your card"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="100" align="right">
                        <asp:Label ID="lblTrackInfo" runat="server" Text=""></asp:Label>
                    </td>
                    <td colspan="100">
                        <asp:TextBox ID="txtTrackInfo" runat="server" Width="100px" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="100">
                    </td>
                    <td colspan="100">
                        <asp:Button ID="btnLogin" runat="server" Visible="true" Text="Login"></asp:Button>
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </div>
</asp:Content>


Output :
Places cursor in textbox when page load
Click tab button from textbox will still keep the cursor in textbox
Clicking on the div, still curosr on same textbox
But multiple clicks take it away

Hope it might give you some direction.
0
 
tj_ntwntAuthor Commented:
Thanks very much johny_bravo1.  It works great!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now