Ed.
Ed.

Reputation: 1694

Problem setting focus to validationsummary control after validation

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.

Upvotes: 2

Views: 5501

Answers (4)

RobS
RobS

Reputation: 9422

There are a few tips added on this Connect issue report: https://connect.microsoft.com/VisualStudio/feedback/details/342104/maintainscrollpositiononpostback-and-validationsummary (sorry for the cut and paste URI, for some reason I can't add the link properly).

It's all a bit of a hack, but it could help you out.

Upvotes: 0

Might
Might

Reputation: 315

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.

Upvotes: 2

Dan Appleyard
Dan Appleyard

Reputation: 7445

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.

Upvotes: 0

rick schott
rick schott

Reputation: 21117

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

Upvotes: 1

Related Questions