user4184963
user4184963

Reputation: 79

C# unable to find content of ASP.net Textboxes

Okay so I am trying to create a sort of registration wizard, so there are different sections within the asp such as the one below:

  <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 -->

So whenever the user fills out the form and hits save. The c# gets the details of the text boxes and updates the 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);

The issue is that when the code passes through this portion it sees each of the text boxes as blank and saves them in the db as and empty string. I have also tried directly referencing the textbox into the method, ie :

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

But neither way seems to work. Any help on how to get the text box values would be greatly appreciated, very confused by this issue.

Upvotes: 1

Views: 937

Answers (3)

Harold_Finch
Harold_Finch

Reputation: 722

If you are using a master page, make sure that you do not have two <form runat="server"> tags. I've once experienced this issue and it was a result of that.

Upvotes: 2

Muhammad Qasim
Muhammad Qasim

Reputation: 1722

Why are you finding your controls using your div running at server? I would suggest you to remove runat="server" from your div if you are using it only to find controls. This makes your response slower.

You can simply access them as follows:

  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.

Upvotes: 0

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

As you are using Textbox directly , no need of using of FindControl:

Try accessing them as follows

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

You can pass to your method as follows:

termaddress(txttermhouse , txttermstr , txttermpostcode );

Upvotes: 0

Related Questions