在我的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
答案 0 :(得分:1)
试试这个:
int value = Convert.toInt32(Request.Form["txtTime"]);
答案 1 :(得分:1)
我猜你的网页上发布的内容比你在这里发布的要多得多。我已经使用您发布的内容快速删除了样本,并且工作正常。我已经在下面提供了完整的示例 - 我建议您将它与您的产品进行比较,以找出不同之处:
<%@ 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>