简体   繁体   English

如何在ASP.NET Forms中将Bootstrap的关闭按钮添加到ValidationSummary?

[英]How do you add Bootstrap's close button to ValidationSummary in ASP.NET Forms?

I'm trying to get asp:ValidationSummary to use Bootstrap's alert styles and to include a close button. 我正在尝试使用asp:ValidationSummary来使用Bootstrap的警报样式并包含一个关闭按钮。 The styling works, but the close button isn't added. 样式工作,但不添加关闭按钮。

This is how my .aspx looks like: 这就是我的.aspx的样子:

<asp:ValidationSummary class="alert alert-danger alert-dismissable"
   ID="ValidationSummary1" ShowSummary="true" runat="server"
   DisplayMode="BulletList"/>

I've tried to get jQuery to add the button, in $(document).ready , like this: 我试图让jQuery添加按钮,在$(document).ready中 ,像这样:

$('.alert-dismissable').each(function () 
{
   $(this).prepend('<button type="button" class="close"
      data-dismiss="alert">&times;</button>');
});

This works just fine if I use a regular div element, but not with asp:ValidationSummary. 如果我使用常规div元素,这可以正常工作,但不能使用asp:ValidationSummary。 If I examine the markup on the client, the button isn't rendered there. 如果我检查客户端上的标记,则不会在那里呈现按钮。

I've also tried subclassing asp:ValidationSummary to insert the markup in Render (HtmlTextWriter writer) . 我还尝试了子类化asp:ValidationSummary来在Render(HtmlTextWriter writer)中插入标记。 But the result is the same as with the previous attempts. 但结果与之前的尝试相同。

I've also tried to enclose the ValidationSummary inside a div, like this: 我还尝试将ValidationSummary包含在div中,如下所示:

<div class="alert alert-danger alert-dismissable">
   <button type="button" class="close" data-dismiss="alert">&times;</button>
   <asp:ValidationSummary
      ID="ValidationSummary1" ShowSummary="true" runat="server"
      DisplayMode="BulletList"/>
</div>

Which seems to render fine in the browser, but closing with the button prevents the ValidationSummary from showing up again as its parent div is now invisible. 这似乎在浏览器中渲染得很好,但是使用按钮关闭会阻止ValidationSummary再次显示,因为它的父div现在是不可见的。

I think you are on the right track with this 我认为你正走在正确的轨道上

<div id="validationSummary" class="alert alert-danger alert-dismissable">
   <button type="button" class="close" data-dismiss="alert">&times;</button>
   <asp:ValidationSummary
      ID="ValidationSummary1" ShowSummary="true" runat="server"
      DisplayMode="BulletList"/>
</div>

Additionally you need to handle the button click that causes validation to reset the visibility and then also clear the html from the validation summary. 此外,您需要处理导致验证重置可见性的按钮单击,然后清除验证摘要中的html。

$( "#myform" ).submit(function( event ) {
  //clear the validation summary html
  $("#ValidationSummary1").empty(); //this should be the ClientID not the serverID of your validation control.
  //display the validation summary div
  $("#validationSummary").toggle();
  //you might want to remove the 'hidden' class instead of toggling the divs visibility
});

i think this is the easiest way: 我认为这是最简单的方法:

      var container = $('form').find('[data-valmsg-summary="true"]');
                container.addClass('whatever').removeclass('if-you-want');
                container.find('ul').find('li').append('<a class="close" onclick="clearConfirmation()">×</a>');

  function clearConfirmation() {
            var container = $('form').find('[data-valmsg-summary="true"]');
            var html = container.html();
            container.find('ul').html(null);
            container.removeClass('alert').removeClass('alert-error');
        }

this is a pseudo-code just to give you an idea. 这是一个伪代码,只是为了给你一个想法。 Change clearConfirmation to your own needs and it will be good :) 根据自己的需要改变clearConfirmation,这将是很好的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM