stats101
stats101

Reputation: 1877

Retrieve textbox values in code behind file asp.net

Hi I have a crontrol on the page:

<asp:TextBox ID="q" runat="server" autocomplete="off" />

which I'm trying to access the value of from the code behind file.

I've tried:

TextBox searchTerm = FindControl("q") as TextBox;

But it doesn't work.

Upvotes: 2

Views: 12044

Answers (2)

HatSoft
HatSoft

Reputation: 11201

You dont need to search it this way TextBox searchTerm = FindControl("q") as TextBox; because it is a server control you will able to get the text by its id

this way string query = q.Text;

Upvotes: 0

Shyju
Shyju

Reputation: 218852

If it is an ASP.NET TextBox server control which is inside your form, you can simply use the Text property

string searchKey=q.Text;

You can access any elements inside your CodeBehind if it has an ID property with a value and Runat Property value set to "Server"

Ex : You can write some markup like this in your .ASPX page

<div id="someInfoDiv" runat="server"> Tim's point is valid</div>

and in codebehind

 someInfoDiv.InnerHtml = "So i am adding that";

Upvotes: 8

Related Questions