无法从动态表中生成的文本框中获取值 [英] Unable to get values from textbox generated in dynamic tables

查看:68
本文介绍了无法从动态表中生成的文本框中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从放置在动态表中的文本框中获取值.

以下是按钮的代码,该按钮通过从文本框中获取数字来生成要询问的字段数:

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;
}


生成texbox和rows后,用户在输入信息时.跟随按钮从文本框中获取文本以执行任务.此处无法获取所有TEXBOX的值

以下是获取值的按钮:


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;
            }
            

        }

推荐答案


检查此
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 


之前输入


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>");

  }


<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>



最好的问候
米特瓦里(M.Mitwalli)



Best Regards
M.Mitwalli


这篇关于无法从动态表中生成的文本框中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆