简体   繁体   English

无法获取文本框值

[英]Cannot get a textbox value

I have 2 textboxes on an Asp.net(with c#) page and I cannot see this fields from cs class. 我在Asp.net(带有c#)页面上有2个文本框,我无法从cs类中看到这些字段。

In aspx: 在aspx中:

 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <ajaxToolkit:ToolkitScriptManager runat="server"></ajaxToolkit:ToolkitScriptManager>

<div>
<table>
<tr>
   <td style="width:40%"> <asp:Label ID="lblFullname" runat="server" Text="Fullname" ></asp:Label></td>
   <td style="width:20%"> </td>
   <td style="width:40%"> 
       <asp:TextBox ID="txtFullname" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
   <td style="width:40%"> <asp:Label ID="lblBirthDate" runat="server" Text="BirthDate" ></asp:Label></td>
   <td style="width:20%"> </td>
   <td style="width:40%">
      <asp:TextBox ID="txtBirthDate" runat="server" Text="" ></asp:TextBox> 
      <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtenderBirthDate" TargetControlID="txtBirthDate" runat="server" 
      UserDateFormat="DayMonthYear" Mask="99/99/9999" MaskType="Date" ></ajaxToolkit:MaskedEditExtender>
   </td>
</tr>
<tr><td colspan="3" style="float:right"><asp:Button Text ="Save"  runat="server" ID="btnSave" OnClick="btnSave_Click"/> </td></tr>
</table>
</div>

And I am trying to get value from the txtFullname.I try txtFullname.Text and this.txtFullname but the application cannot see this values . 我试图从txtFullname.I尝试txtFullname.Text和this.txtFullname获取值,但应用程序无法看到此值。 Can somebody how can I get the value for that textbox? 有人可以如何获得该文本框的值?

The table tag is missing runat="server" attribute. 表标记缺少runat =“server”属性。 Add it in the tag. 将其添加到标记中。 You should be able to see the fields in cs file 您应该能够看到cs文件中的字段

Please check if you have specified the codebehind file name correctly. 请检查您是否正确指定了代码隐藏文件名。 This could be one of the issues for not getting the control name inside the code file. 这可能是未在代码文件中获取控件名称的问题之一。

If that isn't working for you, please use the below code for reference. 如果这不适合您,请使用以下代码作为参考。

You have not included your "Register" directive. 您尚未包含“注册”指令。 So, I took to privilege to give my own. 所以,我很荣幸能够给自己。

Change your markup to: 将您的标记更改为:

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="WebApplication2._Default" %> 
   <%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>
        <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="Server" />
    <div>
        <table>
            <tr>
                <td style="width: 40%">
                    <asp:Label ID="lblFullname" runat="server" Text="Fullname"></asp:Label>
                </td>
                <td style="width: 20%">
                </td>
                <td style="width: 40%">
                    <asp:TextBox ID="txtFullname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 40%">
                    <asp:Label ID="lblBirthDate" runat="server" Text="BirthDate"></asp:Label>
                </td>
                <td style="width: 20%">
                </td>
                <td style="width: 40%">
                    <asp:TextBox ID="txtBirthDate" runat="server" Text=""></asp:TextBox>
                    <asp:MaskedEditExtender id="MaskedEditExtenderBirthDate" targetcontrolid="txtBirthDate"
                        runat="server" userdateformat="DayMonthYear" mask="99/99/9999" masktype="Date"></asp:MaskedEditExtender>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="float: right">
                    <asp:Button Text="Save" runat="server" ID="btnSave" OnClick="btnSave_Click" />
                </td>
            </tr>
        </table>
    </div>
</asp:Content>

This code has been tested and is working. 此代码已经过测试并正在运行。

Are you trying in master page or the page file ? 您是在主页还是页面文件中尝试? It should work with runat="server" 它应该与runat="server"

You code is showing that you are using the content page which must use the master page. 您的代码显示您正在使用必须使用母版页的内容页面。 so you should check following things to resolve this issue. 所以你应该检查以下事项来解决这个问题。

  1. You page must contain the Page directive similar to following one. 您的页面必须包含与下一个类似的Page指令。 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Page Title =“Home Page”Language =“C#”MasterPageFile =“〜/ Site.master”AutoEventWireup =“true”CodeFile =“Default.aspx.cs”Inherits =“_ Default”%>

  2. Codebehind file name must be correct like page Default.aspx and Default.aspx.cs Codebehind文件名必须正确,如页面Default.aspx和Default.aspx.cs

  3. You must use runat="server" to make control accessible at server side. 必须使用runat =“server”才能在服务器端访问控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM