Click here to Skip to main content
15,900,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function on pageload where I am using values from labels(day and month). I am getting values of these labels from a js function in a sql query but these values remain empty at the time of pageload coz js runs after pageload. Is there a way to resolve this?? Any help would be greatly appreciated.
Thanks

js function is a follows:
JavaScript
<script type="text/javascript">
    function time() {
        var currentTime = new Date();
        var day = currentTime.getDate();
        var month = currentTime.getMonth() + 1;
        document.getElementById("day").value = day;
        document.getElementById("month").value = month;
    }
</script>


Sql query is as follows:

C#
"select logo,companyname from logos where status='True' AND type='Birthdays' AND day='"+day.Text.ToString()+"' AND month='"+month.Text.ToString()+"'"
Posted
Updated 27-Jun-13 2:08am
v2

For this you have to first understand how ASP.NET applications work.
When the page is requested by the browser for the first time, the request is sent to the web server where ASP.NET engine process your request and converts it into HTML and renders back to the browser. This is when your JavaScript and HTML are available to the browser. Now you can execute any JavaScript code and change a control's value. And if you post the page again, the ASP.NET will then have the updated value.

Now let's consider your scenario. You want the value of day and month controls on the server side and you assign them values on client side (using javascript). So these values will only be available once you POST the form back to asp.net server (called a postback).
I see that your calculation isn't based on any user input. So this can also be done on the server side. On the page load calculate these values (assign them to the controls if needed) and call the database query.
That will solve your problem.

If you control values still have to depend on JavaScript calculations, you will have to post the form either completely or partially (using AJAX).
 
Share this answer
 
v2
Comments
lucky050 27-Jun-13 8:25am    
Thanx for the prompt reply.. what i am trying to do here is that i want client system's day and month so that my page will display birthdays on this day according to client's system... any help would be greatly appreciated..
Ankur\m/ 28-Jun-13 2:27am    
I am not sure if it is possible if it's the "very first" request by the client unless you track his IP and find his location based on that and then convert the time based on his location.
Another option is after the page is loaded, you can send a asynchronous request to the server passing these values and get the result back and display it. It would be available after a slight delay though.
Ankur\m/ 28-Jun-13 2:35am    
Also read this link - http://msdn.microsoft.com/en-us/library/bb882561.aspx
Should be helpful.
You can create same query in Page_Load itself as long you don't need any input from user using below code

protected void Page_Load(object sender, EventArgs e)
    {
        var query = "select logo,companyname from logos where status='True' AND type='Birthdays' AND day='" + DateTime.Now.Day.ToString() + "' AND month='" + DateTime.Now.Month.ToString() + "'";
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900