简体   繁体   English

在文件asp.net后面的代码中检索文本框值

[英]Retrieve textbox values in code behind file asp.net

Hi I have a crontrol on the page: 嗨我在页面上有一个crontrol:

<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. 但它不起作用。

If it is an ASP.NET TextBox server control which is inside your form, you can simply use the Text property 如果它是表单内的ASP.NET TextBox server control ,则只需使用Text属性即可

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" 您可以访问代码隐藏内部的任何元素,如果它有一个ID使用值和属性Runat设置属性值"Server"

Ex : You can write some markup like this in your .ASPX page 例如 :您可以在.ASPX页面中编写一些这样的标记

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

and in codebehind 并在代码隐藏

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

You dont need to search it this way TextBox searchTerm = FindControl("q") as TextBox; 你不需要这样搜索TextBox searchTerm = FindControl("q") as TextBox; because it is a server control you will able to get the text by its id 因为它是一个服务器控件,你可以通过它的id获取文本

this way string query = q.Text; 这样string query = q.Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM