Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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
C# 如何获取ID';动态生成控件的数量?_C#_Asp.net_Button_Dynamic_File Upload - Fatal编程技术网

C# 如何获取ID';动态生成控件的数量?

C# 如何获取ID';动态生成控件的数量?,c#,asp.net,button,dynamic,file-upload,C#,Asp.net,Button,Dynamic,File Upload,Hiiii 在下图中,动态生成其中的表和控件 单击“创建表格”按钮,使用dropdownlist中的值创建表格,该列表的行数作为值 如何获取特定“文件上传”和“上传”按钮的ID 单击“上载”按钮时,应仅访问与该按钮相同行中的fileupload控件&它不应循环通过表中的所有fileupload控件 在我的代码中,当我单击较低行(比如第3行)中的上载按钮时,它也会上载在较高的文件上载控件中选择的文件(这里是第1行、第2行和第3行) 我不要这个。我只想从点击按钮的同一行的filupload控件上传

Hiiii

在下图中,动态生成其中的表和控件

单击“创建表格”按钮,使用dropdownlist中的值创建表格,该列表的行数作为值

如何获取特定“文件上传”和“上传”按钮的ID

单击“上载”按钮时,应仅访问与该按钮相同行中的fileupload控件&它不应循环通过表中的所有fileupload控件

在我的代码中,当我单击较低行(比如第3行)中的上载按钮时,它也会上载在较高的文件上载控件中选择的文件(这里是第1行、第2行和第3行)

我不要这个。我只想从点击按钮的同一行的filupload控件上传文件

代码:

public partial class stable : System.Web.UI.Page
{
private int tblRow;
private int tblCol = 9;
private int i, j;
private bool CTflag;

Table table = new Table();
TableRow row,rrow;
TableCell cell,rcell;
FileUpload fileUp;
Button UpLdButton;
Button btnCal;
TextBox tb;

Label tbr;

string cmdArg; // for passing filuploaders id with Command button

private string filename = "fileUpLoader";

private string tbRowId = "row";
private string tbColId = "col";

protected int Rows
{
    get
    {
        return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
    }
    set
    {
        ViewState["Rows"] = tblRow;
    }
}

// Columns property to hold the Columns in the ViewState
protected int Columns
{
    get
    {
        return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
    }
    set
    {
        ViewState["Columns"] = tblCol;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {            
        if (CTflag == false)
        {
            this.Rows = tblRow;
            this.Columns = tblCol;                
            CreateDynamicTable();                
        }
        else
        {                
            CTflag = true;                
        }
    }
    //LoadViewState(object this);
    //CreateDynamicTable();
}
protected void Button1_Click(object sender, EventArgs e)
{        
    clrControls();
    CreateDynamicTable(); 
}

protected void CreateDynamicTable()
{        
    tblRow = Convert.ToInt32(DropDownList1.SelectedValue);

    //Creat the Table and Add it to the Page    
    if (CTflag == false)
    {            
        table.Caption = "Challan Entry";
        table.ID = "Challan Entry";
        table.BackColor = System.Drawing.Color.BurlyWood;
        Page.Form.Controls.Add(table);

        // Now iterate through the table and add your controls
        for (i = 0; i < 1; i++)
        {
            row = new TableRow();
            row.BorderStyle = BorderStyle.Ridge;

            for (j = 0; j <= tblCol; j++)
            {
                cell = new TableCell();
                cell.BorderWidth = 5;
                cell.BorderStyle = BorderStyle.Ridge;
                cell.BorderColor = System.Drawing.Color.Azure;
                for (j = 0; j <= tblCol; j++)
                {
                    string[] Header = { "CC NO.", "DATE", "TotalAmt", "NoOfRecpt", "Energy", "New", "Theft", "Misc", "SelectFile", "Upload", "Status" };
                    Label lbl = new Label();
                    lbl.ID = "lblHeader" + j;
                    if (j == 8)
                    {
                        lbl.Width = 220;
                    }
                    else if (j == 9)
                    {
                        lbl.Width = 50;
                    }
                    else
                    {
                        lbl.Width = 100;
                    }
                    lbl.Text = Header[j];

                    // Add the control to the TableCell
                    cell.Controls.Add(lbl);
                }
                row.Cells.Add(cell);
            }

            // Add the TableRow to the Table
            table.Rows.Add(row);
        }
        for (i = 0; i < tblRow; i++)
        {
            row = new TableRow();
            row.ID = tbRowId + i;
            row.BorderStyle = BorderStyle.Ridge;

            for (j = 0; j <= tblCol; j++)
            {
                cell = new TableCell();
                cell.ID = tbColId + i + j;
                cell.BorderWidth = 5;
                cell.BorderStyle = BorderStyle.Ridge;
                cell.BorderColor = System.Drawing.Color.Azure;
                for (j = 0; j <= 0; j++)
                {
                    Label lbl = new Label();
                    lbl.ID = "lblCCRow" + i + "Col" + j;
                    lbl.Text = "CC NO. " + i + " ";
                    lbl.Width = 100;
                    // Add the control to the TableCell
                    cell.Controls.Add(lbl);
                }
                for (j = 1; j <= 1; j++)
                {
                    Label lbl = new Label();
                    lbl.ID = "lblRow" + i + "Col" + j;
                    lbl.Width = 100;
                    lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
                    // Add the control to the TableCell
                    cell.Controls.Add(lbl);
                }
                for (j = 2; j <= 7; j++)
                {
                    tb = new TextBox();
                    tb.Width = 100;
                    tb.ID = "txtBoxRow" + i + "Col" + j;
                    //txtbxNames[i,j] = Convert.ToString(tb.ID);
                    tb.Text = "0";
                    // Add the control to the TableCell
                    cell.Controls.Add(tb);
                }
                for (j = 8; j <= 8; j++)
                {
                    fileUp = new FileUpload();
                    //m = i; n = j;
                    fileUp.ID = filename + i + j;
                    fileUp.Width = 220;
                    cell.Controls.Add(fileUp);
                    cmdArg = fileUp.ID;

                    UpLdButton = new Button();
                    UpLdButton.Width = 100;
                    UpLdButton.Text = "Upload" + i + j;
                    UpLdButton.ID = UpLdButton.Text;
                    UpLdButton.CommandArgument= cmdArg;
                    cell.Controls.Add(UpLdButton);

                    UpLdButton.Click += new EventHandler(UpLdButton_Click);
                }
                for (j = 9; j <= 9; j++)
                {
                    Label lbl = new Label();
                    lbl.ID = "lblRow" + i + j;
                    lbl.Text = "[ Status ]";
                    lbl.Width = 100;
                    // Add the control to the TableCell
                    cell.Controls.Add(lbl);
                }
                row.Cells.Add(cell);
            }
            // Add the TableRow to the Table
            table.Rows.Add(row);
        } //outer for-loop end

        for (i = 0; i < 1; i++)
        {
            rrow = new TableRow();
            rrow.ID = "ResultRow";
            rrow.BorderStyle = BorderStyle.Ridge;                

            for (j = 0; j <= tblCol; j++)
            {
                rcell = new TableCell();
                rcell.ID = "resultCol" + j;
                rcell.BorderWidth = 5;
                rcell.BorderStyle = BorderStyle.Ridge;
                rcell.BorderColor = System.Drawing.Color.Azure;
                for (j = 0; j <= 0; j++)
                {
                    Label lbl = new Label();
                    //lbl.ID = "lblCCRow" + i + "Col" + j;
                    lbl.Text = "<b>Total</b>";
                    lbl.Width = 100;
                    // Add the control to the TableCell
                    rcell.Controls.Add(lbl);
                }
                for (j = 1; j <= 1; j++)
                {
                    Label lbl = new Label();
                    //lbl.ID = "lblRow" + i + "Col" + j;
                    lbl.Width = 100;
                    lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
                    // Add the control to the TableCell
                    rcell.Controls.Add(lbl);
                }
                for (j = 2; j <= 7; j++)
                {
                    tbr = new Label();
                    tbr.Width = 100;
                    tbr.ID = "txtResult" +i+j;                        
                    tbr.Text = tbr.ID;
                    tbr.EnableTheming = true;
                    tbr.BackColor = System.Drawing.Color.White;
                    //txtResNames[i, j] = Convert.ToString(tbr.ID);
                    // Add the control to the TableCell
                    rcell.Controls.Add(tbr);
                }
                for (j = 8; j <= 8; j++)
                {
                    btnCal = new Button();
                    btnCal.Width = 100;
                    btnCal.Text = "Calculate";
                    btnCal.ID = btnCal.Text;                        
                    rcell.Controls.Add(btnCal);
                    btnCal.Click += new EventHandler(btnCal_Click);
                }

                rrow.Cells.Add(rcell);
            }

            // Add the TableRow to the Table
            table.Rows.Add(rrow);
        }

        //flag seetting
        CTflag = true;
        ViewState["dynamictable"] = true;
    }
}

void btnCal_Click(object sender, EventArgs e)
{
    TextBox tbres = new TextBox();
    TextBox tbTemp = new TextBox();

    double TotAmt = 0, NoofRect = 0, Energy = 0,New1 = 0, Theft = 0, Misc = 0;
    for (int i = 0; i < tblRow; i++)
    {
        for (int j = 2; j <= 7; j++)
        {
            TextBox tb = (TextBox)FindControlRecursive(this, string.Format("txtBoxRow{0}Col{1}", i, j));
            Label tbr = (Label)FindControlRecursive(this, string.Format("txtResult{0}{1}", 0, j));
            switch (j)
            {
                case 2:
                    TotAmt += Convert.ToDouble(tb.Text);
                    Label1.Text = Convert.ToString(TotAmt);
                    tbr.Text = Convert.ToString(TotAmt);
                    break;
                case 3:
                    NoofRect += Convert.ToDouble(tb.Text);
                    //Label1.Text = Convert.ToString(NoofRect);
                    tbr.Text = Convert.ToString(NoofRect);
                    break;
                case 4:
                    Energy+= Convert.ToDouble(tb.Text);
                    //Label1.Text = Convert.ToString(TotAmt);
                    tbr.Text = Convert.ToString(Energy);
                    break;
                case 5:
                    New1+= Convert.ToDouble(tb.Text);
                    //Label1.Text = Convert.ToString(TotAmt);
                    tbr.Text = Convert.ToString(New1);
                    break;
                case 6:
                    Theft+= Convert.ToDouble(tb.Text);
                    //Label1.Text = Convert.ToString(TotAmt);
                    tbr.Text = Convert.ToString(Theft);
                    break;
                case 7:
                    Misc+= Convert.ToDouble(tb.Text);
                    //Label1.Text = Convert.ToString(TotAmt);
                    tbr.Text = Convert.ToString(Misc);
                    break;                    
            }
        }
    }        
}

protected void clrControls()
{
    Label1.Text = "";
    for (int i = 0; i < tblRow; i++)
    {
        for (int j = 2; j <= 7; j++)
        {
            fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", i, 8));
            fileUp.Enabled = true;
            Button btn = (Button)FindControlRecursive(this, string.Format("Upload{0}{1}", i, 8));
            btn.Enabled=true;
            TextBox tb = (TextBox)FindControlRecursive(this, string.Format("txtBoxRow{0}Col{1}", i, j));
            tb.Text = "0";
            Label tbr = (Label)FindControlRecursive(this, string.Format("txtResult{0}{1}", 0, j));
            tbr.Text = "0";
            Label statlbl = new Label();
            statlbl = (Label)FindControlRecursive(this, string.Format("lblRow{0}{1}", i, 9));
            statlbl.Text = "[status]";
        }
    }
    for (i = 0; i < 1; i++)
    {
        for (j = 8; j <= 8; j++)
        {
            btnCal.Enabled = true;
        }
    }
}

protected override void LoadViewState(object earlierState)
{
    base.LoadViewState(earlierState);
    if (ViewState["dynamictable"] == null)
    {
        CreateDynamicTable();            
    }        
}

void UpLdButton_Click(object sender, EventArgs e)
{
    Button btnUpLD = sender as Button;

    for (int i = 0; i < tblRow; i++)
    {
        Button tb = (Button)FindControlRecursive(this, string.Format("Upload{0}{1}", i, 8));
        fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", i, 8));
        Label statlbl = new Label();            
        statlbl = (Label)FindControlRecursive(this, string.Format("lblRow{0}{1}", i, 9));            
        if (!fileUp.HasFile)
        {
            //statlbl.Text = "[status]";
            continue;
        }
        else
        {                
            UploadFile(fileUp,tb);
            if (tb.Enabled == true && fileUp.Enabled == true)
            {
                statlbl.Text = "[status]";
            }
            else
            {
                statlbl.Text = "Uploaded";
            }                
        }        
    }            
}    

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}

public static Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
    {
        return root;
    }

    foreach (Control c in root.Controls)
    {
        Control t = FindControlRecursive(c, id);
        if (t != null)
        {
            return t;
        }
    }
    return null;
}

protected void UploadFile(FileUpload xyz, Button btn)
{        
    if (xyz.HasFile)
    {
        string extension = System.IO.Path.GetExtension(xyz.FileName);
        if (extension == ".dat" || extension == ".B60")
        {
            if (File.Exists(Server.MapPath("~\\") + xyz.FileName))
            {
                Label1.Text = "File " + xyz.FileName + " Already Exists!";
            }
            else
            {
                xyz.PostedFile.SaveAs(Server.MapPath("~\\") + xyz.FileName);
                Label1.Text = "The " + xyz.FileName + " Has been uploaded";
                btn.Enabled = false;
                xyz.Enabled = false;
            }
        }
        else
        {
            Label1.Text = "* You can select only '.dat' & '.B60' type files";
            btn.Enabled = true;
            xyz.Enabled = true;
        }            
    }
    else 
    {        
        Label1.Text = "Select a file";
    }        
}   
}
公共部分类稳定:System.Web.UI.Page
{
私人国际贸易;
私有int tblCol=9;
私人int i,j;
私有布尔CTflag;
Table Table=新表();
表格行,箭头;
表细胞;
文件上传文件上传;
按钮上传按钮;
按钮btnCal;
文本框tb;
标签待定;
string cmdArg;//用于通过命令按钮传递filuploaders id
私有字符串filename=“fileUpLoader”;
私有字符串tbRowId=“行”;
专用字符串tbColId=“col”;
受保护整数行
{
收到
{
返回ViewState[“Rows”]!=null?(int)ViewState[“Rows”]:0;
}
设置
{
视图状态[“行”]=tblRow;
}
}
//Columns属性以在ViewState中保存列
受保护的int列
{
收到
{
返回ViewState[“Columns”]!=null?(int)ViewState[“Columns”]:0;
}
设置
{
视图状态[“列”]=tblCol;
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(第IsPostBack页)
{            
如果(CTflag==false)
{
此.Rows=tblRow;
此列=tblCol;
CreateDynamicTable();
}
其他的
{                
CTflag=true;
}
}
//LoadViewState(此对象);
//CreateDynamicTable();
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{        
clrControls();
CreateDynamicTable();
}
受保护的void CreateDynamicTable()
{        
tblRow=Convert.ToInt32(DropDownList1.SelectedValue);
//创建表格并将其添加到页面
如果(CTflag==false)
{            
table.Caption=“Challan条目”;
table.ID=“Challan条目”;
table.BackColor=System.Drawing.Color.BurlyWood;
Page.Form.Controls.Add(表);
//现在遍历该表并添加控件
对于(i=0;i<1;i++)
{
行=新表行();
row.BorderStyle=BorderStyle.Ridge;

例如,对于(j=0;j为什么不在按钮的命令参数中存储一个行ID,然后在单击按钮时,可以使用它在所有行中循环,直到ID与要查找的行匹配为止

foreach (DateRow test in table.Rows)
  if ((test.findcontrol(uploadbutton) as Button).commandArgument = (Sender as Button).commandArgumrnt 
{
    //do stuff here 
}
    else
    {
    \\ do nothing
    }

希望这有帮助

您能减少这段代码,只显示相关部分吗?所有这些文件上载控件的格式都相同,因此它们都将同时提交。HTML表单就是这样工作的。至于识别单击了哪个特定按钮,我相信您的单击处理程序可能能够识别它,但我没有一个详细的“如何”离开我的头顶(应该在发送者对象)。