Reputation: 196
I am having a problem with having the eventhandler textchanged method to work with an array of textboxes. The textboxes are generated through C# and not through ASP.NET.
here is the TextBox code in C#:
int i = 1;
foreach(string a in data)
{
i++;
TextBox text = new TextBox();
text.TextChanged += new EventHandler(updateone);
text.AutoPostBack = true;
text.ID = Convert.ToString(i);
}
I tried out the Text.AutoPostBack false and true and I had the same result. The updateone method is not even touched when I change the text of the textbox. When I do change the text of the textbox it does update the website, but again the updateone method is not even touched in the code. Here is the updateone code:
protected void updateone(object sender, EventArgs e)
{
TextBox text = (TextBox)sender;
}
I thank everyone for their help! I am just confused why this is not working... and also I have to use the C# method and not the ASP.NET way.
Upvotes: 0
Views: 1852
Reputation: 22945
You are dynamically creating ASP.NET controls. This means that they will not be automatically re-created on the postback of the form. Also, the controls have to be created in the Page_Init event, not the Page_Load event.
So the question is, where and when are you creating the textboxes. Make sure they are created at the Page_Init stage, and you are creating them in the request and in the postback.
Upvotes: 0
Reputation: 198
have you tried storing references to your TextBoxes in an instance member so that they don't get garbage collected?
something like:
List<TextBox> textBoxes = ...
//in a loop
text.ID = Convert.ToString(i);
textBoxes.Add(text);
-- edit
also, as a rule of thumb, put as much logging in your application as possible.
Whether it is NLog
, any other logging tool or even a simple Console.WriteLine()
, seeing what your code is actually doing is very helpful.
Upvotes: 1
Reputation: 226
Since you've confirmed that you're posting your actual code and don't seem to get what I'm trying to say, let me try to explain in an answer.
First problem: Your array of TextBox
es does not exist, which the other answer has already addressed and you've apparently fixed but haven't updated the code in your question to show your fix.
Second problem: The TextBox
es you create are not being added to your form in any way. I'm not sure how you're testing your event handler without doing that.
Third problem: Your event handler updateone
doesn't do anything. Imagine you walk into a grocery store, pick up an orange, put it back down, and then leave. That's what your event handler is doing. Instead of just instantiating a temporary TextBox
and then doing nothing, try making a message box pop up, or changing the text of another control that exists on the form.
Maybe something like this will work:
List<TextBox> textboxes = new List<TextBox>();
int i=1;
foreach(string a in data) // I assume data is a list or array of strings
{
// I'm not sure why you iterate over data if you don't use it at all inside the loop...
++i;
TextBox text = new TextBox();
text.TextChanged += new EventHandler(updateone);
text.AutoPostBack = true;
text.ID = Convert.ToString(i);
// Add the TextBox to form here, not sure what the call is
}
Label info = new Label;
Label.Text = "Hello!";
// Add Label to form here, again not sure what the call is
And then your event handler:
protected void updateone(object sender, EventArgs e)
{
info.Text = ((TextBox)sender).Text;
}
Upvotes: 0