views:

437

answers:

4

Hi,

I have a validationsummary control which displays the summary of a few validationrequired controls and all of them belong to a validationgroup.

My submit button also has the same validationgroup so it validates everything upon it being clicked.

The problem i am having is setting the focus to the validationsummary control after validation occurs when my submit button is clicked. The focus goes to the top of my webform.

I need the focus to be put at the validationsummary control. How do i achieve this?

FYI:SetFocusOnError="true" did not work.

Thanks for reading.

A: 

Have you try.. setting this.

SetFocusOnError="true"
Muhammad Akhtar
Yes indeed.It did not work.
Ed
+1  A: 

Markup:

<asp:Button ID="Button1" runat="server" CausesValidation="false" 
  Text="Button" OnClientClick="SummaryFocus();" />

Script:

function SummaryFocus() {
    Page_ClientValidate();
    var i;
    for (i = 0; i < Page_ValidationSummaries.length; i++) 
    {
        if (!Page_ValidationSummaries[i].isvalid) 
        {                
            window.scrollTo(0, document.getElementById(Page_ValidationSummaries[i].id).offsetTop);
            break;
        }
    }
}
rick schott
thanks for ur input...i tried this script,but still focus gets set on top of the page.
Ed
do you have customvalidator doing a postback?
rick schott
no.here is my asp.net code:<asp:ValidationSummary class="error-details" ID="vsInsertComp" runat="server" HeaderText="There are errors on the page. Please make the requested corrections before continuing." DisplayMode="BulletList" ValidationGroup="ProfileInsertComp" Width="80%"></asp:ValidationSummary>
Ed
may i know what difference a custom validator would make in this context?
Ed
A full postback, which takes you to the top of the screen if not using Ajax.
rick schott
ok thanks..what do i need to do now to get this working?
Ed
I tested this code with a few validators that had display=none. It worked with the ValidationSummary at the top or bottom of the screen.
rick schott
A: 

You can't. If you actually look at the validation summary markup in a browser it is just the error messages for each validator that has gone off in its validation group. You can't put focus on it.

Dan Appleyard
A: 

Try this http://forums.asp.net/t/967952.aspx. I haven't verified it. But the last reply said it's working.

Also, you could try MaintainScrollPositionOnPostback="true" so that at least the focus is the same as before it's posted back.

Might