1

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!

3
  • 1
    that should work ... are you sure you're running the code when the document is ready? Commented Mar 30, 2012 at 17:31
  • 1
    That won't work as DIV doesn't get posted to server. If you want to take that data to server, copy it to a form control (say hidden input). Commented Mar 30, 2012 at 17:36
  • 2
    It is because the javascript runs client-side and the C# code runs server-side. The contents (inner html) of the div is not posted back to the server so there is no way that your C# code can access it. Commented Mar 30, 2012 at 17:39

3 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

@Umm, yes, certainly, but it's not clear what you are trying to do. 1) Server produces HTML and sends it to the browser 2) Browser runs javascript and sets the div to contain "some crazy data" 3) You want to consume that data...when? Where? Why? Please elaborate on your use case and we can recommend an approach (possibly AJAX).
1

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;

1 Comment

It should be hiddenResults.Value
1

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.

6 Comments

However, there is no reason for your code not to work, unless results points to null
OP is accessing the data on the server not on the client.
@KirkWoll, LOL, Can't believe I copied that from the OP. Thanks Updated
@Starx, you modified your code but missed my point. The OP was using string myResults. Which means he's trying to do it on the server-side as amit_g mentioned.
@KirkWoll, I understand it now, So, fixed my answer a bit.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.