473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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 E.L. Mitchell
6707 Daniel Court
Fort Myers, FL 33908
Nov 16 '05 #1
2 2322
"Edward Mitchell" <em*******@nospam.ieee.org> wrote in message news:OY**************@TK2MSFTNGP14.phx.gbl...
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
Nov 16 '05 #2
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
Derek Harmon wrote:
"Edward Mitchell" <em*******@nospam.ieee.org> wrote in message news:OY**************@TK2MSFTNGP14.phx.gbl...
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


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vincent Yang | last post by:
I'm using Access 2002. My database is in Access 2000 format. I'm designing a form to collect responses to a 40-item questionnaire. I can fit four list boxes on a screen, so I plan to have 11 tab...
3
by: Joe via DotNetMonster.com | last post by:
Hi, I have an form that saves to a database. I would like the user to be able to enter HTML tags into the Textbox Control fields but I get an error message when entering HTML tags: A...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
1
by: Dan Manes | last post by:
Wondering what other people do about this issue... You're writing a web app in asp.net that requires user input. Data will be stored in SQL Server Express database. You want to make sure data...
0
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is...
6
by: Dhananjay | last post by:
hi everyone i have a problem how to save the textbox contents to sql server database. i am getting this contents after conversion from web browser into textbox(multiline) I want to store all the...
4
by: Surek | last post by:
I have created a form using the wizard, to display the contents of the table. I need to update the table by entering the data through the form. When i click the update button of my form, it ignores...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.