如何在代码隐藏中获取文本框的值

本文关键字:取文本 获取 代码 隐藏 | 更新日期: 2023-09-27 18:31:20

HTML:

<input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />

C#:

string strSMain;
protected void Page_Load(object sender, EventArgs e)
{
    tbSearchMain = (System.Web.UI.WebControls.TextBox)sender;
    strSMail = tbSearchMain.text; // gives me the following error: Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.site_master' to type 'System.Web.UI.WebControls.TextBox'.
    strSMain = searchB.text; //.Text is not an option for me
}

请帮我解决问题。

我正在创建一个 Web 应用程序。控件位于母版页中。

如何在代码隐藏中获取文本框的值

使用这个:

<input id="searchB" x:Name="searchB" type="text" runat="server" value="" placeholder="Search" class="styledTB searchB floatLeft" />

id 属性用于客户端。 x:Name用于服务器端操作

我不确定,但没有文本框会成为Page_Load()函数的发送者。

检查有关数据上下文属性及其工作原理的信息。

http://www.wpf-tutorial.com/data-binding/using-the-datacontext/