在pageload之前调用javascript [英] Calling javascript before pageload

查看:85
本文介绍了在pageload之前调用javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pageload上有一个函数,我使用标签中的值(日和月)。我从sql查询中的js函数获取这些标签的值,但这些值在页面加载后的页面加载coz js运行时保持为空。有办法解决这个问题吗?任何帮助将不胜感激。

谢谢



js功能如下:

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:

<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查询如下:





Sql query is as follows:

"select logo,companyname from logos where status='True' AND type='Birthdays' AND day='"+day.Text.ToString()+"' AND month='"+month.Text.ToString()+"'"

推荐答案

为此,您必须先了解ASP.NET应用程序的工作原理。

该页面首次被浏览器请求,请求被发送到Web服务器,其中ASP.NET引擎处理您的请求并将其转换为HTML并呈现回浏览器。这是您的JavaScript和HTML可供浏览器使用的时间。现在,您可以执行任何JavaScript代码并更改控件的值。如果你再次发布页面,那么ASP.NET将具有更新的值。



现在让我们考虑你的场景。您需要服务器端的日期和月份控件的值,并在客户端分配它们的值(使用javascript)。因此,只有将表单发回asp.net服务器(称为回发)后才能使用这些值。

我看到您的计算不是基于任何用户输入。所以这也可以在服务器端完成。在页面上加载计算这些值(如果需要,将它们分配给控件)并调用数据库查询。

这将解决您的问题。



如果控制值仍然必须依赖于JavaScript计算,则必须完全或部分地发布表单(使用AJAX)。
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).


您可以在Page_Load本身中创建相同的查询很久你不需要用户使用下面的代码输入



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() + "'";
    }


这篇关于在pageload之前调用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆