Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.
Loading

1st Prize - Apple iPad


DOTNET Quiz 2011 - Why double quotes does not work in inline databinding statement?

  • Suppose you have this GridView declaration below:

    <asp:GridView runat="server"> 
         <Columns> 
              <asp:TemplateField> 
                   <ItemTemplate> 
                        <asp:HiddenField runat="server" Value="<%#Eval("ColumnName")%>" /> 
                   </ItemTemplate> 
              <asp:TemplateField> 
         </Columns> 
    </asp:GridView>
    

    Using the binding statement above with double quotes (" ") gives The server tag is not well formed error. Whereas if you use single quotes like this: Value = '<%#Eval("ColumnName")%>' works.

    Why do you think double quotes does not work in inline databinding statement? Explain.

    Posted on 01-24-2011 00:00 |
    Vinz
    1010 · 0% · 25

6  Answers  

Subscribe to Notifications
  • Score
    7
    Value="<%#Eval("ColumnName")%>"

    The opening quotes closes after (". So compiler can parse till Value="<%#Eval(" and would expect it to be terminated there. The rest of the statement i.e. ColumnName")%>" is meaningless and the compiler can't parse it, obviously resulting in an error. Finally Value="<%#Eval(" can't make it data-bind.

    Replied on Jan 24 2011 3:05AM  . 
    Pradeep Kumar
    299 · 0% · 145
  • Score
    10

    The syntax highlighter in the question's code shows the problem perfectly: if you use double quotes inside an XML attribute (and that is excactly what the Value= inside the asp.HiddenField is) after starting the attribute value with a double quote, this effectively ends the string literal. XML allows you to use either single quotes (') or double quotes (") to delimit an attribute's value, but the quotes have to match. XML always takes the first matching quote to end the corresponding starting quote. So if you want to use double quotes inside the attribute value (because C# needs them), you have to use single quotes to delimit it. This code works:

    <asp:HiddenField runat="server" Value='<% #Eval("ColumnName") %>' />
    

    for the content of the <% … %> part is interpreted in .net, the quotes inside the attribute value have to be double quotes. For a programming language that accepts strings delimited with single quotes, the following would work, too:

    <asp:HiddenField runat="server" Value="<% #Eval('ColumnName') %>" />
    
    Replied on Jan 24 2011 6:21AM  . 
    Guenter
    28 · 6% · 1838
  • Score
    9

    I believe it's because when the compiler parses the expression, it actually sees two strings: <%#Eval( and )%>, and columnName is between those elements. That's why it says that the tag is not well formed. But if you use single quotes , everything is ok. Now, let's try to change the code and use something like Value="<%#Eval('ColumnName')%>". This time it doesn't work, we get an exception: Too many characters in character literal. We got an exception becuse single quotes are used to denote a char in .net. ColumnName is more than one char so we get an exception. In my opinion, the solution that starts with a single quote works, because inside those single quotes we do not have a normal string, but the expression that is evaluated on the server.

    Replied on Jan 24 2011 9:59AM  . 
    Maciej Pakulski
    340 · 0% · 120
  • Score
    0

    test editor

    Replied on Feb 23 2011 4:47AM  . 
    Hima
    31 · 6% · 1776
  • Score
    5

    XMl attribute always checks for open double quote to end so compiler cannot understand Value="<%#Eval(" for parsing. Since it has no meaning it will simply terminate and gives error as "Server tag not well formed ". XML always takes the first matching quote to end the corresponding starting quote. If we want to use double quotes inside the attribute value, we have to use single quotes to delimit it

     Value='<% #Eval("ColumnName") %>' />
    
     Value="<% #Eval('ColumnName') %>" />
    

    Either of above 2 are correct.

    Replied on Feb 23 2011 4:50AM  . 
    Vamshi
    132 · 1% · 376
  • Score
    8

    The error is shown because when parsing Value="<%#Eval("ColumnName")%>" HTML parser sees two strings. The first one is "<%#Eval(" and the second one is ")%>". So, the parser will assume that "<%#Eval(" is a string for Value and would not be able to parse the rest showing "The server tag is not well formed" error. However if you use Value = '<%#Eval("ColumnName")%>' HTML parser will consider '<%#Eval("ColumnName")%>' as one string and then it will be able to see "ColumnName" inside of it.

    Please note that another solution Value="<% #Eval('ColumnName') %>" (as Vamshi suggested) will not work neither in C# ("Too many characters in character literal") nor in VB ("Expression expected").

    Replied on Mar 24 2011 12:58PM  . 
    Dmitry Kharlap (aka Docker)
    148 · 1% · 325

Your Answer


Sign Up or Login to post an answer.
Please note that your answer will not be considered as part of the contest because the competition is over. You can still post an answer to share your knowledge with the community.

Copyright © Rivera Informatic Private Ltd.