0

I am trying to pass values which I am taking from an external js File into my .aspx page.

Here Is my .js file code:

function GETdateTime() {
    var d = new Date()
    var date = new String(d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear());
    var time = new String(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
    document.getElementById("test").outerHTML = "<br>" + date + "<br>" + time;
}

In my .aspx page I am calling the above function and retrieving results like:

 <form id="form1" runat="server" method =" post">
    <span id = "test"> </span>

    <script type =" text/javascript" src="JavaScript1.js"  >
    </script>     

     <script type ="text/javascript">
            GETdateTime();
     </script>
</form>

What I am trying to achieve is that I am planning to have one HiddenField which would store the date, time and other things and then grab the values in that HiddenField and pass them to my vb.net code behind to store them in a string. The problem is that I have seen to many approaches and I am kind of confused with which one to go with like here,here and here. I am also considering efficiency, consistency, flexibility and overall performance. Any thoughts or suggestions would be appreciated.

Community
  • 1
  • 1
HShbib
  • 1,811
  • 3
  • 25
  • 47
  • you may consider this answer as well: http://stackoverflow.com/questions/14603256/passing-values-from-javascript-to-code-behind-in-asp-net – Aristos Feb 01 '13 at 13:06

1 Answers1

0

If you have a form like this:

<form id="form1" runat="server" method =" post">
    <asp:HiddenField runat="server" id="test" />
    <script type =" text/javascript" src="JavaScript1.js"  >
    </script>     

     <script type ="text/javascript">
            GETdateTime("<%=test.ClientID %>");
     </script>
</form>

Than you need a javascript:

function GETdateTime(hiddenFieldID) {
    var d = new Date()
    var date = new String(d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear());
    var time = new String(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
    document.getElementById(hiddenFieldID).value = "<br>" + date + "<br>" + time;
}

Than, in code behind after form submitted, to get a value set with JS you can simply do:

hiddenFieldVal = test.Value 
Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • ok so in `function GETdateTime(hiddenFieldID) ` the `hiddenFieldID` do I have to write it down like `function GETdateTime (test) also the same for `document.getElementById(test)? OR just leave the brackets empty as it is passed through the function in my html? – HShbib Feb 01 '13 at 13:20
  • See my answer. There is a line: `GETdateTime("<%=test.ClientID %>"); ` - That is how you should pass it. – Viktor S. Feb 01 '13 at 13:32
  • test.Value - after form is submitted and in code behind? Also - have you checked if there are no JS errors? – Viktor S. Feb 01 '13 at 13:44
  • If I do the js file in Visual studio, I do not get the `.value` attribute of `document.getElementByID(test)`. The related things I get when I write the `.` is `outerHTML, outerText, InnerText, InnerHTML`. Visual Studio doesn't tell you an error in a .js file. I coppied it into DreamWeaver and it doesn't say that there is an error, however it does take the `.value` attribute. Also, I do not have a button to submit the form, everything should work on `PageLoad` event in asp.net and `window.onload` in .js external file. – HShbib Feb 01 '13 at 14:05
  • First: Why are you doing this: `document.getElementByID(test)`? Have you seen my code? It has everything you need to make your code work under asp.net. – Viktor S. Feb 01 '13 at 14:09
  • Here: `GETdateTime("<%=test.ClientID %>");` you take an actual id of hidden field and pass it to JS. In browser, you will get: `GETdateTime("cn101_test");` or something like that, where cn101_test is actual id of hidden field in browser. – Viktor S. Feb 01 '13 at 14:10
  • And here: `document.getElementById(hiddenFieldID).value` I'm taking that element using passed ID. – Viktor S. Feb 01 '13 at 14:11
  • Studio and dreamweaver will not show you errors in really a lot of cases when they have a deal with JS. You should open browser console when you run your page in browser and see errors there (Press F12 to see it, you may need to install firebug for FF) – Viktor S. Feb 01 '13 at 14:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23782/discussion-between-humam-shbib-and-fangel) – HShbib Feb 01 '13 at 14:31