0

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 
3
  • 1
    @Shyju - Please use please instead of pls. Commented Aug 5, 2012 at 17:34
  • 1
    try int value = Convert.toInt32(Request.Form["txtTime"]); Commented Aug 5, 2012 at 17:45
  • Anyone knows why my way didn't work ? Commented Aug 5, 2012 at 17:50

2 Answers 2

1

try this:

 int value = Convert.toInt32(Request.Form["txtTime"]);
Sign up to request clarification or add additional context in comments.

1 Comment

You should not need to access Request.Form when doing WebForms unless there is a specific reason. In this example it's definitely not required - the sample in my answer works fine without it.
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.