How to Move Many Web Form TextBox Strings into/out of a Database

  • Thread starter Thread starter Edward Mitchell
  • Start date Start date
E

Edward Mitchell

I have Web Form (.aspx file) with a fair number of textboxes (<asp:textbox
.... />) that I need to fill from a database and then save back to the
database when I'm done. This presumably must be a fairly common problem.
If I use brute force, I finish up with a lot of assignments in the
Page_Load(...) routine and the same number backwards in the OnClick handler
for the submit.

In C++ I would create an single array of pointers to the text box objects
and then have two loops using the textbox name to transfer the column info
into or out of the database.

Is there a clever way in C# to do the equivalent?

If I could get hold of all the controls in the page, it seems like I could
do something like:

String[] textBoxNames = { "T1", "T2", "T3" ...};
DataRow dataRow = dataSet.Tables["MyTable"].Rows[iSelectedRow];
....
foreach(String textBoxID in textBoxNames) {
TextBox tb = SomeSortofCollection.FindControl(textBoxID);

// when we fill the text box from the data store
tb.Text = dataRow[textBoxID];
}

and when the data is to be stored away, a similar loop, but with the last
line changed.

dataRow[textBoxID] = tb.Text;

I don't know what to put in for the collection in which I should be looking
to find the control!

Can anyone suggest an example that shows how to do this sort of thing.

Ed
 
Edward Mitchell said:
I have Web Form (.aspx file) with a fair number of textboxes (<asp:textbox
... />) that I need to fill from a database and then save back : :
foreach(String textBoxID in textBoxNames) {
TextBox tb = SomeSortofCollection.FindControl(textBoxID); : :
I don't know what to put in for the collection in which I should be looking
to find the control!

Your WebForm class inherits from Page, which is a Control, which has a virtual
FindControl( string) method so you're looking for something like this,

TextBox tb = this.FindControl( textBoxID) as TextBox;
if ( null != tb )
// Do Something


Derek Harmon
 
Derek,

Thanks for the feedback. It's really neat when one can look for the
named control in the control collection. Since I have both regular
textboxes and a custom control textbox (PromptedText), I was able to do
the following:

static String[] controlNames = {
"FirstName",
"LastName",
"Address1",
....
};
....
foreach(String controlName in controlNames) {
Control ctrl = this.FindControl(controlName);
if(ctrl is TextBox) {
TextBox tb = ctrl as TextBox;
drBoat[controlName] = tb.Text;
break;
}
if(ctrl is PromptedText) {
PromptedText pt = ctrl as PromptedText;
drBoat[controlName] = pt.Text;
break;
}
}

and then repeat the loop the other way round when I wanted to send the
info back to the database.

I'm not sure whether this is better than databinding each textbox
control. From what I understand, data binding is really a one way
affair to transfer data from the database TO the control and doesn't
work as easily the other way round.

Regards,

Ed
 
Back
Top