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
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

710 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