开发者

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 up开发者_开发百科on 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.


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜