0

I have div control which runs on server

<div id="report" runat="server" ></div>

I'm appending some content to the above div using jquery at run time. Now i want to access the innerHtml of the above div.

example:

<div id="report" runat="server" >
  <div class="someclass">some Text1</div>
  <div class="someclass">some Text2</div>
  <div class="someclass">some Text3</div>
</div>

expected output is(following HTML in a string format):

  <div class="someclass">some Text1</div>
  <div class="someclass">some Text2</div>
  <div class="someclass">some Text3</div>

so far i tried the following two methods but no luck

ContentPlaceHolder myContent = (ContentPlaceHolder)this.Master.FindControl("contentPlaceHolder");
Control cc= myContent.FindControl("report");

and

string HTMLString=report.InnerHtml;
vallabha
  • 385
  • 1
  • 12

1 Answers1

1

Form input elements inside a form element posted to server.
All other content is static and not posted to server.
It's fundamental rule of HTTP, you can see details from here.
So you cannot get the report.InnerHtml.

You have two option, while preparing your div's content, write the same content inside a hidden field. And at server side get the hidden field's value.

<input type="hidden"  id="hdnDivContents" runat="server">

and set the value using jquery or javascript.

$('#<% hdnDivContents.ClientID %>').val($("#<% report.ClientID %>").val());

and get the value in code behind

string HTMLString=hdnDivContents.Value;

Or make an AJAX call while preparing your content to get it at server side.

शेखर
  • 17,412
  • 13
  • 61
  • 117
  • i have tried the hidden field solution but ended with run time exception(`A potentially dangerous Request.Form value was detected from the client (ctl00$contentPlaceHolder$hdnDivContents="
    – vallabha Oct 28 '14 at 05:03
  • You need to allow this for your page. [link](http://stackoverflow.com/questions/2673850/validaterequest-false-doesnt-work-in-asp-net-4) – शेखर Oct 28 '14 at 05:21
  • same exception after doing the changes as you suggested – vallabha Oct 28 '14 at 05:51