2014-11-21 101 views
0

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應用程序。該控件位於MasterPage中。

回答

1

使用此:

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

id屬性是使用客戶端。 x:Name用於服務器端操作

+0

你能提供整個代碼請。謝謝。 – SearchForKnowledge 2014-11-21 17:23:35

+0

我腦凍結了。我只能使用'searchB.Value'; – SearchForKnowledge 2014-11-21 17:25:36

+0

這有道理@SearchForKnowledge? – techvice 2014-11-21 17:25:47

相關問題