user4184963 user4184963 - 6 months ago 47
ASP.NET (C#) Question

C# unable to find content of 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
<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 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 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> <!--/.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.

Answer Source

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.