M Kenyon II
M Kenyon II

Reputation: 4264

Quotes in my markup replaced with html entities?

I have a content page (using a master page) with a text box and a button. I want the button to call some javascript before doing anything else. Right now I'm just testing the javascript, so this is a rough draft.

The problem is that my markup is getting altered, so the client doesn't see all the quotes. Quotes are getting converted to HTML entity strings.

So the button in this code:

<asp:Content ID="Content1" ContentPlaceHolderID="cphMainContent" runat="server">
    <div>
        <asp:TextBox ID="txtInputTopics" 
            runat="server" TextMode="MultiLine" 
            Width="335px" Height="250px" 
            CssClass="mediumFontTextbox"/>
        <asp:Button ID="btnTestAdd" 
            runat="server" Text="Add"
            OnClientClick='CheckFirstAlphaNum(document.getElementById("<%= txtInputTopics.ClientID %>").val);'  />
    </div>
</asp:Content>

Get's changed to this:

<input type="submit" name="ctl00$ctl00$cphBody$cphMainContent$btnTestAdd"
   value="Add"
   onclick="CheckFirstAlphaNum(document.getElementById(&quot;&lt;%= txtInputTopics.ClientID %>&quot;).val);" 
   id="cphBody_cphMainContent_btnTestAdd" />

Why?

Upvotes: 2

Views: 316

Answers (2)

user757095
user757095

Reputation:

as stated by Guffa you need another method to get the input node, you can try to get the previous sibling of the button

//source: http://www.w3schools.com/dom/prop_element_previoussibling.asp
function get_previoussibling(n) {
  x=n.previousSibling;
  while (x.nodeType!=1) {
    x=x.previousSibling;
  }
  return x;
}

function CheckFirstAlphaNum(element) {
  var inputEl = get_previoussibling(element);
  var value = inputEl.value;
  // do your stuff
}

html

<asp:Content ID="Content1" ContentPlaceHolderID="cphMainContent" runat="server">
  <div>
    <asp:TextBox ID="txtInputTopics" 
      runat="server" TextMode="MultiLine" 
      Width="335px" Height="250px" 
      CssClass="mediumFontTextbox"/>
    <asp:Button ID="btnTestAdd" 
      runat="server" Text="Add"
      OnClientClick="CheckFirstAlphaNum(this);" />
  </div>
</asp:Content>

Upvotes: 0

Guffa
Guffa

Reputation: 700362

That's because they should be HTML encoded. To include a quotation mark inside an attribute value that is delimited by quotation marks, it has to be HTML encoded. The client will see the quotation marks just fine.

Your problem is that you are trying to use a server code tag (<%= txtInputTopics.ClientID %>) inside an attribute in a control that has runat="server". The code ends up literally in the HTML attribute instead of putting the client id value in the attribute.

You need another way of putting the value in the attribute, for example using a data binding expression. See for example: https://stackoverflow.com/a/1393407/69083

Upvotes: 1

Related Questions