locked
TableRows disappear on button click RRS feed

  • Question

  • User-1072848465 posted

    I am creating a table from code behind where the number or rows is determined by the count of a List(); When the user clicks save I am unable to read the tableRows to capture the newly entered text in the textboxes. How can I retain the TableRows on save to read them?

    Assistance is much appreciated.

    var getScouts = (from s in cxt.Scouts
                     select new
                     {
                       scoutId = s.Id,
                       scoutFirstName = s.firstName,
                       scoutLastName = s.lastName,
                       membershipId = s.MembershipID
                     }).ToList();

    The table is prepopulated from the List() and the goal is to allow users to enter the number of items sold in a text field and click Save.

          foreach (var item in getScouts)
          {
            TableRow orderRow = new TableRow();
            OrderFormTable.Rows.Add(orderRow);
            orderRow.ID = "EntryRow" + rowCtr.ToString();
    
            TableCell scoutFirstNameText = new TableCell();
            orderRow.Cells.Add(scoutFirstNameText);
    
            TextBox firstNameTextBox = new TextBox();
            firstNameTextBox.Text = item.scoutFirstName;
            firstNameTextBox.ID = "scoutFirstNameTxtBox" + rowCtr.ToString();
            scoutFirstNameText.Controls.Add(firstNameTextBox);
    
            TableCell scoutLastNameText = new TableCell();
            orderRow.Cells.Add(scoutLastNameText);
    
            TextBox lastNameTextBox = new TextBox();
            lastNameTextBox.Text = item.scoutLastName;
            lastNameTextBox.ID = "scoutLastNameTxtBox" + rowCtr.ToString();
            scoutLastNameText.Controls.Add(lastNameTextBox);
    
            TableCell membershipIdText = new TableCell();
            orderRow.Cells.Add(membershipIdText);
    
            TextBox membershipIdTextBox = new TextBox();
            membershipIdTextBox.Text = item.membershipId.ToString();
            membershipIdTextBox.ID = "membershipIdTxtBox" + rowCtr.ToString();
            membershipIdText.Controls.Add(membershipIdTextBox);
    
            TableCell productQty = new TableCell();
            orderRow.Cells.Add(productQty);
    
            TextBox productQtyTextBox = new TextBox();
            productQtyTextBox.ID = "productQtyTxtBox" + rowCtr.ToString();
            productQty.Controls.Add(productQtyTextBox);
    
            TableCell scoutIdHiddenField = new TableCell();
            orderRow.Cells.Add(scoutIdHiddenField);
    
            HiddenField scoutIdHidden = new HiddenField();
            scoutIdHidden.ID = "scoutIdHidden" + rowCtr.ToString();
            scoutIdHidden.Value = item.scoutId.ToString();
            scoutIdHiddenField.Controls.Add(scoutIdHidden);
    
          }

    When save is clicked, I want to read each TableRow to capture values, but the rows of the table disappear. Please Help!

              foreach (TableCell tc in tr.Controls)
              {
                if (tc.HasControls())
                {
    
                  if (tc.Controls[0] is TextBox)
                  {
                    string strTextBoxText = ((TextBox)tc.Controls[0]).Text;
                    string strTextBoxID = ((TextBox)tc.Controls[0]).ID;
    
                    if (strTextBoxID.Contains("scoutFirstNameTxtBox"))
                    {
                      string strScoutFirstName = strTextBoxText;
                      scoutsFirstName = strScoutFirstName.Trim();
                    }
                    if (strTextBoxID.Contains("scoutLastNameTxtBox"))
                    {
                      string strScoutLastName = strTextBoxText;
                      scoutsLastName = strScoutLastName.Trim();
                    }
                    if (strTextBoxID.Contains("productQtyTxtBox"))
                    {
                      string trimProdQty = strTextBoxText.Trim();
    
                      if (trimProdQty != "")
                      {
                        itemsSold = Convert.ToInt32(strTextBoxText);
                      }
                      else
                      {
                        itemsSold = 0;
                      }
    
                    }
                  }
                  else if (tc.Controls[0] is HiddenField)
                  {
                    string strHiddenField = ((HiddenField)tc.Controls[0]).Value;
                    if (strHiddenField.Contains("scoutIdHidden"))
                    {
                      scoutId = Convert.ToInt32(strHiddenField.Trim());
                    }
                  }
                }
    
              }

    I am saving the table to ViewState so the new table entries are displayed on postback

        protected override object SaveViewState()
        {
          object[] newViewState = new object[2];
    
          List<string> txtValues = new List<string>();
    
          foreach (TableRow row in OrderFormTable.Controls)
          {
            foreach (TableCell cell in row.Controls)
            {
              if (cell.HasControls())
              {
                if (cell.Controls[0] is TextBox)
                {
                  txtValues.Add(((TextBox)cell.Controls[0]).Text);
                }
              }
            }
          }
    
          newViewState[0] = txtValues.ToArray();
          newViewState[1] = base.SaveViewState();
          return newViewState;
        }
    
        protected override void LoadViewState(object savedState)
        {
          //if we can identify the custom view state as defined in the override for SaveViewState
          if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
          {
            object[] newViewState = (object[])savedState;
            string[] txtValues = (string[])(newViewState[0]);
            if (txtValues.Length > 0)
            {
              //re-load tables
              getUnitScouts();
              int i = 0;
              foreach (TableRow row in OrderFormTable.Controls)
              {
                foreach (TableCell cell in row.Controls)
                {
                  if (cell.HasControls())
                  {
                    if (cell.Controls[0] is TextBox && i < txtValues.Length)
                    {
                      ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();
    
                    }
                  }
                }
              }
            }
            //load the ViewState normally
            base.LoadViewState(newViewState[1]);
          }
          else
          {
            base.LoadViewState(savedState);
          }
        }

    Thanks

    Brad

    Saturday, June 11, 2016 12:07 PM

Answers

  • User36583972 posted

    Hi bdassow,

    From your description, I have made a sample on my side. It is working, you can refer the following code.

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Table ID="tbl" runat="server">
                </asp:Table>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem>Select</asp:ListItem>
                    <asp:ListItem>Generate_Table</asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="btnSet" runat="server" Text="Button" OnClick="btnSet_Click" />
            </div>
        </form>
    </body>
    </html>

    ASPX.CS:

      protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList1.SelectedIndex == 1)
                {
                    CreateDynamicTable();
                }
            }
    
            private void CreateDynamicTable()
            {   
                int tblRows = 5;       
                for (int i = 0; i < tblRows; i++)
                {
                    TableRow orderRow = new TableRow();
                    orderRow.ID = "EntryRow" + i.ToString();
    
                    TableCell scoutFirstNameText = new TableCell();       
                    TextBox firstNameTextBox = new TextBox();
                    firstNameTextBox.Text = "scoutFirstName"+ i.ToString();
                    firstNameTextBox.ID = "scoutFirstNameTxtBox" + i.ToString();
                    scoutFirstNameText.Controls.Add(firstNameTextBox);
                    orderRow.Cells.Add(scoutFirstNameText);
    
    
                    TableCell scoutLastNameText = new TableCell();
                    TextBox lastNameTextBox = new TextBox();
                    lastNameTextBox.Text = "scoutLastName"+ i.ToString();
                    lastNameTextBox.ID = "scoutLastNameTxtBox" + i.ToString();
                    scoutLastNameText.Controls.Add(lastNameTextBox);
                    orderRow.Cells.Add(scoutLastNameText);
    
    
                    TableCell membershipIdText = new TableCell();
                    TextBox membershipIdTextBox = new TextBox();
                    membershipIdTextBox.Text = "membershipId"+ i.ToString();
                    membershipIdTextBox.ID = "membershipIdTxtBox" + i.ToString();
                    membershipIdText.Controls.Add(membershipIdTextBox);
                    orderRow.Cells.Add(membershipIdText);
    
    
                    TableCell productQty = new TableCell();   
                    TextBox productQtyTextBox = new TextBox();
                    productQtyTextBox.ID = "productQtyTxtBox" + i.ToString();
                    productQty.Controls.Add(productQtyTextBox);
                    orderRow.Cells.Add(productQty);
    
                    TableCell scoutIdHiddenField = new TableCell();
                    HiddenField scoutIdHidden = new HiddenField();
                    scoutIdHidden.ID = "scoutIdHidden" + i.ToString();
                    scoutIdHidden.Value ="scoutId" + i.ToString(); 
                    scoutIdHiddenField.Controls.Add(scoutIdHidden);
                    orderRow.Cells.Add(scoutIdHiddenField);
    
                    // Add the TableRow to the Table
                    tbl.Rows.Add(orderRow);
                    tbl.EnableViewState = true;
                    ViewState["tbl"] = true;
                }
            }
    
            protected void btnSet_Click(object sender, EventArgs e)
            {
                foreach (TableRow tr in tbl.Controls)
                {
                    foreach (TableCell tc in tr.Controls)
                    {
                        if (tc.Controls[0] is TextBox)
                        {
                            Response.Write(((TextBox)tc.Controls[0]).Text);
                        }
                        else if (tc.Controls[0] is HiddenField)
                        {
                            string strHiddenField = ((HiddenField)tc.Controls[0]).Value;                       
                                Response.Write("HIDEN"+((HiddenField)tc.Controls[0]).Value);                        
                        }
                    }
                    Response.Write("<br/>");
                }
    
            }
    
            protected override object SaveViewState()
            {
                object[] newViewState = new object[2];
    
                List<string> txtValues = new List<string>();
    
                foreach (TableRow row in tbl.Controls)
                {
                    foreach (TableCell cell in row.Controls)
                    {
                        if (cell.Controls[0] is TextBox)
                        {
                            txtValues.Add(((TextBox)cell.Controls[0]).Text);
                        }
                    }
                }
    
                newViewState[0] = txtValues.ToArray();
                newViewState[1] = base.SaveViewState();
                return newViewState;
            }
            protected override void LoadViewState(object savedState)
            {
                //if we can identify the custom view state as defined in the override for SaveViewState
                if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
                {
                    object[] newViewState = (object[])savedState;
                    string[] txtValues = (string[])(newViewState[0]);
                    if (txtValues.Length > 0)
                    {
                        //re-load tables
                        CreateDynamicTable();
                        int i = 0;
                        foreach (TableRow row in tbl.Controls)
                        {
                            foreach (TableCell cell in row.Controls)
                            {
                                if (cell.Controls[0] is TextBox && i < txtValues.Length)
                                {
                                    ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();
    
                                }
                            }
                        }
                    }
                    //load the ViewState normally
                    base.LoadViewState(newViewState[1]);
                }
                else
                {
                    base.LoadViewState(savedState);
                }
            }
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 14, 2016 5:39 AM