Using the information your (development) environment provides - is allowed!
Huge title while the subject or the example in this case is actually very simple and straightforward. I saw a question on newsgroups which went something like:
- poster states he doesn't know much about control events
- he/she asked if he/she had a method like
protected void tbPreRender(object sender, EventArgs e)
{
tb1.Attributes["value"] = tb1.Text;
tb2.Attributes["value"] = tb2.Text;
}
wired to two separate TextBoxes Prerender event, is it fired twice? He/she hoped that ASP.NET would deal this somehow that it would be called only once, like SQL Server does with subqueries.
First thing to note in this case of course is that ASP.NET cannot do much assumptions, due to its generality, it does what it can in the most common case, and that definately is all. So this logic (UI logic) is up to the page developer right away. Second thing is that SQL Server is totally another beast, important though and lots used with ASP.NET, but it's not really comparable in that sense, that ASP.NET would somehow reuse similar logic. ASP.NET does what's needed to abstract HTTP, and SQL Server does the same for relational database and its additional featureset stuff. Third is that you can write the previous case like:
//Wiring events somewhere
tb1.PreRender += new EventHandler(tbPreRender);
tb2.PreRender += new EventHandler(tbPreRender);
protected void tbPreRender(object sender, EventArgs e)
{
//Use the knowledge that control raising the event is available via
sender argument
TextBox tb=(TextBox)sender;
tb.Attributes["value"] = tb.Text;
}
when you basically take use of the knowledge what the environment does, in this case, how ASP.NET exposes events and what's available via them.
Sounds like I'm patronizing and ditting the i's and crossing the t's, but my point is that this is normal unawareness for newbies , but emphasizes how important it really is that you learn to know the "landscape" of technologies you're working on, what each one of them does and what's each one's responsibility. Kind of think it as understanding your personal development architecture. It doesn't mean you need to know everything (measuring what one does or doesn't need to know is actually a hell of a problem for any developer these days, be it newbie or not) but you should be comfortable with the environment, especially if you earn your living by using it. You can learn while working - work is the best teacher - but you cannot be blind.
In this case, reading a good book, or looking at the documentation or definately asking someone mor eexperienced, of course brings lights on - learning always starts from somewhere, but be prepared that you'll do that rest of your life, at least in this profession. :-)