March 30, 2012
jQuery.html() not working well with Asp.Net innerHtml
Question by Umm….
So basically, I have a div which is runat=”server”
<div id="results" runat="server" class="results"></div>
Now, I add HTML to this using jQuery.
$('.results').html('Some crazy data');
However, when I try to access the data in this DIV using C#, it says the DIV has no contents.
string myResults = results.innerHtml;
myResults is empty.
Can someone help me out? I’ve never seen this behavior before.
Many Thanks!
Answer by amit_g
You may be looking for something like this…
<div id="results" runat="server" class="results"></div>
<input type="hidden" id="hiddenResults" runat="server" class="hiddenResults" />
$('.results').html('Some crazy data');
$('.hiddenResults').val('Some crazy data');
Now you can access the data on server
string myResults = hiddenResults.Value;
Answer by Starx
Instead of mixing up jQuery and Javascript. Try
var myResults = $(".results").html();
Just to verify everything is correct, see if your results
is defined as below
var results = document.getElementById("results");
var myResults = results.innerHtml;
If you trying to get the value of results
to the server, it is not possible through jQuery or Javascript unless send an AJAX request.