【问题标题】:ASP.NET Multiple controls with the same ID 'x' were found. FindControlASP.NET 找到具有相同 ID 'x' 的多个控件。查找控件
【发布时间】:2010-10-07 20:45:10
【问题描述】:

得到以下错误

找到多个具有相同 ID 'ltlItemCode' 的控件。 FindControl 要求控件具有唯一的 ID。

页面加载时不会发生此错误,但是当我更改具有 AutoPostBack="true" 的下拉列表的值时。

代码是

    //Number of Services
    numberofServices = Int32.Parse(DCCFunctions.GetNumServicesPerRoom(roomId.ToString()));
    additionalServices = new UserControls_AdditionalService[numberofServices - 1];

    String htmlTable = String.Empty;
    Int32 cell = 1;
    Int32 rows = numberofServices;
    Int32 cols = 4;


    TableHeaderRow h = new TableHeaderRow();
    TableHeaderCell hc1 = new TableHeaderCell();
    hc1.Text = "Item Description";
    h.Cells.Add(hc1);
    TableHeaderCell hc2 = new TableHeaderCell();
    hc2.Text = "Item Price";
    h.Cells.Add(hc2);
    TableHeaderCell hc3 = new TableHeaderCell();
    hc3.Text = "Item Quantity";
    h.Cells.Add(hc3);
    TableHeaderCell hc4 = new TableHeaderCell();
    hc4.Text = "Item Sub Total";
    h.Cells.Add(hc4);
    Table1.Rows.Add(h);

    // Open database connection
    DBConnection conn = new DBConnection();

    // Retrieve details
    SqlCommand sqlGetDetails = conn.SetStoredProcedure("spGetAdditionalServicesDetails");
    DBConnection.AddNewParameter(sqlGetDetails, "@roomId", ParameterDirection.Input, SqlDbType.Int, roomId);

    try
    {
        conn.Open();

        SqlDataReader reader_list = sqlGetDetails.ExecuteReader();
        if (reader_list.HasRows)
        {
            while (reader_list.Read())
            {
                //returnVal = reader_list["Num"].ToString();
                htmlTable += "<tr>" + Environment.NewLine;
                TableRow r = new TableRow();

                additionalServices[cell - 1] = (ASP.usercontrols_additionalservice_ascx)LoadControl("~/UserControls/AdditionalService.ascx");

                Literal ItemCode = (Literal)additionalServices[cell - 1].FindControl("ltlItemCode") as Literal;
                ItemCode.Text = reader_list["itemDescription"].ToString();


                Literal ItemPrice = (Literal)additionalServices[cell - 1].FindControl("ltlItemPrice") as Literal;
                ItemPrice.Text = "€" + reader_list["unitPrice"].ToString();

                Literal ItemTotal = (Literal)additionalServices[cell - 1].FindControl("ltlTotalPrice") as Literal;
                ItemTotal.Text = "€" + "0";

                TableCell ItemCodeCell = new TableCell();
                ItemCodeCell.Controls.Add((Literal)additionalServices[cell - 1].FindControl("ltlItemCode") as Literal);

                TableCell ItemCodePriceCell = new TableCell();
                ItemCodePriceCell.Controls.Add((Literal)additionalServices[cell - 1].FindControl("ltlItemPrice") as Literal);

                TableCell ItemCodeTotalCell = new TableCell();
                ItemCodeTotalCell.Controls.Add((Literal)additionalServices[cell - 1].FindControl("ltlTotalPrice") as Literal);

                TableCell c = new TableCell();
                DropDownList qtyList = (DropDownList)additionalServices[cell - 1].FindControl("qtyList") as DropDownList;
                qtyList.Items.Add(new System.Web.UI.WebControls.ListItem("Select Quantity...", "0"));
                qtyList.DataBind();

                for (Int32 count = 1; count < 101; count++)
                {
                    qtyList.Items.Add(new System.Web.UI.WebControls.ListItem(count.ToString(),count.ToString()));
                }
                //c.ColumnSpan = 5;
                c.Controls.Add((DropDownList)additionalServices[cell - 1].FindControl("qtyList") as DropDownList);


                r.Cells.Add(ItemCodeCell);
                r.Cells.Add(ItemCodePriceCell);
                r.Cells.Add(c);
                r.Cells.Add(ItemCodeTotalCell);
                //r.Controls.Add(additionalServices[cell - 1]);
                //cell += 1;

                // Add the row
                Table1.Rows.Add(r);
            }
        }
        reader_list.Close();
    }
    catch (Exception ex)
    {
        M1Utils.ErrorHandler(ex);
    }
    finally
    {
        conn.Close();
    }`

【问题讨论】:

  • 这方面有进展吗?

标签: asp.net dynamic controls unique


【解决方案1】:

ItemCodeCell.Controls.Add((Literal)additionalServices[cell - 1].FindControl("ltlItemCode") as Literal);

我猜这就是问题所在,看起来您一遍又一遍地添加相同的控件,但我不确定。尝试更改新添加的文字的 .ID。

另外,我以前从未使用过"EnsureID()",但它似乎可能会有所帮助。类似于


Literal duplicateLiteral = (Literal)additionalServices[cell - 1].FindControl("ltlItemCode") as Literal;
duplicateLiteral.EnsureID();
ItemCodeCell.Controls.Add(duplicateLiteral);

这可能行不通,因为它看起来会一遍又一遍地插入相同的副本duplicateLiteral,尽管我不确定。

【讨论】:

  • Ensue ID 的使用语法是什么?
  • 我使用 EnsureID() 编辑了答案以包含一个 sn-p。让我知道这是否有效
【解决方案2】:

我认为 EnsureID 不会解决您的问题,因为它只会为还没有 ID 的控件创建 ID。

正如 Allen 指出的那样,您正在一遍又一遍地添加相同的控件。您需要创建新控件,这将为您提供新的 ID。您还为已引用的控件调用 FindControl...?如果需要,从现有控件中复制数据,但不要重复添加现有控件。对于文字,可能唯一有价值的是文本?因此,将您的代码更改为:

Literal ItemCode = (Literal)additionalServices[cell - 1].FindControl("ltlItemCode") as Literal;
ItemCode.Text = reader_list["itemDescription"].ToString();

Literal ItemCode2 = new Literal();  //create a new control
ItemCode2.Text = ItemCode.Text;     //copy the data you need

TableCell ItemCodeCell = new TableCell();
ItemCodeCell.Controls.Add(ItemCode2);  // add new control

【讨论】:

    【解决方案3】:

    我同意您似乎一遍又一遍地添加相同的控制,这会导致命名冲突。看起来您可能以错误的顺序将控件添加到其他控件的控件集合中。例如,您按以下顺序添加:

    TableHeaderRow h = new TableHeaderRow();    
    TableHeaderCell hc1 = new TableHeaderCell();    
    hc1.Text = "Item Description";    
    h.Cells.Add(hc1);    
    TableHeaderCell hc2 = new TableHeaderCell();    
    hc2.Text = "Item Price";    
    h.Cells.Add(hc2);    
    TableHeaderCell hc3 = new TableHeaderCell();    
    hc3.Text = "Item Quantity";    
    h.Cells.Add(hc3);    
    TableHeaderCell hc4 = new TableHeaderCell();    
    hc4.Text = "Item Sub Total";    
    h.Cells.Add(hc4);    
    Table1.Rows.Add(h);
    

    当你真的应该按这个顺序添加时:

    TableHeaderRow h = new TableHeaderRow();   
    Table1.Rows.Add(h); 
    TableHeaderCell hc1 = new TableHeaderCell();
    h.Cells.Add(hc1);   
    hc1.Text = "Item Description";      
    TableHeaderCell hc2 = new TableHeaderCell();
    h.Cells.Add(hc2);    
    hc2.Text = "Item Price";       
    TableHeaderCell hc3 = new TableHeaderCell();
    h.Cells.Add(hc3);    
    hc3.Text = "Item Quantity";      
    TableHeaderCell hc4 = new TableHeaderCell();
    h.Cells.Add(hc4);    
    hc4.Text = "Item Sub Total";    
    

    作为一项规则,我总是尝试尽快将新控件添加到父控件集合中,以确保任何子控件都继承正确的 UniqueID。如果在将 X 添加到父 Controls 集合之前创建新的 Control X 并开始添加到 X.Controls,则子控件可能不会继承正确的 UniqueID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多