Looping through DataList and reading TextBox values on Button Click

Discussion in 'ASP .Net' started by bwalke, Jun 17, 2005.

  1. bwalke

    bwalke Guest

    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.


    bwalke, Jun 17, 2005
    1. Advertisements

  2. bwalke

    Teemu Keiski Guest


    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

    'here do something with each row

    Teemu Keiski, Jun 17, 2005
    1. Advertisements

  3. bwalke

    Jeremy S Guest

    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
    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:
    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 +
    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.

    Jeremy S, Jun 17, 2005
  4. bwalke

    Jeremy S Guest

    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!).
    Jeremy S, Jun 17, 2005
  5. bwalke

    Teemu Keiski Guest

    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, Jun 20, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.