SetFocusOnError

Discussion in 'ASP .Net Web Controls' started by Shooter, Jul 16, 2007.

  1. Shooter

    Shooter Guest

    Hi,

    I'm having problems getting validation controls to set the focus back to the
    textbox that caused the error (or any error). Using ASP.Net 2 and IE 6.

    Does anyone have a solution to help me out on this one? Heres some code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
    Inherits="WebForm1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" />

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    SetFocusOnError="true"
    ErrorMessage="TextBox1 is empty" ControlToValidate="TextBox1"
    runat="server"/><br />
    <asp:TextBox ID="TextBox2" runat="server" />

    <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
    SetFocusOnError="true"
    ErrorMessage="TextBox2 is empty" ControlToValidate="TextBox2"
    runat="server"/>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button"
    CausesValidation="true"/>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server"
    ShowMessageBox="True"
    ShowSummary="False" />
    </div>
    </form>
    </body>
    </html>
     
    Shooter, Jul 16, 2007
    #1
    1. Advertising

  2. Shooter

    Shooter Guest

    Hi timmy123,

    I tried this on my laptop and home and found it also worked so I'm putting
    it down to either differing versions of IE or maybe something that occurred
    with the conversion of this project from 1.1 to 2.0 via the wizard.

    It maybe the conversion as the page I copied the code from had a demo of it
    and it was working ok in the same IE ver here at work.

    "timmy123 via DotNetMonster.com" wrote:

    > Hi,
    >
    >
    > The Focus API allows you to declaratively or programmatically set focus to
    > specific controls on the form. You can use the Page.SetFocus method to pass
    > the ID of a control that should receive focus or you can call the Focus
    > method on the control directly. You can also set the DefaultFocus property of
    > the Form element to the ID of a control that should receive focus when the
    > page is first loaded. Similar to DefaultFocus, you can set the DefaultButton
    > property of the Form element to the ID of a Button control that should submit
    > when the Enter key is pressed inside of any input control on the form. If
    > your page should have different default buttons depending on the input
    > control that has current focus, you can wrap specific input controls in a
    > Panel control and set the DefaultButton property of the Panel instead. Any
    > input control in the Panel will use the Panel's DefaultButton property to
    > submit the form when the Enter key is pressed from within the input control.
    > A validation feature that depends on the Focus API is SetFocusOnError which
    > is set on validator controls to cause the first invalid control to receive
    > focus.
    >
    > Page.SetFocus(TextBox1);
    >
    >
    > I tried your code. It is working for me. Any questions, please post it again.
    >
    > Database programming using Visual Basic 2005 and Csharp2005
    > http://www.vkinfotek.com
    >
    >
    >
    >
    >
    > Shooter wrote:
    > >Hi,
    > >
    > >I'm having problems getting validation controls to set the focus back to the
    > >textbox that caused the error (or any error). Using ASP.Net 2 and IE 6.
    > >
    > >Does anyone have a solution to help me out on this one? Heres some code:
    > >
    > ><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
    > >Inherits="WebForm1" %>
    > >
    > ><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > >"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > >
    > ><html xmlns="http://www.w3.org/1999/xhtml" >
    > ><head runat="server">
    > > <title>Untitled Page</title>
    > ></head>
    > ><body>
    > > <form id="form1" runat="server">
    > > <div>
    > > <asp:TextBox ID="TextBox1" runat="server" />
    > >
    > ><asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    > > SetFocusOnError="true"
    > > ErrorMessage="TextBox1 is empty" ControlToValidate="TextBox1"
    > >runat="server"/><br />
    > ><asp:TextBox ID="TextBox2" runat="server" />
    > >
    > ><asp:RequiredFieldValidator ID="RequiredFieldValidator2"
    > > SetFocusOnError="true"
    > > ErrorMessage="TextBox2 is empty" ControlToValidate="TextBox2"
    > >runat="server"/>
    > > <br />
    > > <asp:Button ID="Button1" runat="server" Text="Button"
    > >CausesValidation="true"/>
    > > <asp:ValidationSummary ID="ValidationSummary1" runat="server"
    > >ShowMessageBox="True"
    > > ShowSummary="False" />
    > > </div>
    > > </form>
    > ></body>
    > ></html>

    >
    > --
    > Message posted via DotNetMonster.com
    > http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net-web-controls/200707/1
    >
    >
     
    Shooter, Jul 24, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lit

    SetFocusOnError Validation

    Lit, Aug 25, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    381
  2. Boudewijn

    setfocusonerror

    Boudewijn, Aug 17, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    116
    Boudewijn
    Aug 17, 2006
Loading...

Share This Page