2012-10-24 29 views
1

存在我這裏有一個代碼,由輸入框和文本區域,但是當我正要打電話給輸入框,其名稱爲txtBxSearch。發生錯誤,它說: 「 'txtBxSearch' 這個名字不會在目前情況下存在」「txtBxSearch」這個名字不會在目前情況下

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 

<div style="padding-top:10px; padding-left:40px;"> <span class="fields">To:</span><br /> 
    <input type="text" id="txtBxSearch" name="txtBxSearch" class="border fields" 
    style="width:891px;" onclick="return txtBxSearch_onclick()" /> 

<div style="padding-top:10px; padding-left:40px;"><span class="fields">Text Message:</span><br /> 
<textarea id="TextArea1" onkeyup="textCounter(this,'counter',160);" cols="20" rows="2" class="fields border" style="height:150px; width:95%;"></textarea> 

</asp:Content> 

我在C#代碼隱藏

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Data.SqlClient; 
using System.Web.Services; 
using System.Web.UI.WebControls; 

public partial class SMS : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 
private string groupKeyword; 
private string message; 
private int priorityLevel; 
private bool isDiagnosticCommand; 
private bool concatenate; 
private object confirmationDate; 


protected void btnSend_Click(object sender, ImageClickEventArgs e) 
    { 

     groupKeyword = txtBxSearch.value; 
     message = TextArea1.Value; 
     priorityLevel = 253; 
     //confirmationDate = DateTime.Now.ToShortDateString(); 
     isDiagnosticCommand = false; 
     concatenate = false; 


} 
} 
+0

可能重複的問題http://stackoverflow.com/questions/564289/read-post-data-submitted-to-asp-net-form – pmtamal

回答

6

因爲你的控制txtbxSearch是一個HTML控件,它不是一個ASP.Net控件。這就是爲什麼你不能在你的代碼中訪問它。

指定runat="server"屬性與你的文本框,你應該能夠訪問它在後面的代碼。

您也可以嘗試在後面的代碼:

string value = Request.Form["txtbxSearch"]; 

,如果你不希望使用runat="server"與輸入控制。

+0

是否有任何其他方式來訪問我的控制中的HTML在ASP? – ChristineS

+0

但是當我使用RUNAT =「服務器」,它說:「 無法隱式轉換類型‘System.Web.UI.HtmlControls.HtmlInputText’到‘線’」 – ChristineS

+0

@ChristineS,我不知道你是怎麼想訪問控制在代碼後面。它將不能作爲普通的ASP.Net TextBox訪問。嘗試'字符串值=的Request.Form [「txtbxSearch」];'來獲取值 – Habib

0

你的代碼需要:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 

<div style="padding-top:10px; padding-left:40px;"> <span class="fields">To:</span><br /> 
    <input type="text" id="txtBxSearch" name="txtBxSearch" class="border fields" 
    style="width:891px;" onclick="return txtBxSearch_onclick()" runat="server" /> 
<div style="padding-top:10px; padding-left:40px;"><span class="fields">Text Message:</span><br /> 
<textarea id="TextArea1" onkeyup="textCounter(this,'counter',160);" cols="20" rows="2" class="fields border" style="height:150px; width:95%;"></textarea> 
</asp:Content> 

注意在txtBxSearch

0

的RUNAT =服務器如果要訪問它,你只需要中用runat = 「server」 屬性

<div style="padding-top:10px; padding-left:40px;"> <span class="fields">To:</span><br /> 
     <input type="text" id="txtBxSearch" name="txtBxSearch" class="border fields" 
     style="width:891px;" onclick="return txtBxSearch_onclick()" runat="server" /> 
相關問題