简体   繁体   English

为在页面加载时从javascript函数获取的文本框赋值

[英]assign value to a textbox obtained from a javascript function on page load

Quick background: I am new to programming and have been using VisualStudio, ASP.NET, and C# for a couple months. 快速背景:我是编程新手,已经使用VisualStudio,ASP.NET和C#几个月了。 I have no experience with JavaScript. 我没有使用JavaScript的经验。 I am trying to convert times from UTC to the time local to the browser viewing a page. 我正在尝试将时间从UTC转换为浏览器查看页面的本地时间。 After research I believe my best option for the various things I need to do is to detect the local offset from UTC on page load and then hold that information to use for local conversion and postback to the server. 经过研究后,我认为我需要做的各种事情的最佳选择是在页面加载时检测UTC的本地偏移量,然后保存该信息以用于本地转换和回发到服务器。

I have written the following js function in my "Source" page: 我在“源”页面中编写了以下js函数:

<script type="text/javascript" >
    function calculateOffset() {
        var rightNow = new Date();
        var UTCTime = rightNow.getUTCHours();
        var LocalTime = rightNow.getHours();
        var UTCDate = rightNow.getUTCDate();
        var LocalDate = rightNow.getDate();
        if (UTCDate == LocalDate) {
            var offset = LocalTime - UTCTime;
        }
        else {
            var offset = LocalTime - 24 - UTCTime;
        }
        return offset;
    }
</script>

I then have a TextBox Control that I would like to use to hold the offset returned: 然后我有一个TextBox控件,我想用它来保存返回的偏移量:

<asp:TextBox ID="HiddenOffsetBox" runat="server" Visible="False" Text ="<%# calculateOffset() %>" ></asp:TextBox>

I receive the following error on debugging: Compiler Error Message: CS0103: The name 'calculateOffset' does not exist in the current context 我在调试时收到以下错误:编译器错误消息:CS0103:当前上下文中不存在名称'calculateOffset'

This is in a child page inheriting from a master page, I have tried placing the JS in both the head and body content, I have also created a .js file but haven't figured out how to call it. 这是在从母版页继承的子页面中,我尝试将JS放在头部和正文内容中,我还创建了一个.js文件,但还没弄明白如何调用它。 Primarily I am looking for a way to get the offset output from the JS function into the textbox but if you have any other suggestions I welcome them. 主要是我正在寻找一种方法来将JS函数的偏移量输出到文本框中,但如果您有任何其他建议我欢迎他们。 Thanks ahead of time! 提前谢谢!

EDIT I created a test asp.net web form to try the suggestion below, to simplify everything here is the code: 编辑我创建了一个测试asp.net Web表单来尝试下面的建议,简化这里的一切是代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="GTLWebApp.TestForm" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script type="text/javascript" >

        $(document).ready(function () {
        // recommend using jQuery to make sure the page is loaded
        document.getElementById('<%= HiddenOffsetBox.ClientID%>').text =   calculateOffset();
        //or
        //jQuery way
        //$('#<%= HiddenOffsetBox.ClientID%>').val(calculateOffset());

        function calculateOffset() {
            var rightNow = new Date();
            var UTCTime = rightNow.getUTCHours();
            var LocalTime = rightNow.getHours();
            var UTCDate = rightNow.getUTCDate();
            var LocalDate = rightNow.getDate();
            if (UTCDate == LocalDate) {
                var offset = LocalTime - UTCTime;
            }
            else {
                var offset = LocalTime - 24 - UTCTime;
            }
            return offset;
        }
        </script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="HiddenOffsetBox" runat="server" Visible="True" Text = ""></asp:TextBox>
        </div>
        </form>

    </body>
    </html>

When I run this I get no errors but I don't get the offset in the text box, any further help would be appreciated... 当我运行这个我没有错误,但我没有得到文本框中的偏移量,任何进一步的帮助将不胜感激...

<script type="text/javascript" >
    //un-comment for jQuery
    //recommend using jQuery to make sure the page is loaded
    //$(document).ready(function() {            
       // $('#<%= HiddenOffsetBox.ClientID%>').val(calculateOffset());    
    //});

    document.getElementById('<%= HiddenOffsetBox.ClientID%>').value = calculateOffset();

    function calculateOffset() {
        var rightNow = new Date();
        var UTCTime = rightNow.getUTCHours();
        var LocalTime = rightNow.getHours();
        var UTCDate = rightNow.getUTCDate();
        var LocalDate = rightNow.getDate();
        if (UTCDate == LocalDate) {
            var offset = LocalTime - UTCTime;
        }
        else {
            var offset = LocalTime - 24 - UTCTime;
        }
        return offset;
    }
</script>

<asp:hidden ID="HiddenOffsetBox" runat="server" value="" ></asp:hidden>

The following is working for me. 以下是为我工作。

this.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script type='text/javascript'> calculateOffset() </script>");

Just paste this line on your Page_Load event. 只需在Page_Load事件上粘贴此行即可。

Also instead of "return offset" you can directly assign the value of "offset" to the textbox as: 也可以直接将“offset”的值分配给文本框,而不是“返回偏移量”:

document.getElementById("<%=HiddenOffsetBox.ClientID %>").value = offset;

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM