早上好
我有一个带有按钮板的网页。它在网络不好的加油站使用,所以每次他们点击按钮向文本框中添加数字时,它都会永远加载,所以我将按钮点击更改为 JavaScript。
它工作得很好,但现在当我从文本框中检索值时,后面的代码会得到一个空值(非空)。
这是我的代码
用于将数字添加到文本框的按钮 和文本框
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="OVKWEBAPP.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="mainstyle.css" />
<style type="text/css">
.modal
{
position: fixed;
top: 0;
left: 0;
/*background-color: black;*/
z-index: 99;
opacity: 0.8;
filter: alpha(opacity=80);
-moz-opacity: 0.8;
min-height: 100%;
width: 100%;
}
.loading
{
font-family: Arial;
font-size: 10pt;
border: 5px solid #67CFF5;
width: 200px;
height: 100px;
display: none;
position: fixed;
background-color: White;
z-index: 999;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtRknr" runat="server" CssClass="txtBoxRknr" PlaceHolder="Rekening Nommer" Enabled="False" autocomplete="off" Width="175px"></asp:TextBox>
<input type="button" id="btnPad1" runat="server" value="1" class="buttonsb" onclick="input(this);"/>
<input type="button" id="btnPad2" runat="server" value="2" class="buttonsb" onclick="input(this);"/>
<asp:Button ID="btnMsg1" runat="server" Text="Gaan Voort" CssClass="buttonsa" OnClick="btnMsg1_Click" />
</form>
</body>
<script type="text/javascript">
function input(e) {
document.getElementById("<%=txtRknr.ClientID %>").value = document.getElementById("<%=txtRknr.ClientID %>").value + e.value;
}
</script>
</html>
这是我在代码隐藏文件中的代码
protected void btnMsg1_Click(object sender, EventArgs e)
{
lblError.Text = "";
//string user = txtRknr.Text;
string user = ((TextBox)FindControl("txtRknr")).Text;
if(user == "")
//if (txtRknr.Text == "")
{
lblError.Text = "ONGELDIGE REKENING NOMMER";
return;
}
}
基本上,所有这些需要做的就是从网站上的按钮板将帐号添加到文本框中。
添加帐户后,他们将单击“继续”按钮,然后后面的代码检查文本框是否为空,如果不为空,则会连接到数据库以检查它是否有效。
但是,无论我向文本框添加多少数字,当我从后面的代码访问它时,它仍然是空的。
最佳答案
Enabled="False"
意味着您的输入字段将有 disabled
属性。禁用的表单元素不会发布在 HTML 表单中,这意味着当您从代码隐藏中访问它时,它会使用文本框的默认值。
尝试ReadOnly="True"
而不是Enabled="False"
如果您不希望手动编辑该字段。
编辑
我猜 ASP.NET 会阻止您读取 ReadOnly=True
中的值文本框,以防止您完全执行您想要执行的操作。一个<asp:HiddenField>
创建 <input type="hidden">
,您应该能够写入该值并将其正确发布到代码隐藏中。
关于javascript - 我无法访问 <asp :textbox> value in code behind file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110082/