C# 无法找到 ASP.net 个文本框的内容

C# unable to find content of ASP.net Textboxes

好的,所以我正在尝试创建一种注册向导,所以 asp 中有不同的部分,例如下面的部分:

  <div class="panel panel-default">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a href="#addwizard" data-parent="#accordion-demo" data-toggle="collapse">
              Term Time Address
            </a>
          </h4>
        </div>
        <div id="termaddy" class="panel-collapse collapse in"  runat="server">
          <div class="panel-body">
            <form id="form-termaddy" >
          <div class="main_input_box">
                 <div class="col-lg-6">

                    <div class="form-group">
                     <asp:TextBox ID="txttermhousenum" runat="server" placeholder="House Number" class="form-control" textmode="SingleLine"></asp:TextBox>
                         <asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator28" controltovalidate="housenum" errormessage="Required Field" display="Dynamic"/>
                 </div>

                   <div class="form-group">
                    <div class="main_input_box">
                     <asp:TextBox ID="txtxtermstreet" runat="server" placeholder="Street" class="form-control"  textmode="SingleLine"></asp:TextBox>
                         <asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator29" controltovalidate="street" errormessage="Required Field" display="Dynamic"/>
                 </div>
                       </div>

                   <div class="form-group">
                    <div class="main_input_box">

                     <asp:TextBox ID="txtxtermpost" runat="server" placeholder="Postcode" class="form-control" textmode="SingleLine"></asp:TextBox>
                         <asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator31" controltovalidate="postcode" errormessage="Required Field" display="Dynamic"/>
                 </div>
                       </div>


                   </div>
               </div>
            </form>
          </div> <!--/.panel-body -->
        </div> <!-- /#addwizard -->
      </div> <!-- /.panel.panel-default -->

因此,每当用户填写表单并点击保存时。 c# 获取文本框的详细信息并更新 db:

//term time address info
        string txttermhouse = ((TextBox)termaddy.FindControl("txttermhousenum")).Text;
        string txttermstr = ((TextBox)termaddy.FindControl("txtxtermstreet")).Text;
        string txttermpostcode = ((TextBox)termaddy.FindControl("txtxtermpost")).Text;

        termaddress(txttermhouse, txttermstr, txttermpostcode);

问题是,当代码通过这部分时,它会将每个文本框视为空白,并将它们作为空字符串保存在数据库中。我也试过直接将文本框引用到方法中,即:

 termaddress(txttermhousenum.Text, txtxtermstreet.Text, txtxtermpost.Text);

但这两种方法似乎都行不通。任何有关如何获取文本框值的帮助将不胜感激,对此问题非常困惑。

直接使用Textbox,无需使用FindControl:

尝试按如下方式访问它们

  string txttermhouse = txttermhousenum.Text;
  string txttermstr = txtxtermstreet.Text;
  string txttermpostcode = txtxtermpost.Text;

您可以按如下方式传递给您的方法:

termaddress(txttermhouse , txttermstr , txttermpostcode );

为什么要在服务器上使用 div 运行 查找控件?如果您仅使用它来查找控件,我建议您从 div 中删除 runat="server"。这会使您的响应变慢。

您可以按如下方式简单地访问它们:

  string txttermhouse = txttermhousenum.Text;
  string txttermstr = txtxtermstreet.Text;
  string txttermpostcode = txtxtermpost.Text;

You really don't need to find controls. You can access them directly. You need to find control when its repeating inside grid column template or within repeater etc., then you need to find control otherwise you can access them directly.

如果您使用母版页,请确保您没有两个 <form runat="server"> 标签。我曾经遇到过这个问题,结果就是这样。