Deviney
Deviney

Reputation: 131

ASP.Net: Textbox change giving errors

I am trying to run a function when a user has finished entering text in the text-box field.

But when I try to compile the code I get this error

Error:

Compiler Error Message: CS1061: 'ASP.account_default_testingpage_aspx'
does not contain a definition for 'GetAverageRent_TextChanged' and no
extension method 'GetAverageRent_TextChanged' accepting a first
argument of type 'ASP.account_default_testingpage_aspx' could be found
(are you missing a using directive or an assembly reference?)

any idea how to fix this?

My markup is

 <asp:TextBox ID="PostcodeTxb" runat="server" OnTextChanged="GetAverageRent_TextChanged" AutoPostBack="true">EnterPostcode</asp:TextBox>

My c# is

private void GetAverageRent_TextChanged(object sender, EventArgs e)
{
   TextBox PostcodeTxb = sender as TextBox;
   if (PostcodeTxb != null)
   {
       string theText = PostcodeTxb.Text;
   }
}

Upvotes: 0

Views: 1805

Answers (3)

anbuj
anbuj

Reputation: 519

It may be because you've changed the textbox name from GetAverageRent to PostcodeTxb after adding the method. Go to youraspfile.aspx.designer.cs where your TextBox PostcodeTxb defined and add GetAverageRent_TextChanged event. It is recommended to use the same controlname_eventname for the event of the control to avoid confusions. For example, your event name must be PostcodeTxb_TextChanged instead of GetAverageRent_TextChanged.

PostcodeTxb.TextChanged +=new EventHandler(PostcodeTxb_TextChanged); has to be changed as PostcodeTxb.TextChanged +=new EventHandler(GetAverageRent_TextChanged); if you want to keep the current naming.

Upvotes: 0

freefaller
freefaller

Reputation: 19953

The problem is that your event handler is private.

If you're defining the event handler in the mark-up, it must be protected or public. Normally you would use protected.

Change...

private void GetAverageRent_TextChanged(object sender, EventArgs e)

To...

protected void GetAverageRent_TextChanged(object sender, EventArgs e)

Upvotes: 3

Hardik Vinzava
Hardik Vinzava

Reputation: 988

your textbox id and code-behind method is mismatched So your code is wrong,it may be like this:-

my markup is

<asp:TextBox ID="GetAverageRent" runat="server" OnTextChanged="GetAverageRent_TextChanged" AutoPostBack="true"></asp:TextBox>

C# code is

private void GetAverageRent_TextChanged(object sender, EventArgs e)
        {
            TextBox PostcodeTxb = sender as TextBox;
            if (PostcodeTxb != null)
            {
                string theText = PostcodeTxb.Text;
            }
        }

Upvotes: 0

Related Questions