5

收到以下错误

找到具有相同 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();
    }`
4

3 回答 3

3

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

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

于 2009-02-16T14:39:30.333 回答
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。

于 2009-02-21T20:51:23.783 回答
1

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

正如艾伦指出的那样,您一遍又一遍地添加相同的控制。您需要创建新控件,这将为您提供新的 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
于 2009-02-17T10:34:59.633 回答