Solved

I can't focus a textbox control neither submit with enter key within content pages in Asp.net 2.0

Posted on 2007-04-05
4
474 Views
Last Modified: 2013-11-07
I've a master page - content page web site and my problem is that I can't focus a control within the content pages in asp.net 2.0
Same thing happend when I want to enable the enter key to submit information in a content page.
I don't have the atribute in the content page to asign the default button.

How can I focus a control within a content page?
How can I submit a content page with the enter key?
0
Comment
Question by:abonjour
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 500 total points
ID: 18861546
HI, abonjour!

1. In .net 2.0 you can use new SetFocus method to set focus on controls:

<%@ page language="VB" %>

<script runat="server">
    Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        SetFocus(focusHere)
    End Sub
</script>

<html>
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="default" runat="server" /><br />
        <asp:textbox id="focusHere" runat="server" />
    </form>
</body>
</html>

2. Simply javascript, after you press "enter" on form, it set up default button and submit form. ("c" in this case)

<body onkeypress="OnKey();">
<form runat="server">
<asp:textbox id="x" runat="server"></asp:textbox>
<asp:textbox id="y" runat="server"></asp:textbox>
<asp:textbox id="z" runat="server"></asp:textbox>

<asp:button id="a" text="a" runat="server"></asp:button>
<asp:button id="b" text="b" runat="server"></asp:button>
<asp:button id="c" text="c" runat="server"></asp:button>
<asp:button id="d" text="d" runat="server"></asp:button>
</form>

<script language = "javascript">
function OnKey()
{
   if (eval(window.event.keyCode) == 13)
   {
      document.all['c'].focus();
      ............
      ............
      document.forms(0).submit();    
   }
}
</script>
0
 

Author Comment

by:abonjour
ID: 18875450
This solution apply to a common aspx. But whithin a contentpage I can't have a form tag because .net report an error. A page can have only one server-side Form tag!!
I think I have to acces the form of the masterpage but I don't know how.
This is the code in the content page and I want to set focus in this textbox.

<asp:content id="Content2" contentplaceholderid="PageContent" runat="server">
    <asp:TextBox ID="txtCosto" runat="server"  ></asp:TextBox>
</asp:content>
0
 
LVL 15

Expert Comment

by:igor_alpha
ID: 18878966
I understood your problem and suggest you to use SetFocus() method instead of using javascript. In this case you don't have to manually coding javascript and place it somethere in content page. It's a best way to set focus on control. You can use SetFocus method on codebehind of content page or on content aspx and focus would be set on control inside a content page.

Second scenario can be changed using div tag placed inside content page.
To submit form after clicking "Enter" button you just need to correctly specify buttons names in javascript "OnKey" function. They names would like ctl00_ContentPlaceHolder1_TextBox1, ctl00_ContentPlaceHolder1_TextBox2 etc...

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div onkeypress="OnKey();">
<asp:textbox id="x" runat="server"></asp:textbox>
<asp:textbox id="y" runat="server"></asp:textbox>
<asp:textbox id="z" runat="server"></asp:textbox>

<asp:button id="a" text="a" runat="server"></asp:button>
<asp:button id="b" text="b" runat="server"></asp:button>
<asp:button id="c" text="c" runat="server"></asp:button>
<asp:button id="d" text="d" runat="server"></asp:button>

<script language = "javascript">
function OnKey()
{
   if (eval(window.event.keyCode) == 13)
   {
      document.all['c'].focus();
      ............
      ............
      document.forms(0).submit();    
   }
}
</script>

</div>
</asp:Content>
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question