Link to home
Start Free TrialLog in
Avatar of abonjour
abonjour

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of igor_alpha
igor_alpha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abonjour
abonjour

ASKER

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