简体   繁体   English

奇怪-由于以下原因,单击更新按钮不会导致回发

[英]Strange - Clicking Update button doesn’t cause a postback due to <!— tag

If I define the following template inside DetailsView , then upon clicking an Update or Insert button, the page is posted back to the server: 如果我在DetailsView中定义以下模板,则在单击“ 更新”或“ 插入”按钮时,该页面将被发布回服务器:

   <EditItemTemplate>
       <asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
    <asp:CompareValidator ID="valDateType" runat="server" ControlToValidate="txtDate" Type="Date" Operator="DataTypeCheck" Display="Dynamic" >*</asp:CompareValidator> 
</EditItemTemplate>

If I remove CompareValidator control from the above code by simply deleting it, then page still gets posted back.But if instead I remove CompareValidator control by enclosing it within <!-- --> tags, then for some reason clicking an Update or Insert button doesn't cause a postback...instead nothing happens: 如果我通过简单地删除上面的代码从上面的代码中删除CompareValidator控件,则页面仍会被发布回去;但是如果我通过将它括在<!-- -->标记中来删除CompareValidator控件,则出于某种原因单击更新插入按钮不会引起回发...而是什么也不会发生:

   <EditItemTemplate>
       <asp:TextBox ID="txtDate" runat="server"   Text='<%# Bind("Date") %>'></asp:TextBox>
   <!-- <asp:CompareValidator ID="valDateType" runat="server" ControlToValidate="txtDate" Type="Date" Operator="DataTypeCheck" Display="Dynamic" >*</asp:CompareValidator> -->
</EditItemTemplate>
</EditItemTemplate>

Any idea why page doesn't get posted back? 知道为什么页面不回发吗?

thanx 感谢名单

Try using server-side comments: 尝试使用服务器端注释:

<%--<asp:CompareValidator ID="valDateType" runat="server" ControlToValidate="txtDate" Type="Date" Operator="DataTypeCheck" Display="Dynamic" >*</asp:CompareValidator>--%>

You can also use the Enabled="False" attribute. 您也可以使用Enabled="False"属性。

I got bitten by something similar in (someone else's!) JSP last week. 上周,我在(别人的!)JSP中被类似的东西咬了。 The HTML comments don't stop the tag from being parsed; HTML注释不会阻止标记的解析。 if it generates an HTML comment, you end up with nested HTML comments and all manner of strange things going on. 如果它生成HTML注释,则最终会嵌套HTML注释以及各种奇怪的事情。

The only "safe" use of HTML-style comment delimiters in an ASP/JSP is where there is no generated content between them: 在ASP / JSP中,HTML样式注释定界符的唯一“安全”用法是它们之间没有生成的内容:

<!-- This is safe -->

We know exactly what that will be after the ASP engine has parsed it - it'll look the same. 我们确切地知道ASP引擎对其进行解析之后将是什么-它将看起来相同。 The problem may bite you when you have an ASP tag in there: 当您在那里有一个ASP标记时,该问题可能会困扰您:

<!-- This might not be safe because I have no idea 
     what <asp:joesCustomTag/> expands to -->

If that tag generates an HTML comment, you'll have an HTML comment in your HTML comment! 如果该标记生成HTML注释,则HTML注释中将包含HTML注释! Let's see what happens when that custom tag is parsed and the HTML gets sent to the browser: 让我们看看解析该自定义标签并将HTML发送到浏览器时会发生什么:

<!-- This might not be safe because I have no idea 
     what 

     <!-- Joe's custom tag -->
     <p>Joe is 1337</p>

     expands to -->

You can see the problem right there - SO's own parser is confused by the nested comments. 您可以在那里看到问题-嵌套注释使SO自己的解析器感到困惑。 It's safest to assume that every tag generates an HTML comment, and that putting it in an HTML comment will bite you. 最安全的假设是每个标签都会生成一个HTML注释,并且将其放入HTML注释会伤到你。 Even if it's your tag, you might add an HTML comment to it later. 即使是您的标签,也可以稍后添加HTML注释。

The answer, as Paperjam says, is to use the correct server-side comments to comment things out - though, of course, you really shouldn't be leaving commented-out dead things around waiting to bite you. 正如Paperjam所说,答案是使用正确的服务器端注释将事情注释掉-但是,当然,您实际上不应该将被注释掉的坏东西浪费在等待您的咬伤。 Only use HTML-style comments if you actually want a comment to appear in the HTML source. 仅当您实际上希望注释显示在HTML源代码中时,才使用HTML样式的注释。

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

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