从添加在网格页脚中的动态行中获取数据

时间:2018-07-11 13:15:36

标签: c# asp.net dynamic

我正在开发一个应用程序,其中添加了转发器控件,并且在其中添加了Grid View,因为我需要基于从db提取的数据显示单独的网格。因此,数据基于日期进行分组并分成网格。现在,每个网格都附有另一个按钮。如果用户单击,它将向该特定网格视图添加新行。假设用户单击两次添加了两个新行。我面临的问题是从要添加的行中获取值。由于行尚未成为网格的一部分,因此row.count()仅给我计数直到存在的行。因此,任何解决方案都如何获取值。这是代码:

   private void CreateExtraFooter(GridView grdview, List<Employee> list, int NoOfFooter_rows)
    {
        for (int i = 0; i < NoOfFooter_rows; i++)
        {
        GridViewRow row = new GridViewRow(i, i, DataControlRowType.Footer, DataControlRowState.Normal);

        #region Eachrow
        TableCell cell0 = new TableCell(); row.Cells.Add(cell0);
        TableCell cell1 = new TableCell(); row.Cells.Add(cell1);
        TableCell cell2 = new TableCell(); row.Cells.Add(cell2);
        TableCell cell3 = new TableCell(); row.Cells.Add(cell3);
        TableCell cell4 = new TableCell(); row.Cells.Add(cell4);
        TableCell cell5 = new TableCell(); row.Cells.Add(cell5);
        TableCell cell6 = new TableCell(); row.Cells.Add(cell6);
        TableCell cell7 = new TableCell(); row.Cells.Add(cell7);
        TableCell cell8 = new TableCell(); row.Cells.Add(cell8);
        TableCell cell9 = new TableCell(); row.Cells.Add(cell9);
        TableCell cell10 = new TableCell(); row.Cells.Add(cell10);
        TableCell cell11 = new TableCell(); row.Cells.Add(cell11);
        TableCell cell12 = new TableCell(); row.Cells.Add(cell12);
        TableCell cell13 = new TableCell(); row.Cells.Add(cell13);
        TableCell cell14 = new TableCell(); row.Cells.Add(cell14);
        TableCell cell15 = new TableCell(); row.Cells.Add(cell15);

        DropDownList ddlSalesRep_F = new DropDownList();
        ddlSalesRep_F.ID = "ddlSalesRep_F";

        ddlSalesRep_F.DataSource = list;
        ddlSalesRep_F.DataValueField = "EmpId";
        ddlSalesRep_F.DataTextField = "EmpName";
        ddlSalesRep_F.DataBind();
        row.Cells[0].Controls.Add(ddlSalesRep_F);

        TextBox txtPotsUpfrontPercentage_F = new TextBox();
        txtPotsUpfrontPercentage_F.ID = "txtPotsUpfrontPercentage_F";
        txtPotsUpfrontPercentage_F.MaxLength = 6;
        txtPotsUpfrontPercentage_F.BackColor = Color.GhostWhite;
        txtPotsUpfrontPercentage_F.Width = new Unit(50);
        txtPotsUpfrontPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtPotsUpfrontPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[1].Controls.Add(txtPotsUpfrontPercentage_F);

        TextBox txtPotsUnitPercentage_F = new TextBox();
        txtPotsUnitPercentage_F.ID = "txtPotsUnitPercentage_F";
        txtPotsUnitPercentage_F.MaxLength = 6;
        txtPotsUnitPercentage_F.BackColor = Color.GhostWhite;
        txtPotsUnitPercentage_F.Width = new Unit(50);
        txtPotsUnitPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtPotsUnitPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[2].Controls.Add(txtPotsUnitPercentage_F);

        TextBox txtPotsResPercentage_F = new TextBox();
        txtPotsResPercentage_F.ID = "txtPotsResPercentage_F";
        txtPotsResPercentage_F.MaxLength = 6;
        txtPotsResPercentage_F.BackColor = Color.GhostWhite;
        txtPotsResPercentage_F.Width = new Unit(50);
        txtPotsResPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtPotsResPercentage_F.Attributes.Add("onblur", "return isValidPOTS(this)");
        row.Cells[3].Controls.Add(txtPotsResPercentage_F);

        TextBox txtLdUpfrontPercentage_F = new TextBox();
        txtLdUpfrontPercentage_F.ID = "txtLdUpfrontPercentage_F";
        txtLdUpfrontPercentage_F.MaxLength = 6;
        txtLdUpfrontPercentage_F.BackColor = System.Drawing.ColorTranslator.FromHtml("#DDDCE7"); ;
        txtLdUpfrontPercentage_F.Width = new Unit(50);
        txtLdUpfrontPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtLdUpfrontPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[4].Controls.Add(txtLdUpfrontPercentage_F);

        TextBox txtLdUnitPercentage_F = new TextBox();
        txtLdUnitPercentage_F.ID = "txtLdUnitPercentage_F";
        txtLdUnitPercentage_F.MaxLength = 6;
        txtLdUnitPercentage_F.BackColor = System.Drawing.ColorTranslator.FromHtml("#DDDCE7"); ;
        txtLdUnitPercentage_F.Width = new Unit(50);
        txtLdUnitPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtLdUnitPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[5].Controls.Add(txtLdUnitPercentage_F);

        TextBox txtLdResPercentage_F = new TextBox();
        txtLdResPercentage_F.ID = "txtLdResPercentage_F";
        txtLdResPercentage_F.MaxLength = 6;
        txtLdResPercentage_F.BackColor = System.Drawing.ColorTranslator.FromHtml("#DDDCE7"); ;
        txtLdResPercentage_F.Width = new Unit(50);
        txtLdResPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtLdResPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[6].Controls.Add(txtLdResPercentage_F);

        TextBox txtAdsUpfrontPercentage_F = new TextBox();
        txtAdsUpfrontPercentage_F.ID = "txtAdsUpfrontPercentage_F";
        txtAdsUpfrontPercentage_F.MaxLength = 6;
        txtAdsUpfrontPercentage_F.BackColor = Color.GhostWhite;
        txtAdsUpfrontPercentage_F.Width = new Unit(50);
        txtAdsUpfrontPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtAdsUpfrontPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[7].Controls.Add(txtAdsUpfrontPercentage_F);

        TextBox txtAdsUnitPercentage_F = new TextBox();
        txtAdsUnitPercentage_F.ID = "txtAdsUnitPercentage_F";
        txtAdsUnitPercentage_F.MaxLength = 6;
        txtAdsUnitPercentage_F.BackColor = Color.GhostWhite;
        txtAdsUnitPercentage_F.Width = new Unit(50);
        txtAdsUnitPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtAdsUnitPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[8].Controls.Add(txtAdsUnitPercentage_F);

        TextBox txtAdsResPercentage_F = new TextBox();
        txtAdsResPercentage_F.ID = "txtAdsResPercentage_F";
        txtAdsResPercentage_F.MaxLength = 6;
        txtAdsResPercentage_F.BackColor = Color.GhostWhite;
        txtAdsResPercentage_F.Width = new Unit(50);
        txtAdsResPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtAdsResPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[9].Controls.Add(txtAdsResPercentage_F);

        TextBox txtBroadbandUpfrontPercentage_F = new TextBox();
        txtBroadbandUpfrontPercentage_F.ID = "txtBroadbandUpfrontPercentage_F";
        txtBroadbandUpfrontPercentage_F.MaxLength = 6;
        txtBroadbandUpfrontPercentage_F.BackColor = System.Drawing.ColorTranslator.FromHtml("#DDDCE7"); ;
        txtBroadbandUpfrontPercentage_F.Width = new Unit(50);
        txtBroadbandUpfrontPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtBroadbandUpfrontPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[10].Controls.Add(txtBroadbandUpfrontPercentage_F);

        TextBox txtBroadbandUnitPercentage_F = new TextBox();
        txtBroadbandUnitPercentage_F.ID = "txtBroadbandUnitPercentage_F";
        txtBroadbandUnitPercentage_F.MaxLength = 6;
        txtBroadbandUnitPercentage_F.BackColor = System.Drawing.ColorTranslator.FromHtml("#DDDCE7"); ;
        txtBroadbandUnitPercentage_F.Width = new Unit(50);
        txtBroadbandUnitPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtBroadbandUnitPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[11].Controls.Add(txtBroadbandUnitPercentage_F);

        TextBox txtBroadbandResPercentage_F = new TextBox();
        txtBroadbandResPercentage_F.ID = "txtBroadbandResPercentage_F";
        txtBroadbandResPercentage_F.MaxLength = 6;
        txtBroadbandResPercentage_F.BackColor = System.Drawing.ColorTranslator.FromHtml("#DDDCE7"); ;
        txtBroadbandResPercentage_F.Width = new Unit(50);
        txtBroadbandResPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtBroadbandResPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[12].Controls.Add(txtBroadbandResPercentage_F);

        TextBox txtLdCanUpfrontPercentage_F = new TextBox();
        txtLdCanUpfrontPercentage_F.ID = "txtLdCanUpfrontPercentage_F";
        txtLdCanUpfrontPercentage_F.MaxLength = 6;
        txtLdCanUpfrontPercentage_F.BackColor = Color.GhostWhite;
        txtLdCanUpfrontPercentage_F.Width = new Unit(50);
        txtLdCanUpfrontPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtLdCanUpfrontPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[13].Controls.Add(txtLdCanUpfrontPercentage_F);

        TextBox txtLdCanUnitPercentage_F = new TextBox();
        txtLdCanUnitPercentage_F.ID = "txtLdCanUnitPercentage_F";
        txtLdCanUnitPercentage_F.MaxLength = 6;
        txtLdCanUnitPercentage_F.BackColor = Color.GhostWhite;
        txtLdCanUnitPercentage_F.Width = new Unit(50);
        txtLdCanUnitPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtLdCanUnitPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[14].Controls.Add(txtLdCanUnitPercentage_F);

        TextBox txtLdCanResPercentage_F = new TextBox();
        txtLdCanResPercentage_F.ID = "txtLdCanResPercentage_F";
        txtLdCanResPercentage_F.MaxLength = 6;
        txtLdCanResPercentage_F.BackColor = Color.GhostWhite;
        txtLdCanResPercentage_F.Width = new Unit(50);
        txtLdCanResPercentage_F.Attributes.Add("onkeypress", "return isValidPercentageKey(event)");
        txtLdCanResPercentage_F.Attributes.Add("onblur", "return isValidNumber(this)");
        row.Cells[15].Controls.Add(txtLdCanResPercentage_F);
        #endregion

        grdview.Controls[0].Controls.Add(row);

        }
    }

此方法将新行添加到网格中,并且根据单击的次数将其添加到网格中。唯一的问题是,当用户在行字段中输入值时,如何获取新添加的行的值。

    if (ViewState["ExtraFooter_Primary"] == null || ViewState   ["ExtraFooter_Primary"].ToString() == "-1")
            {
                ViewState["ExtraFooter_Primary"] = "-1";


                Session.Add(ConstantSession.EmployeeList, employeeList);

                DropDownList ddlSalesRep = gvPrimaryGrid.FooterRow.FindControl("ddlSalesRep_F") as DropDownList;
                ddlSalesRep.DataSource = employeeList;
                ddlSalesRep.DataValueField = "SMPID";
                ddlSalesRep.DataTextField = "SMPName";
                ddlSalesRep.DataBind();
            }

            Button btnAddPrimaryRep = (Button)e.Item.FindControl("btnAddPrimaryRep");
            btnAddPrimaryRep.Visible = Convert.ToInt16(ViewState["ExtraFooter_Primary"]) > 3 ? false : true;

            ViewState["ExtraFooter_Primary"] = (Convert.ToInt16(ViewState["ExtraFooter_Primary"]) + 1).ToString();
            CreateExtraFooter(gvPrimaryGrid, employeeList, Convert.ToInt16(ViewState["ExtraFooter_Primary"]));

0 个答案:

没有答案