Retrieve textbox values in code behind file asp.net

11,823

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";
Share:
11,823
stats101
Author by

stats101

Updated on June 04, 2022

Comments

  • stats101
    stats101 almost 2 years

    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.

  • Tim Copenhaver
    Tim Copenhaver almost 12 years
    As a note, you can access any element this way so long as it has an id assigned and the runat attribute set to "server", even if it is not an ASP.Net control.
  • Dushan Perera
    Dushan Perera almost 12 years
    @TimCopenhaver: Agreed. I willmention that in my answer.