0

I am creating a button in the code behind of my aspx.cs page. However I need to detect within this textbox when any text is changed within it after it has been created.

                        TextBox addn = new TextBox
                    {
                        ID = "Tb_Address" + i,
                        CssClass = "TextBoxProfile",
                        OnTextChanged = "textChangedEventHandler",
                        Text = "testc"
                    };
                    Tb_Container.Controls.Add(addn);

However OnTextChanged gives me this error:

'System.Web.UI.WebControls.TextBox.OnTextChanged(System.EventArgs)' is inaccessible due to its protection level 

I have read around these issues; but I don't have a designer page to change the protection level, neither do I think that would work as these controls are created programmatically.

Edit:

I had tried:

addn.OnTextChanged += textChangedEventHandler;

However this still gives me the above error.

addn.TextChanged += textChangedEventHandler;

The above works, but it's not the desired output, as I wish for this to be automatic and not on PageLoad(?).

  • Can you clarify more? When you are creating textbox programmatically you have to bind each textbox with event in the code. – Hassan Jun 18 '14 at 11:07
  • I haven't binded the textbox with an event, I didn't know to do that. Do you have any good examples you could link me to? –  Jun 18 '14 at 11:47

4 Answers4

0

You are subscribing to the text-changed event in way you do it in the ascx file. Since you creating the controls programmatically, you should subscribe to the event like this:

TextBox addn = new TextBox {
    ID = "Tb_Address" + i,
    CssClass = "TextBoxProfile",
    Text = "testc"
};
addn.OnTextChanged += textChangedEventHandler;
Tb_Container.Controls.Add(addn);
Maarten
  • 22,527
  • 3
  • 47
  • 68
  • I had done this previously too (sorry), but again I am unable to do this due to the Controls protection level? –  Jun 18 '14 at 10:44
0

you can directly attach event as shown below and take an event handler... and take a look at this answer Dynamically create an ImageButton :-

TextBox addn = new TextBox ;

addn.TextChanged += new EventHandler(b_textChanged);

and its handler :-

void b_textChanged(object sender, EventArgs e)
    {
        ////Your code
    }
Community
  • 1
  • 1
Neel
  • 11,625
  • 3
  • 43
  • 61
0

addn.TextChanged += textChangedEventHandler;
The above works, but it's not the desired output, as I wish for this to be automatic and not on PageLoad(?).

Do you mean that the text change should be detected as you are typing in the values? because if you mean that then JavaScript is what you need.

Netnetter
  • 41
  • 1
  • 9
0

OnTextChanged is a method, not an event, you can't subscribe to this, it is the method that raises the event. It is protected so that if you derive from the Textbox class, you can override with your own behaviour. See OnTextChanged method.

TextChanged is the actual event itself that you need to subscribe to, you had done this correctly, but I think you expected it to behave differently? How did you expect it to behave? Netnetter might be on the right lines of using javascript. See TextChanged event.

addn.TextChanged += textChangedEventHandler; The above works, but it's not the desired output, as I wish for this to be automatic and not on PageLoad(?).

Patrick Allwood
  • 1,822
  • 17
  • 21
  • This pretty much sums up my issues and my misunderstanding. I assumed that on changing the text (much like the Regular expression validator) it would be an auto thing; but it's not which is my bad. –  Jun 19 '14 at 09:07