2011-07-21 207 views

回答

1

做到這一點,最簡單的方法是用簡單的HTML錨標記<a>,您可以在您的驗證控件的ErrorMessage屬性,它會顯示在你的ValidationSummary控件的HTML。對於例子

<asp:ValidationSummary ID="ValidationSummary1" runat="server" /> 
<asp:Button ID="Button5" runat="server" Text="Submit" /> 
<br /> 
<div style="height:800px"></div> 
<a name="TextBox1"></a> 
Required Field 
<asp:TextBox ID="TextBox1" runat="server" /> 
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ErrorMessage="Required Field is Required <a href='#TextBox1'>Click Here To Go To</a>" 
    Text="***" 
    ControlToValidate="TextBox1" /> 

一個更優雅的方式將上述方法與使用scrollTo功能或許凸顯場jQuery的結合。您可以在錨標記的onclick屬性中包含此jQuery/Javascript代碼。

1

我之前用@jdmonty建議的方式實現了這一點 - 通過將錨標籤添加到每個RFV的ErrorMessage屬性。最終我發現這太乏味了,所以我鼓掌了一些jQuery來爲我完成這項工作。這段代碼會將您的驗證消息與href=#targetControl的定位標記包裝在一起,當然點擊滾動到目標輸入。

將此添加到$(document).ready();部分腳本代碼。

var validators = Page_Validators; // returns collection of validators on page 

     $(validators).each(function() { 
      //get target control and current error validation message from each validator 
      var errorMsg = $(this).context.errormessage; 
      var targetControl = $(this).context.controltovalidate; 
      var errorMsgWithLink = "<a href='#" + targetControl + "'> " + errorMsg + "</a>"; 

      //update error message with anchor tag 
      $(this).context.errormessage = errorMsgWithLink; 
     }); 

您可以添加一些額外的jQuery作爲@ jdmonty建議平滑滾動。你也可以在你的樣式表中使用css僞類':focus'爲'active'輸入文本框添加樣式,像input[type=text]:focus{background-color:red;}這樣的類型在聚焦時真的會突出顯示。

P.S.我知道這個問題很老,但我今天看到有人提出了一個更優雅的解決方案,所以對於我鞋子裏的其他人來說,這裏就是了。