javascript - 我无法访问 <asp :textbox> value in code behind file

标签 javascript c# html asp.net

早上好

我有一个带有按钮板的网页。它在网络不好的加油站使用,所以每次他们点击按钮向文本框中添加数字时,它都会永远加载,所以我将按钮点击更改为 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/

相关文章:

c# - 从后面的代码修改一个 HTML 标签级别的 CSS 选择器

c# - 从对象数组中获取字符串数组

javascript - 用于移动设备的 HTML5 拖放

JavaScript 代码在 jQuery load() 之后不影响 div

javascript - 当父选择为空时,子选择列表中的默认值消失

javascript - 单击来自另一个窗口的链接时激活打开的窗口选项卡

javascript - 如何在沙盒 iframe (IE11) 中使用 javascript 创建 iframe 内容?

c# - 如何打开新表单,传递参数和返回参数

html - NSAttributedString 从 HTML 暗模式初始化,如何设置默认前景色?

jquery - 保存由 Jquery 动态创建的页面