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
445 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

      Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now