简体   繁体   中英

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:

   <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:

   <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.

I got bitten by something similar in (someone else's!) JSP last week. The HTML comments don't stop the tag from being parsed; if it generates an HTML comment, you end up with nested HTML comments and all manner of strange things going on.

The only "safe" use of HTML-style comment delimiters in an ASP/JSP is where there is no generated content between them:

<!-- This is safe -->

We know exactly what that will be after the ASP engine has parsed it - it'll look the same. The problem may bite you when you have an ASP tag in there:

<!-- 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! Let's see what happens when that custom tag is parsed and the HTML gets sent to the browser:

<!-- 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. It's safest to assume that every tag generates an HTML comment, and that putting it in an HTML comment will bite you. Even if it's your tag, you might add an HTML comment to it later.

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. Only use HTML-style comments if you actually want a comment to appear in the HTML source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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