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
467 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

778 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