1

I have a code here that consist of input box and a text area, but when I'm about to call the input box which name is txtBxSearch. An error occurred, It says "The name 'txtBxSearch' does not exist in the current context"

<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>

My code behind in 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;


}
 }
ChristineS
  • 503
  • 1
  • 6
  • 12
  • possible duplicate question http://stackoverflow.com/questions/564289/read-post-data-submitted-to-asp-net-form – pmtamal Oct 24 '12 at 04:53

4 Answers4

6

Because your control txtbxSearch is an HTML control, its not an ASP.Net control. That is why you can't access it in your code behind.

Specify runat="server" attribute with your textbox, and you should be able to access it in code behind.

You can also try in code behind:

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

if you don't want to use runat="server" with the input control.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Is there any other way to access my control in html in asp? – ChristineS Oct 24 '12 at 04:47
  • but when I use runat="server" it says : " Cannot implicitly convert type 'System.Web.UI.HtmlControls.HtmlInputText' to 'string'" – ChristineS Oct 24 '12 at 04:57
  • @ChristineS, I am not sure how you are trying to access that control in code behind. It will not be accessible as a normal ASP.Net TextBox. Try `string value = Request.Form["txtbxSearch"];` to get the value – Habib Oct 24 '12 at 04:59
  • and now there is a new error "An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'" – ChristineS Oct 24 '12 at 05:05
  • @ChristineS, I just test it with your code and it seems to be working. You may place your C# code behind code where you are accessing it in your question, and I would be able to see if there is something else going wrong. – Habib Oct 24 '12 at 05:23
0

your code needs to be:

<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>

Note the runat=server in the txtBxSearch

stack72
  • 8,198
  • 1
  • 31
  • 35
0

If you want to access it you just need a runat="server" attribute

<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" />
awright18
  • 2,255
  • 2
  • 23
  • 22
0

Snap Shot from visual studio

In the visual studio change the value of GenerateMember to True . Then you can the get the text field name. It worked for me.

ranojan
  • 819
  • 8
  • 11