473,388 Members | 1,209 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,388 software developers and data experts.

Looping through DataList and reading TextBox values on Button Click

I am developing a web form which is going to be used for manufacturing
input. The form uses a DataList which list a batch of parts that may
range anywhere from 1-9 parts. The DataList contains 6 textboxes
pre-fill with data (reason for using datalist) which will capture user
input and changes to the textboxes. So the 6 textboxes in columns will
be constant but the rows will vary representing each part. I am
looking to loop through each row and grab the values of the 6
textboxes. Without using the Edit commands on the datalist is it
possible to find the textbox values in each row. I want to have the
whole datalist read back into the database on a button click. Is this
possible. Best ways of going about it. Hope this makes sense.

Thanks,

Brett

Nov 19 '05 #1
4 8739
Hi,

yes it is. You can loop through dataList's Items collection and then for
every item, locate the relevant controls.

Dim dlitem As DataListItem
Dim tb1 As TextBox
Dim tb2 As TextBox
....
For Each dlitem in DataList1.Items

tb1=CType(dlitem.FindControl("TextBox1"),TextBox)
tb2=CType(dlitem.FindControl("TextBox2"),TextBox)
...
'here do something with each row

Next
....

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #2
If anyone has a better way than what I show below, I'd really like to know.
I have extensively researched this exact same question and no one has every
suggested anything other than what the code below does. Basically you have
too loop through the Form collection:
//This line is in your Page code-behind
dataClass.LoopThroughFormCollection(Request.Form); // pass Request.Form to a
method that will loop through it.

// This method is in a separate class
public void
LoopThroughFormCollection(System.Collections.Speci alized.NameValueCollection
formCollection) {

int currentControlNumber = 0; // Your TextBox will appear many times -
ASP.NET will number each one uniquely - so you might want to parse this out
string strTemp = "";
string controlName = "";
int delPos = 0; //delimiter position
string controlValue = "";

String[] arrayFormKeys = formCollection.AllKeys;
// Loop through all of the controls in the Form collection
for (loop = 0; loop < arrayFormKeys.Length; loop++) {

// Parse out the control name and number from the form KEY value, and then
get the value associated with that KEY.
// This is an example of what we're parsing from the form KEY value:
"MyDataListName:_ctl1:MyTextBox"
if (arrayFormKeys[loop].StartsWith("MyDataListName")) {
strTemp = arrayFormKeys[loop].Replace("MyDataListName:_ctl", "");
delPos = strTemp.IndexOf(":", 0); // delimiter position
currentControlNumber = Convert.ToInt32(strTemp.Substring(0, delPos));
controlName = strTemp.Substring(delPos + 1, (strTemp.Length - (delPos +
1)));
controlValue = formCollection[arrayFormKeys[loop]];
// At this point you have everything you need to know about the current
row in your DataList that was returned via the Form collection

if (controlName == "MyTextBox") {
// Do something here; write textbox value to db or whatever.
}
}
}
}

-HTH

<bw****@lbrspec.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I am developing a web form which is going to be used for manufacturing
input. The form uses a DataList which list a batch of parts that may
range anywhere from 1-9 parts. The DataList contains 6 textboxes
pre-fill with data (reason for using datalist) which will capture user
input and changes to the textboxes. So the 6 textboxes in columns will
be constant but the rows will vary representing each part. I am
looking to loop through each row and grab the values of the 6
textboxes. Without using the Edit commands on the datalist is it
possible to find the textbox values in each row. I want to have the
whole datalist read back into the database on a button click. Is this
possible. Best ways of going about it. Hope this makes sense.

Thanks,

Brett

Nov 19 '05 #3
I wish I had discovered that a year ago when I was just getting started.
Thanks! My Form looping logic (in my response to the OP) can be disregarded
(and I'll be doing some refactoring this weekend!).


"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:ez*************@TK2MSFTNGP12.phx.gbl...
Hi,

yes it is. You can loop through dataList's Items collection and then for
every item, locate the relevant controls.

Dim dlitem As DataListItem
Dim tb1 As TextBox
Dim tb2 As TextBox
...
For Each dlitem in DataList1.Items

tb1=CType(dlitem.FindControl("TextBox1"),TextBox)
tb2=CType(dlitem.FindControl("TextBox2"),TextBox)
...
'here do something with each row

Next
...

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #4
The "pattern" is quite similar for all databound controls like Repeater,
DataList and DataGrid. You just loop through the Items collection. Type of
the item is respectively RepeaterItem, DataListItem and DataGridItem. Then
again one can run FindControl etc against it.

Nice to know that my response helped.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Nov 19 '05 #5

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

Similar topics

0
by: Alex | last post by:
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Configuration Public Class Main : Inherits Page ...
8
by: MattB | last post by:
I think I'm close to making this work, but so far it does not. I have a datalist (id = "dlVFields") on a web form that has an item template with a textbox in it. The textbox id = "txtVerify"....
3
by: Chris | last post by:
Hi Folks, well I have this "small" problem with the footertemplate in a datalist. I added a button and a textbox to update the db with the given String of the Textbox. I already searched for...
10
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I...
2
by: Chris Fink | last post by:
This should be relatively simple but I am unable to find an asp:button tag in a datalist footer. I have tried it numerous ways including the FindControl method from the many events that the...
2
by: Roman | last post by:
I have built a simple user control that contains 2 buttons, a text box and a dropdownlist. When a button is clicked it sets the visible property of the textbox/dropdownlist and the button. ie the...
1
by: Gary Frank | last post by:
I added a couple of labels and a button to a Datalist control called "dlSignIn" by adding them to the Item template. The labels are bound to a dataset column. Program code gets executed in the...
3
by: Mirek Endys | last post by:
I have DataList as part of DataList item. DataList in DataList. The parent DataList working well including Edit command, that shows Edit template and correctly bind the data into edit template...
0
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I am having this problem. I have this datalist and whenever I expand the datalist by the plus button, it overlaps other controls on my web page and sometimes these controls come...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.