Question: I am having an issue with validation that is performed on textboxes which have the text set in the code behind file, which is setting the text based on the data in a datatable. The issue is when I load the page and click a button, the validation runs as it should, however fields that have been set with text, fail the validation - which they shouldn't. 

The front end code is:

```
<asp:TextBox ID="tbName" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="tbRequired" EnableClientScript="true" 
        Display="Dynamic" runat="server" ControlToValidate="tbName" ErrorMessage="Please enter a name"></asp:RequiredFieldValidator>

```

The back end code is:

```
 tbName.Text = dtDetails.Rows[0]["Name"].ToString();

```

Any suggestions would be greatly appreciated.

Cheers

For example, when the page loads the textbox is set (from behind code, so not user input), E.g "John" (this text is inside the textbox), when a button on the page is clicked, the error message is displayed E.g "Please enter a name" next to the textbox with John in it.

The required field has text in it, I am unsure why it is displaying the error message. Can it be an issue from setting the textbox text with code behind rather than entering it on the page?

Answer: 
Found out that the issue was with the data that came back from the database had extra blank spaces in it, so I used the Trim() method which fixed it.

Thanks to those who replied.

Cheers
I'd need more information, but could it be that you've forgotten to wrap the setting of the text fields within a if(!Page.IsPostBack) on the Page\_Load()?

e.g.

```
if(!Page.IsPostBack)
{
    tbName.Text = dtDetails.Rows[0]["Name"].ToString();
}

```

Page\_Load() runs on each page load, including PostBacks. If you haven't wrapped setup code in Page\_Load() with the above if statement, then the textbox would get overwritten with the value from the DataTable before calling the OnClick event. If the value in the DataTable was blank (or if the DataTable didn't get reinitialized) then the TextBox would become blank again.

As I said, this is a bit of a guess as there isn't much information about the data or your codebehind code.