我有一個很長的表格供我的用戶填寫。有沒有辦法將ValidationSummary中的錯誤消息超鏈接到特定的文本字段?添加超鏈接到ValidationSummary
1
A
回答
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.我知道這個問題很老,但我今天看到有人提出了一個更優雅的解決方案,所以對於我鞋子裏的其他人來說,這裏就是了。
相關問題
- 1. 添加超鏈接到PHP表
- 2. 將類添加到超鏈接
- 3. 將超鏈接添加到RadioButtonList
- 4. 將超鏈接添加到數據表
- 5. 添加一個超鏈接到由JSON
- 6. 添加一個超鏈接到noty alert
- 7. 動態添加超鏈接到RichTextBox
- 8. 添加超鏈接到WPF WPF
- 9. 添加自動超鏈接到圖像
- 10. ffmpeg:添加超鏈接到視頻?
- 11. Twitter API,添加超鏈接
- 12. 添加類的ValidationSummary
- 13. 添加超鏈接onClick的超鏈接在extJs
- 14. 存儲過程添加超鏈接
- 15. 在郵件中添加超鏈接
- 16. 優化添加超鏈接語法
- 17. XML添加<a>超鏈接
- 18. 添加超鏈接的NSString的iOS
- 19. 單擊超鏈接時添加模態
- 20. 如何在gridview中添加超鏈接
- 21. 如何在JLabel中添加超鏈接
- 22. 在sql server中添加超鏈接
- 23. 在php中添加超文本鏈接
- 24. 解析ASIN並添加超鏈接
- 25. 使用GMaps爲Infowindow添加超鏈接
- 26. 在AlertDialog中添加超鏈接(郵件)
- 27. 如何添加超鏈接ModelState.AddModelError
- 28. 作爲TableItem添加超鏈接SWT
- 29. 動態添加鏈接到鏈接
- 30. 將鏈接添加到鏈接