簡體   English   中英

ASP.NET TextBox - 文本屬性

[英]ASP.NET TextBox - text property

在我的ASP.NET表單中,我有一個文本框,使用javascript代碼每10秒更改一次該值。 文本框的初始值為10,每10秒事件高出5。 當我單擊我的提交按鈕,並嘗試訪問myTextBox.Text屬性時,我得到值10,而不是我的文本框中的當前值。 這里有什么問題,為什么我看不到文本框中寫的當前值?

我的客戶端代碼:

var d = 0;
    var interval = 0;
    function initializeMe() {
        d = document.getElementById("<%= time.ClientID %>").innerHTML;
        interval = self.setInterval("clock()", 10000);
    }
    function clock() {
        if (d > 0) {
            document.getElementById("<%= time.ClientID %>").innerHTML = d;
            document.getElementById("<%= txtTime.ClientID %>").value = d.toString();
            d = d - 1;
        }
        else {

            d = "Message";
            document.getElementById("<%= vrijeme.ClientID %>").innerHTML = d;
            document.getElementById("<%= txtVrijeme.ClientID %>").value = "0";
        }
    }



<form id="form1" runat="server">

    <div>

        <asp:Label ID="time" runat="server"></asp:Label>
        <asp:TextBox ID="txtTime" runat="server"></asp:TextBox>
        <asp:Button ID="submit" runat="server" Text="Sumbmit" 
            onclick="sumbit_Click"/>


    </div>
</form>

我的服務器代碼

int value = 700 - Convert.ToInt16(txtTime.Text);//But here server reads wrong value 

嘗試這個:

 int value = Convert.toInt32(Request.Form["txtTime"]);

我猜你的網頁還有很多比你在這里發布的更多。 我已經使用您發布的內容快速刪除了樣本,並且工作正常。 我已經在下面提供了完整的示例 - 我建議您將它與您的產品進行比較,以找出不同之處:

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">

    protected void sumbit_Click(object sender, EventArgs e)
    {
        int value = 700 - Convert.ToInt16(txtTime.Text);
        lblResult.Text = value.ToString();
    }

</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="time" runat="server" Text="100"></asp:Label>
        <asp:TextBox ID="txtTime" runat="server"></asp:TextBox>
        <asp:Button ID="submit" runat="server" Text="Sumbmit"
            OnClick="sumbit_Click" />

        <hr />
        Result: <asp:Label runat="server" ID="lblResult" />
    </div>

    </form>

    <script type="text/javascript">
        var d = 0;
        var interval = 0;

        function initializeMe() {
            d = document.getElementById("<%= time.ClientID %>").innerHTML;
            interval = self.setInterval(function () { clock(); }, 1000);
        }
        function clock() {
            if (d > 0) {
                document.getElementById("<%= time.ClientID %>").innerHTML = d;
                document.getElementById("<%= txtTime.ClientID %>").value = d.toString();
                d = d - 1;
            }
        }

        initializeMe();

    </script>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM