1

All I want to do is write this string "runat='server'"; in javascript. and use that here:

var dropdown = "<td><asp:DropDownList ID='drpid' runat='server' DataSourceID='SqlDataSource1' DataValueField='Id' DataTextField='Text'></asp:DropDownList></td>"

error is this: SyntaxError: missing ) after argument list

6
  • 3
    So you want to use client side code to create a server side control? The server won't know about it because by the time the JS runs the server has already sent the page to the browser.
    – nnnnnn
    Commented Oct 30, 2015 at 6:18
  • yes exactly, but the page never postback to server again.
    – OMID
    Commented Oct 30, 2015 at 6:21
  • @OMID that does not matter. once it's client side, the server will not know about the field, and won't render it. the browser will see an attribute it does not care about, and will mostly just ignore it. Commented Oct 30, 2015 at 8:00
  • oh hang on. ASP.NET is rendering the control inside your string definition Commented Oct 30, 2015 at 8:04
  • in fact I want add an ASP.NET element by using javascript to the page
    – OMID
    Commented Oct 30, 2015 at 8:10

1 Answer 1

2

ASP.NET is rendering the control inside your string definition.

The output of that <asp:DropDownList> contains newlines, double quotes, and references to javascript functions, so it will definitely make a mess of your javascript string.

Instead, let asp.net render the dropdown somewhere else (it can even be inside an invisible div) like this :

<div id="hiddenthingContainer" style="display:none;">
   <asp:dropdownlist /> ... etc
</div>

Then, either use document.getElementById("hiddenthingContainer") or use jQuery or whatever dom library you prefer to get the element.

once you have it, it becomes a simple matter of getting the contents of the hidden container and presto, there's your string.

example using jQuery :

<div id="hiddenthingContainer" style="display:none">
     <asp:DropDownList ID='drpid' runat='server' DataSourceID='SqlDataSource1' DataValueField='Id' DataTextField='Text'></asp:DropDownList>
</div>

<script type="text/javascript">
$(document).ready(function(){
    var dropdown = $("#hiddenthingContainer").html()
});
</script>
5
  • Keep in mind that this might not end up doing everything you want it to do. Asp.Net does a lot of stuff behind the scenes to hook up listeners, bind/render your view etc. Commented Oct 30, 2015 at 8:16
  • why you get html here if you can use element? and also get DropDown instread div
    – Grundy
    Commented Oct 30, 2015 at 8:25
  • @Grundy true, you could just use the element instead of fetching it as a string. Commented Oct 30, 2015 at 8:26
  • Thank you for your good answer, but May be an infinite number of rows in the table. and of course an infinite number of dropdowns
    – OMID
    Commented Oct 30, 2015 at 8:39
  • 1
    there can never be an infinite number of rows in the table ;) Commented Oct 30, 2015 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.