Umm....
Umm....

Reputation: 79

jQuery.html() not working well with Asp.Net innerHtml

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!

Upvotes: 1

Views: 1442

Answers (3)

amit_g
amit_g

Reputation: 31250

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;

Upvotes: 1

Starx
Starx

Reputation: 78971

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.

Upvotes: 1

Kirk Woll
Kirk Woll

Reputation: 77546

This line looks like C#:

string myResults = results.innerHtml;

Presumably you're trying to access your server-side DIV in C#. That happens before the html is sent to the browser. Which means, that's not going to work at all since you're setting the value client-side in the browser (after the html has already been sent).

Upvotes: 4

Related Questions