我无法在代码隐藏文件中访问<asp:textbox>值

时间:2016-10-18 13:48:35

标签: 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;
            }
}

基本上所有这些需要做的是从网站上的按钮板向文本框添加帐号。

添加帐户后,他们会点击继续按钮,然后后面的代码检查文本框是否为空,如果它不为空它连接到数据库以检查它是否有效。

但无论我在文本框中添加多少个数字,当我从后面的代码访问它时它仍然是空的。

2 个答案:

答案 0 :(得分:5)

Enabled="False"表示您的输入字段将具有disabled属性。禁用的表单元素不会以HTML表单形式发布,这意味着当您从代码隐藏中访问它时,它会使用文本框的默认值。

如果您不想手动编辑字段,请尝试ReadOnly="True"而不是Enabled="False"

修改

我猜ASP.NET阻止您从ReadOnly=True文本框中读取值,以防止您完全按照您要做的操作。 <asp:HiddenField>创建<input type="hidden">,您应该能够写入该值并将其正确发布到代码隐藏。

答案 1 :(得分:-3)

试试这个。

Request.Form["by-name-attribute"];