Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am unable to get the values from textbox which are placed is dynamic table.

Following is the code of the button which generates the number of fields to be asked by getting number from textbox :
     protected void btngenerate_Click(object sender, EventArgs e)
     {
         Table tbl = new Table();
         //btngenerate.Enabled = false;
         btnaddquantity.Enabled = true;
         for (int i = 1; i <= int.Parse( txtQuantity.Text); i++)
         {
             TextBox txtEngno = new TextBox();
             TextBox txtChassis = new TextBox();
             TextBox txtColour = new TextBox();
             TextBox txtInvest = new TextBox();
             TextBox txtFrieght = new TextBox();
             TextBox txtYear = new TextBox();
             Label lblEngno = new Label();
             Label lblChassis = new Label();
             Label lblColour = new Label();
             Label lblInvest = new Label();
             Label lblFrieght = new Label();
             Label lblYear = new Label();

             txtYear.ID = "txtYear-" + i;
             txtEngno.ID = "txtEngno-" + i;
             txtChassis.ID = "txtChassis-" + i;
             txtColour.ID = "txtColour-" + i;
             txtFrieght.ID = "txtFrieght-" + i;
             txtInvest.ID = "txtInvest-" + i;


             lblEngno.ID= "lblEngno-"+i;
             lblChassis.ID = "lblChassis-" + i;
             lblColour.ID = "lblColor-" + i;
             lblYear.ID = "lblYear" + i;
             lblInvest.ID = "lblInvest" + i;
             lblFrieght.ID = "lblFrieght" + i;


             lblEngno.Text="Engine No. :";
             lblYear.Text = "Year :";
             lblInvest.Text = "Investment Price :";
             lblFrieght.Text = "Frieght Price :";
             lblChassis.Text="Chassis No. :";
             lblColour.Text = "Colour :";



             TableRow row = new TableRow();
             TableRow row2 = new TableRow();

             TableCell cell11 = new TableCell();
             TableCell cell12 = new TableCell();
             TableCell cell13 = new TableCell();
             TableCell cell14 = new TableCell();
             TableCell cell15 = new TableCell();
             TableCell cell16 = new TableCell();
             TableCell cell21 = new TableCell();
             TableCell cell22 = new TableCell();
             TableCell cell23 = new TableCell();
             TableCell cell24 = new TableCell();
             TableCell cell25 = new TableCell();
             TableCell cell26 = new TableCell();


             cell11.Controls.Add(lblChassis);
             cell12.Controls.Add(txtChassis);
             cell13.Controls.Add(lblEngno);
             cell14.Controls.Add(txtEngno);
             cell15.Controls.Add(lblColour);
             cell16.Controls.Add(txtColour);

             cell21.Controls.Add(lblInvest);
             cell22.Controls.Add(txtInvest);
             cell23.Controls.Add(lblFrieght);
             cell24.Controls.Add(txtFrieght);
             cell25.Controls.Add(lblYear);
             cell26.Controls.Add(txtYear);

             row.Cells.Add(cell11);
             row.Cells.Add(cell12);
             row.Cells.Add(cell13);
             row.Cells.Add(cell14);
             row.Cells.Add(cell15);
             row.Cells.Add(cell16);

             row2.Cells.Add(cell21);
             row2.Cells.Add(cell22);
             row2.Cells.Add(cell23);
             row2.Cells.Add(cell24);
             row2.Cells.Add(cell25);
             row2.Cells.Add(cell26);
             row.BackColor = System.Drawing.Color.LightGray;
             row2.BackColor = System.Drawing.Color.LightGray;
             tbl.Rows.Add(row);

             tbl.Rows.Add(row2);
             TableRow row3 = new TableRow();
             row3.Cells.Add(new TableCell());
             tbl.Rows.Add(row3);

         }
         PlaceHolderTbl.Controls.Add(tbl);
        Session["atbl"] = tbl;
}

After generating the texboxes and rows , user when enter the information. Following button gets the texts from textbox in order to perform task. HERE IM UNABLE TO GET THE VALUES OF ALL THE TEXBOXES

Following is the button which gets the values :
protected void btnaddquantity_Click(object sender, EventArgs e)
        {

            List<VehicleDetails> lst = new List<VehicleDetails>();
            Vehicle car = new Vehicle ();
          car.company = lstCompany.SelectedItem.Text.ToString();
            car.model = lstModel.SelectedItem.Text.ToString();
             try
            {

                Table tbl = Session["atbl"] as Table;
                Session.Remove("atbl");
                int rows = tbl.Rows.Count;
                 if (rows>0)
                {
                for (int i = 1; i <= int.Parse(txtQuantity.Text.ToString()); i++)
                {
                VehicleDetails a = new VehicleDetails();

                a.chassis = (tbl.FindControl("txtChassis-" + i) as TextBox).Text.ToString();
                a.engine = (tbl.FindControl("txtEngno-" + i) as TextBox).Text.ToString();
                a.colour = (tbl.FindControl("txtColour-" + i) as TextBox).Text.ToString();
                a.frieghtprice = int.Parse((tbl.FindControl("txtFrieght-" + i) as TextBox).Text.ToString());
                a.investprice = int.Parse((tbl.FindControl("txtInvest-" + i) as TextBox).Text.ToString());
                a.year = (tbl.FindControl("txtYear-" + i) as TextBox).ToString();
                lst.Add(a);
                }
                 lst.TrimExcess();
            }
            
           
                        garage grg = new garage();
                        grg.AddQuantity(lst, car,int.Parse( txtQuantity.Text.ToString()));
            }
            catch (Exception ex)
            {
                
                lblStatus.Text = ex.Message;
            }
            

        }
Posted

1 solution

Hi ,
Check this
Well this example With Master page Scenario for this example in textbox enter how many of your controls you want to create then Click button1 Then Enter The values you want in Controls created Press Button2 it will retrieved the value you have Enterd before 

C#
protected void Page_Load(object sender, EventArgs e)
  {

  }
  TextBox txtEngno;
  TextBox txtChassis;
  TextBox txtColour;
  TextBox txtInvest;
  TextBox txtFrieght;
  TextBox txtYear;
  Label lblEngno   ;
  Label lblChassis ;
  Label lblColour  ;
  Label lblInvest   ;
  Label lblFrieght ;
  Label lblYear;
  TableRow row   ;
  TableRow row2;

  TableCell cell11;
  TableCell cell12 ;
  TableCell cell13 ;
  TableCell cell14;
  TableCell cell15;
  TableCell cell16;
  TableCell cell21;
  TableCell cell22;
  TableCell cell23;
  TableCell cell24;
  TableCell cell25;
  TableCell cell26;
  TableRow row3;
  Table tbl = new Table();

  protected void Button1_Click(object sender, EventArgs e)
  {
      for (int i = 1; i <= int.Parse(txtQuantity.Text); i++)
      {

          txtEngno = new TextBox();
          txtChassis = new TextBox();
          txtColour = new TextBox();
          txtInvest = new TextBox();
          txtFrieght = new TextBox();
          txtYear = new TextBox();

          lblEngno = new Label();
          lblChassis = new Label();
          lblColour = new Label();
          lblInvest = new Label();
          lblFrieght = new Label();
          lblYear = new Label();

          row = new TableRow();
          row2 = new TableRow();
          cell11 = new TableCell();
          cell12 = new TableCell();
          cell13 = new TableCell();
          cell14 = new TableCell();
          cell15 = new TableCell();
          cell16 = new TableCell();
          cell21 = new TableCell();
          cell22 = new TableCell();
          cell23 = new TableCell();
          cell24 = new TableCell();
          cell25 = new TableCell();
          cell26 = new TableCell();
          row3 = new TableRow();

          txtYear.ID = "txtYear-" + i;
          txtEngno.ID = "txtEngno-" + i;
          txtChassis.ID = "txtChassis-" + i;
          txtColour.ID = "txtColour-" + i;
          txtFrieght.ID = "txtFrieght-" + i;
          txtInvest.ID = "txtInvest-" + i;


          lblEngno.ID = "lblEngno-" + i;
          lblChassis.ID = "lblChassis-" + i;
          lblColour.ID = "lblColor-" + i;
          lblYear.ID = "lblYear" + i;
          lblInvest.ID = "lblInvest" + i;
          lblFrieght.ID = "lblFrieght" + i;


          lblEngno.Text = "Engine No. :";
          lblYear.Text = "Year :";
          lblInvest.Text = "Investment Price :";
          lblFrieght.Text = "Frieght Price :";
          lblChassis.Text = "Chassis No. :";
          lblColour.Text = "Colour :";

          cell11.Controls.Add(lblChassis);
          cell12.Controls.Add(txtChassis);
          cell13.Controls.Add(lblEngno);
          cell14.Controls.Add(txtEngno);
          cell15.Controls.Add(lblColour);
          cell16.Controls.Add(txtColour);

          cell21.Controls.Add(lblInvest);
          cell22.Controls.Add(txtInvest);
          cell23.Controls.Add(lblFrieght);
          cell24.Controls.Add(txtFrieght);
          cell25.Controls.Add(lblYear);
          cell26.Controls.Add(txtYear);

          row.Cells.Add(cell11);
          row.Cells.Add(cell12);
          row.Cells.Add(cell13);
          row.Cells.Add(cell14);
          row.Cells.Add(cell15);
          row.Cells.Add(cell16);

          row2.Cells.Add(cell21);
          row2.Cells.Add(cell22);
          row2.Cells.Add(cell23);
          row2.Cells.Add(cell24);
          row2.Cells.Add(cell25);
          row2.Cells.Add(cell26);
          row.BackColor = System.Drawing.Color.LightGray;
          row2.BackColor = System.Drawing.Color.LightGray;
          tbl.Rows.Add(row);

          tbl.Rows.Add(row2);
          row3.Cells.Add(new TableCell());
          tbl.Rows.Add(row3);
      }
      ContentPlaceHolder cont = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
      cont.Controls.Add(tbl);
      Session["atbl"] = tbl;
      ViewState.Add("countTimes", txtQuantity.Text);


  }

  protected void Button2_Click(object sender, EventArgs e)
  {
      tbl = new Table();
      tbl = (Table)Session["atbl"];
      ContentPlaceHolder cont = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
      cont.Controls.Add(tbl);
      string storeToDbValueOne = "";

      foreach (TableRow tr in tbl.Rows)
      {
          foreach (TableCell cl in tr.Cells)
          {
              foreach (Control ctrl in cl.Controls)
              {
                  if (ctrl is TextBox)
                  {
                      storeToDbValueOne += "  , " + Request.Form[((TextBox)ctrl).UniqueID];
                  }
              }
          }
      }
      Response.Write("<script>alert('" + storeToDbValueOne + "')</script>");

  }

XML
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

        <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

</asp:Content>


Best Regards
M.Mitwalli
 
Share this answer
 
v4
Comments
[no name] 9-Sep-12 9:13am    
Hi i tried using request.form["id"]
but im getting it as null because
to got id txtpower im getting this is request.form as "ctl00$ContentBody$txtPower"
Mohamed Mitwalli 9-Sep-12 9:21am    
Hi ,
Did you follow the same example i have provide it to you ?
Mohamed Mitwalli 11-Sep-12 8:49am    
Check it i already update the solution .Let me know how's it going ?
[no name] 9-Sep-12 9:23am    
yes , following is the code :
protected void btngenerate_Click(object sender, EventArgs e)
{
Table tbl = new Table();
//btngenerate.Enabled = false;
btnaddquantity.Enabled = true;
try
{
ViewState["quantity"] = int.Parse(txtQuantity.Text.ToString());
}
catch (Exception ex)
{

lblStatus.Text = ex.Message;
}

for (int i = 1; i <= int.Parse( txtQuantity.Text); i++)
{
TextBox txtEngno = new TextBox();
TextBox txtChassis = new TextBox();
TextBox txtColour = new TextBox();
TextBox txtInvest = new TextBox();
TextBox txtFrieght = new TextBox();
TextBox txtYear = new TextBox();
Label lblEngno = new Label();
Label lblChassis = new Label();
Label lblColour = new Label();
Label lblInvest = new Label();
Label lblFrieght = new Label();
Label lblYear = new Label();

txtYear.ID = "txtYear" + i;
txtEngno.ID = "txtEngno" + i;
txtChassis.ID = "txtChassis" + i;
txtColour.ID = "txtColour" + i;
txtFrieght.ID = "txtFrieght" + i;
txtInvest.ID = "txtInvest" + i;
txtYear.ClientIDMode=ClientIDMode.Static;
//txtEngno.ClientIDMode = txtChassis.ClientIDMode = txtFrieght.ClientIDMode = txtColour.ClientIDMode = txtInvest.ClientIDMode =

lblEngno.ID= "lblEngno-"+i;
lblChassis.ID = "lblChassis-" + i;
lblColour.ID = "lblColor-" + i;
lblYear.ID = "lblYear" + i;
lblInvest.ID = "lblInvest" + i;
lblFrieght.ID = "lblFrieght" + i;


lblEngno.Text="Engine No. :";
lblYear.Text = "Year :";
lblInvest.Text = "Investment Price :";
lblFrieght.Text = "Frieght Price :";
lblChassis.Text="Chassis No. :";
lblColour.Text = "Colour :";



TableRow row = new TableRow();
TableRow row2 = new TableRow();

TableCell cell11 = new TableCell();
TableCell cell12 = new TableCell();
TableCell cell13 = new TableCell();
TableCell cell14 = new TableCell();
TableCell cell15 = new TableCell();
TableCell cell16 = new TableCell();
TableCell cell21 = new TableCell();
TableCell cell22 = new TableCell();
TableCell cell23 = new TableCell();
TableCell cell24 = new TableCell();
TableCell cell25 = new TableCell();
TableCell cell26 = new TableCell();


cell11.Controls.Add(lblChassis);
cell12.Controls.Add(txtChassis);
cell13.Controls.Add(lblEngno);
cell14.Controls.Add(txtEngno);
cell15.Controls.Add(lblColour);
cell16.Controls.Add(txtColour);

cell21.Controls.Add(lblInvest);
cell22.Controls.Add(txtInvest);
cell23.Controls.Add(lblFrieght);
cell24.Controls.Add(txtFrieght);
cell25.Controls.Add(lblYear);
cell26.Controls.Add(txtYear);

row.Cells.Add(cell11);
row.Cells.Add(cell12);
row.Cells.Add(cell13);
row.Cells.Add(cell14);
row.Cells.Add(cell15);
row.Cells.Add(cell16);

row2.Cells.Add(cell21);
row2.Cells.Add(cell22);
row2.Cells.Add(cell23);
row2.Cells.Add(cell24);
row2.Cells.Add(cell2
[no name] 9-Sep-12 9:23am    
row2.Cells.Add(cell21);
row2.Cells.Add(cell22);
row2.Cells.Add(cell23);
row2.Cells.Add(cell24);
row2.Cells.Add(cell25);
row2.Cells.Add(cell26);
row.BackColor = System.Drawing.Color.LightGray;
row2.BackColor = System.Drawing.Color.LightGray;
tbl.Rows.Add(row);

tbl.Rows.Add(row2);
TableRow row3 = new TableRow();
row3.Cells.Add(new TableCell());
tbl.Rows.Add(row3);

}
PlaceHolderTbl.Controls.Add(tbl);
//tbl.EnableViewState = true;
//tblVehicleInfo.EnableViewState = true;

//ViewState["atbl"] = tbl;
//Session["atbl"] = tbl;



}

protected void btnaddquantity_Click(object sender, EventArgs e)
{

List<vehicledetails> lst = new List<vehicledetails>();
Vehicle car = new Vehicle ();
car.company = lstCompany.SelectedItem.Text.ToString();
car.model = lstModel.SelectedItem.Text.ToString();
try
{
//Table tbl = PlaceHolderTbl.FindControl("tbl") as Table;
// Table tbl = Session["atbl"] as Table;
//Session.Remove("atbl");
int rows = int.Parse(ViewState["quantity"].ToString());
if (rows>0)
{
for (int i = 1; i <= rows; i++)
{
VehicleDetails a = new VehicleDetails();
a.chassis = (Request.Form["txtChassis" + i]);
a.engine = (Request.Form["txtEngno" + i]);
a.colour = (Request.Form["txtColour-" + i]);
a.frieghtprice = int.Parse((Request.Form["txtFrieght" + i]));
a.investprice = int.Parse((Request.Form["txtInvest" + i]));
a.year = (Request.Form["txtYear" + i]);
lst.Add(a);
//a.chassis = (tbl.FindControl("txtChassis-" + i) as TextBox).Text.ToString();
//a.engine = (tbl.FindControl("txtEngno-" + i) as TextBox).Text.ToString();
//a.colour = (tbl.FindControl("txtColour-" + i) as TextBox).Text.ToString();
//a.frieghtprice = int.Parse((tbl.FindControl("txtFrieght-" + i) as TextBox).Text.ToString());
//a.investprice = int.Parse((tbl.FindControl("txtInvest-" + i) as TextBox).Text.ToString());
//a.year = (tbl.FindControl("txtYear-" + i) as TextBox).ToString();
//lst.Add(a);
}
lst.TrimExcess();
}


garage grg = new garage();
grg.AddQuantity(lst, car,int.Parse( txtQuantity.Text.ToString()));
}
catch (Exception ex)
{

lblStatus.Text = ex.Message;
}


}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900