MrD
MrD

Reputation: 2481

ASP.NET TextBox - text property

In my ASP.NET form I have one text box, which value gets changed every 10 seconds using javascript code. Initial value of text box is 10, and gets higher by 5 on every 10 seconds event. When I click my submit button, and try to access myTextBox.Text property, I get value of 10, instead of current value in my text box. What is the problem here, why I can't see the current value written in my text box<'

My client side code:

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>

My server code

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

Upvotes: 0

Views: 1110

Answers (2)

Richard
Richard

Reputation: 30628

I'm guessing there's a lot more going on with your page than you've posted here. I've knocked up a quick sample using what you've posted, and it worked fine. I've included the complete sample below - I suggest you compare it to yours to find out what's different:

<%@ 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>

Upvotes: 1

Thousand
Thousand

Reputation: 6638

try this:

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

Upvotes: 1

Related Questions