-1

I need to get a JavaScript variable from my code behind without doing a page refresh or a button click event. Here's my code:

aspx:

<asp:HiddenField ID="docLengthValue" runat="server" />
<script type="text/javascript">
var body = document.body,
    html = document.documentElement;
var height = Math.max(body.scrollHeight, body.offsetHeight,
    html.clientHeight, html.scrollHeight, html.offsetHeight);
//alert(height + ": Page length");
document.getElementById("<%=docLengthValue.ClientID%>").setAttribute("Value", height);
</script>

c#:

//Skrollr body tag background parallax animation
string docLengthVar = docLengthValue.Value;
HtmlGenericControl skrollr = (HtmlGenericControl)this.Page.Master.FindControl("bodyBG");
            skrollr.Attributes.Add("data-0", "background-position: 0px -120px;");
            skrollr.Attributes.Add("data-" + docLengthVar, "background-position: 0px 0px;");
            dataAttb.Text = "This is the Document length:&nbsp;&nbsp;" + docLengthVar;

How can I access the Value field of the <asp:HiddenField ID="docLengthValue" runat="server" />? I know that the JavaScript compiles after the C#, but is there a way to get this variable?

Jeff Mcbride
  • 453
  • 2
  • 6
  • 19
  • Why do you want to do this? There is almost certainly a much better way of accomplishing what you're looking to do. Can you describe the motivation for your question? – NWard Jun 25 '14 at 22:46
  • You could always send it to a handler with an AJAX request. – itsme86 Jun 25 '14 at 22:50
  • Are you are trying to get the [page length?](http://stackoverflow.com/questions/24415257/using-c-sharp-to-get-an-aspx-page-document-length) Anyway this is a duplicate of how to use [ajax within webforms, an extensively discussed subject.](http://stackoverflow.com/questions/202538/using-jquery-for-ajax-with-asp-net-webforms). What have you tried and what is not working? – TheNorthWes Jun 25 '14 at 22:58

1 Answers1

1

Jeff-

This is not really going to be an answer, and I apologize but I think you may be quite far off course here.

Your recent post history seems to indicate that you want a parallax effect on your web page. The posts I am referencing: 1 and 2.

Have you followed this tutorial? Where along it have you had a problem?

General

I think you have some confusion about how the your web pages are working. WebForms is a ASP technology, or active server page. You can read more about this but basically it provides a platform which you can more easily develop responsive web sites.

The JS you are writing is a technology that runs entirely in the browser. When the JavaScript runs the code has no idea that it is in an ASPX page. It has no idea how to talk to the code behind.

When your page renders it is pure and raw html that the client receives. Through the magic of the platform when the user submits the form or clicks a button your code behind runs. But that is nothing you can't do on your own. All that is happening is your browser is sending properly formatted HTTP requests, and your web server is dispatching those to pages (code / class(es).

Relation to your problem

You are trying to animate a background image on a web page. That is an entirely client side job. You do not want the client to be talking to the web server frequently enough to create a smooth transition. In reality it is likely impossible.

The stuff you are looking at is pure JavaScript and runs entirely in the browser. I highly encourage you to pursue that as the avenue to solve your problem. And then ask questions as you have problems with that.

Edit: Apologies if I have inferred too much. My suggestions are in earnest.

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35
  • admiral, I was way off course. You are actually correct... Dunno was I was trying to do part in JavaScript and part in C#. This can all be accomplished in JavaScript. But... Does my JavaScript code get the actual height of the webpage after it is rendered? When I tested the all JavaScript version, it doesn't seem to be the right value. Also, should the JavaScript go into the Masterpage (where the body tag is located)? – Jeff Mcbride Jun 26 '14 at 04:52
  • I doubt the JS would get the 'wrong' value from a built in feature, more likely it isn't exactly what you think it is. If you background is on every page and you want that effect then I think the masterpage should be fine. – TheNorthWes Jun 26 '14 at 15:42
  • I realized this needs to be all done in JavaScript. I have a method in mind. – Jeff Mcbride Jun 26 '14 at 21:47