Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 动态控件-具有相同ID的多个控件_Asp.net_Dynamic_Controls - Fatal编程技术网

Asp.net 动态控件-具有相同ID的多个控件

Asp.net 动态控件-具有相同ID的多个控件,asp.net,dynamic,controls,Asp.net,Dynamic,Controls,我的动态创建的表/控件有问题。我已经搜索了互联网站,找到了一些解决方案,并尝试了一些,但出于什么原因,我没有看到下面的代码 本质上,我希望能够在需要时添加新列,然后重新加载表,并删除动态创建的列。然后我想做一次回发,以获取动态创建的单元格的值。相当直截了当 代码如下: using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using Syst

我的动态创建的表/控件有问题。我已经搜索了互联网站,找到了一些解决方案,并尝试了一些,但出于什么原因,我没有看到下面的代码

本质上,我希望能够在需要时添加新列,然后重新加载表,并删除动态创建的列。然后我想做一次回发,以获取动态创建的单元格的值。相当直截了当

代码如下:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

namespace TimeTracker
{
    public partial class _Default : System.Web.UI.Page
    {


        private int NumberofRows
        {
            get
            {
                object o = ViewState["NumberofRows"];
                if (o == null) return 4;
                return (int)o;
            }
            set
            {
                ViewState["NumberofRows"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //{
                CreateBigTestTable();
            //}
        }

        /// <summary>
        /// 
        /// </summary>
        protected void CreateBigTestTable()
        {
            Table1.Controls.Clear();
            DataTable dt;

            if (Cache["GetData"] != null)
            {
                dt = (DataTable)Cache["GetData"];
            }
            else
            {
                dt = GetData();
                Cache.Insert("GetData", dt, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
            }

            int colSpan = dt.Rows.Count;
            int rowCount = NumberofRows;

            // Create a TableHeaderRow.
            TableHeaderRow headerRow = new TableHeaderRow();
            headerRow.BackColor = System.Drawing.Color.AliceBlue;

            // Create TableCell objects to contain the text for the header.
            TableHeaderCell headerTableCell1 = new TableHeaderCell();
            TableHeaderCell headerTableCell2 = new TableHeaderCell();
            TableHeaderCell headerTableCell3 = new TableHeaderCell();
            headerTableCell1.Text = "";
            headerTableCell1.Scope = TableHeaderScope.Column;
            headerTableCell1.AbbreviatedText = "";

            Label lblheaderTableCell2 = new Label();
            lblheaderTableCell2.Text = "Labor Category Hours";

            ImageButton imgBtnNew = new ImageButton();
            imgBtnNew.ImageUrl = "~/images/plus.gif";
            imgBtnNew.ImageAlign = ImageAlign.Top;
            imgBtnNew.ID = "imgBtnNew";
            imgBtnNew.Click += new ImageClickEventHandler(imgBtnNew_Click);

            headerTableCell2.Controls.Add(lblheaderTableCell2);
            headerTableCell2.Controls.Add(imgBtnNew);
            headerTableCell2.ColumnSpan = colSpan;

            headerTableCell3.Text = "Total Hours";
            headerTableCell3.HorizontalAlign = HorizontalAlign.Right;
            headerTableCell3.Scope = TableHeaderScope.Column;
            headerTableCell3.AbbreviatedText = "Total Hours";
            headerTableCell3.ColumnSpan = 2;

            headerRow.Cells.Add(headerTableCell1);
            headerRow.Cells.Add(headerTableCell2);
            headerRow.Cells.Add(headerTableCell3);

            ///
            ///Sub header row
            ///
            TableHeaderRow headerRow1 = new TableHeaderRow();
            headerRow1.BackColor = System.Drawing.Color.AliceBlue;

            TableHeaderCell headerTableCell11 = new TableHeaderCell();
            TableHeaderCell headerTableCell22 = new TableHeaderCell();
            TableHeaderCell headerTableCell33 = new TableHeaderCell();
            TableHeaderCell headerTableCell44 = new TableHeaderCell();

            headerTableCell11.Text = "Functional Area";
            headerTableCell11.Scope = TableHeaderScope.Column;
            headerTableCell11.AbbreviatedText = "Functional Area";
            headerTableCell11.Height = 100;
            headerTableCell11.VerticalAlign = VerticalAlign.Bottom;

            headerRow1.Cells.Add(headerTableCell11);

            // Labor Category Title
            foreach (DataRow dr in dt.Rows)
            {
                TableCell tempCell = new TableCell();

                Label lblLaborCategoryTitle = new Label();
                lblLaborCategoryTitle.Text = dr["Title"].ToString();
                tempCell.Controls.Add(lblLaborCategoryTitle);

                ImageButton ctrl = new ImageButton();
                ctrl.ID = "dynamicImageButton" + dr["ID"].ToString();
                ctrl.ImageUrl = "~/images/delete.gif";
                ctrl.Click += new ImageClickEventHandler(img_Click);

                tempCell.Controls.Add(ctrl);

                headerRow1.Cells.Add(tempCell);  
            }

            headerTableCell33.Text = "Current Period";
            headerTableCell33.HorizontalAlign = HorizontalAlign.Right;
            headerTableCell33.Scope = TableHeaderScope.Column;
            headerTableCell33.AbbreviatedText = "Current Period";
            headerTableCell33.Height = 100;
            headerTableCell33.VerticalAlign = VerticalAlign.Top;
            headerRow1.Cells.Add(headerTableCell33);

            headerTableCell44.Text = "Cummulative";
            headerTableCell44.HorizontalAlign = HorizontalAlign.Right;
            headerTableCell44.Scope = TableHeaderScope.Column;
            headerTableCell44.AbbreviatedText = "Cummulative";
            headerTableCell44.Height = 100;
            headerTableCell44.VerticalAlign = VerticalAlign.Top;
            headerRow1.Cells.Add(headerTableCell44);

            // Add the TableHeaderRow as the first item in the Rows collection of the table.
            Table1.Rows.AddAt(0, headerRow);
            Table1.Rows.AddAt(1, headerRow1);

            ///
            /// Add rows to the table.
            ///
            for (int rowNum = 0; rowNum < rowCount; rowNum++)
            {
                TableRow tempRow = new TableRow();

                // add cell for functional area
                TableCell tempCellSpacer = new TableCell();
                tempCellSpacer.Text = "Functional Area: " + rowNum.ToString();
                tempRow.Cells.Add(tempCellSpacer);

                for (int cellNum = 0; cellNum < colSpan; cellNum++)
                {
                    TableCell tempCell = new TableCell();

                    TextBox tb = new TextBox();
                    tb.MaxLength = 128;
                    //tb.ID = "txt_Number" + cellNum.ToString();
                    tb.Text = String.Format("({0},{1})", rowNum, cellNum);
                    tempCell.Controls.Add(tb);
                    tempRow.Cells.Add(tempCell);
                }

                // add cells for current period
                TableCell tempCurrentPeriod = new TableCell();
                tempRow.Cells.Add(tempCurrentPeriod);

                // add cells for Cummulative
                TableCell tempCummulative = new TableCell();
                tempRow.Cells.Add(tempCummulative);

                Table1.Rows.Add(tempRow);
            }

            ///
            /// Create a TableFooterRow
            /// 
            TableFooterRow footerRow = new TableFooterRow();
            footerRow.BackColor = System.Drawing.Color.AliceBlue;

            // Create TableCell objects to contain the text for the footer.
            TableCell footerTableCell1 = new TableCell();
            TableCell footerTableCell2 = new TableCell();
            TableCell footerTableCell3 = new TableCell();
            footerTableCell1.Text = "Total Amount";
            footerTableCell1.ColumnSpan = colSpan+1;
            footerTableCell1.HorizontalAlign = HorizontalAlign.Right;
            footerTableCell2.Text = "Column 2 footer";
            footerTableCell2.HorizontalAlign = HorizontalAlign.Right;
            footerTableCell3.Text = "Column 3 footer";
            footerTableCell3.HorizontalAlign = HorizontalAlign.Right;

            // Add the TableCell objects to the Cells collection of the TableFooterRow.
            footerRow.Cells.Add(footerTableCell1);
            footerRow.Cells.Add(footerTableCell2);
            footerRow.Cells.Add(footerTableCell3);

            // Add the TableFooterRow to the Rows collection of the table.
            Table1.Rows.Add(footerRow);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void img_Click(object sender, ImageClickEventArgs e)
        {
            Response.Write("Test");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnNew_Click(object sender, ImageClickEventArgs e)
        {
            DataTable dt;

            if (Cache["GetData"] != null)
            {
                dt = (DataTable)Cache["GetData"];
                dt.Rows.Add(new Object[] { 5, "Test" });
            }
            else
            {
                dt = GetData();
                dt.Rows.Add(new Object[] { 5, "Test" });
                Cache.Insert("GetData", dt, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
            }

            CreateBigTestTable();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ProcessControls(Table1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="control"></param>
        private void ProcessControls(Control control)
        {
            foreach (Control ctrl in control.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                {
                    Response.Write(string.Format("Control ID: {0} Text: {1}<br/>", ((TextBox)ctrl).ID, ((TextBox)ctrl).Text)); 
                }

                if (ctrl.HasControls())
                    ProcessControls(ctrl);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("ID", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Title", typeof(String)));

            dr = dt.NewRow();
            dr[0] = "1";
            dr[1] = "Web Developer";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "2";
            dr[1] = "Project Manager";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "3";
            dr[1] = "System Analyst";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "4";
            dr[1] = "Database Administrator";
            dt.Rows.Add(dr);

            return dt;
        }
    }
}
使用系统;
使用系统集合;
使用系统配置;
使用系统数据;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用系统文本;
命名空间时间跟踪器
{
公共部分类\u默认值:System.Web.UI.Page
{
私有整数行数
{
得到
{
对象o=视图状态[“NumberofRows”];
如果(o==null)返回4;
返回(int)o;
}
设置
{
ViewState[“NumberofRows”]=值;
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
//如果(!Page.IsPostBack)
//{
CreateBigTestTable();
//}
}
/// 
/// 
/// 
受保护的void CreateBigTestTable()
{
表1.Controls.Clear();
数据表dt;
如果(缓存[“GetData”]!=null)
{
dt=(数据表)缓存[“GetData”];
}
其他的
{
dt=GetData();
Insert(“GetData”,dt,null,System.Web.Caching.Cache.noabsolutionexpntiation,TimeSpan.frommins(20));
}
int colSpan=dt.Rows.Count;
int rowCount=行数;
//创建一个TableHeaderRow。
TableHeaderRow headerRow=新的TableHeaderRow();
headerRow.BackColor=System.Drawing.Color.AliceBlue;
//创建TableCell对象以包含标题文本。
TableHeaderCell headerTableCell1=新的TableHeaderCell();
TableHeaderCell headerTableCell2=新的TableHeaderCell();
TableHeaderCell headerTableCell3=新的TableHeaderCell();
headerTableCell1.Text=“”;
headerTableCell1.Scope=表格HeaderScope.Column;
headerTableCell1.缩写文本=”;
Label lblheaderTableCell2=新标签();
lblheaderTableCell2.Text=“人工类别工时”;
ImageButton imgBtnNew=新建ImageButton();
imgBtnNew.ImageUrl=“~/images/plus.gif”;
imgBtnNew.ImageAlign=ImageAlign.Top;
imgBtnNew.ID=“imgBtnNew”;
imgBtnNew.Click+=新建图像ClickEventHandler(imgBtnNew\u Click);
headerTableCell2.Controls.Add(lblheaderTableCell2);
headerTableCell2.Controls.Add(imgBtnNew);
headerTableCell2.ColumnSpan=colSpan;
headerTableCell3.Text=“总小时数”;
headerTableCell3.HorizontalAlign=HorizontalAlign.Right;
headerTableCell3.Scope=表格HeaderScope.Column;
headerTableCell3.缩写text=“总小时数”;
headerTableCell3.ColumnSpan=2;
headerRow.Cells.Add(headerTableCell1);
headerRow.Cells.Add(headerTableCell2);
headerRow.Cells.Add(headerTableCell3);
///
///子标题行
///
TableHeaderRow headerRow1=新的TableHeaderRow();
headerRow1.BackColor=System.Drawing.Color.AliceBlue;
TableHeaderCell headerTableCell11=新的TableHeaderCell();
TableHeaderCell headerTableCell22=新的TableHeaderCell();
TableHeaderCell headerTableCell33=新的TableHeaderCell();
TableHeaderCell headerTableCell44=新的TableHeaderCell();
headerTableCell11.Text=“功能区”;
headerTableCell11.Scope=表格HeaderScope.Column;
headerTableCell11.缩写为text=“功能区”;
headerTableCell11.高度=100;
headerTableCell11.VerticalAlign=垂直Align.底部;
headerRow1.单元格。添加(headerTableCell11);
//劳工类别名称
foreach(数据行dr在dt.行中)
{
TableCell tempCell=新的TableCell();
Label lblLaborCategoryTitle=新标签();
lbllaborcategorytle.Text=dr[“Title”].ToString();
添加(lblLaborCategoryTitle);
ImageButton ctrl=新建ImageButton();
ctrl.ID=“dynamicImage按钮”+dr[“ID”].ToString();
ctrl.ImageUrl=“~/images/delete.gif”;
ctrl.Click+=新建ImageClickEventHandler(img\u单击);
tempCell.Controls.Add(ctrl);
headerRow1.Cells.Add(tempCell);
}
headerTableCell33.Text=“本期”;
headerTableCell33.HorizontalAlign=HorizontalAlign.Right;
headerTableCell33.Scope=表格HeaderScope.Column;
headerTableCell33.缩写text=“本期”;
headerTableCell33.高度=100;
headerTableCell33.VerticalAlign=VerticalAlign.Top;
headerRow1.单元格。添加(headerTableCell33);
headerTableCell44.Text=“累计”;
headerTableCell44.HorizontalAlign=HorizontalAlign.Right;
headerTableCell44.Scope=表格HeaderScope.Column;
headerTableCell44.缩写text=“累积”;
headerTableCell44.高度=100;
headerTableCell44.VerticalAlign=VerticalAlign.Top;
headerRow1.单元格。添加(headerTableCell44);
//将TableHeaderRow添加为第一个i
public string GetUniqueId(string key)
{
  //lookup "key" in a private list
  //if the key is present, generate a random number, append it
  //to "key", add it to the list for remembering it and return it
}