ASP.NET找到多个具有相同ID“ x”的控件。 FindControl [英] ASP.NET Multiple controls with the same ID 'x' were found. FindControl

查看:87
本文介绍了ASP.NET找到多个具有相同ID“ x”的控件。 FindControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到以下错误



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



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

代码为

  //服务数
numberofServices = Int32.Parse(DCCFunctions.GetNumServicesPerRoom(roomId.ToString()));
AdditionalServices =新的UserControls_AdditionalService [numberofServices-1];

字符串htmlTable = String.Empty;
Int32单元格= 1;
Int32行= numberofServices;
Int32 cols = 4;


TableHeaderRow h = new TableHeaderRow();
TableHeaderCell hc1 = new TableHeaderCell();
hc1.Text =商品说明;
h.Cells.Add(hc1);
TableHeaderCell hc2 = new TableHeaderCell();
hc2.Text =商品价格;
h.Cells.Add(hc2);
TableHeaderCell hc3 = new TableHeaderCell();
hc3.Text =物品数量;
h.Cells.Add(hc3);
TableHeaderCell hc4 = new TableHeaderCell();
hc4.Text =商品总计;
h.Cells.Add(hc4);
Table1.Rows.Add(h);

//打开数据库连接
DBConnection conn = new DBConnection();

//检索详细信息
SqlCommand sqlGetDetails = conn.SetStoredProcedure( spGetAdditionalServicesDetails);
DBConnection.AddNewParameter(sqlGetDetails, @roomId,ParameterDirection.Input,SqlDbType.Int,roomId);

试试
{
conn.Open();

SqlDataReader reader_list = sqlGetDetails.ExecuteReader();
if(reader_list.HasRows)
{
而(reader_list.Read())
{
// returnVal = reader_list [ Num]。ToString() ;
htmlTable + =& lt; tr> + Environment.NewLine;
TableRow r =新的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 =& euro; + reader_list [ unitPrice]。ToString();

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

TableCell ItemCodeCell = new TableCell();
ItemCodeCell.Controls.Add((Literal)additionalServices [cell-1] .FindControl( ltlItemCode)以文字形式表示);

TableCell ItemCodePriceCell = new TableCell();
ItemCodePriceCell.Controls.Add((Literal)additionalServices [cell-1] .FindControl( ltlItemPrice)以文字形式表示);

TableCell ItemCodeTotalCell = new TableCell();
ItemCodeTotalCell.Controls.Add((Literal)additionalServices [cell-1] .FindControl( ltlTotalPrice)以文字形式表示);

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]);
//单元格+ = 1;

//添加行
Table1.Rows.Add(r);
}
}
reader_list.Close();
}
catch(异常例外)
{
M1Utils.ErrorHandler(ex);
}
最终
{
conn.Close();
}`


解决方案

我同意就像您要一遍又一遍地添加相同的控件,从而导致命名冲突。看起来您似乎是在以错误的顺序将控件添加到其他控件的控件集合中。例如,您按以下顺序添加:

  TableHeaderRow h = new TableHeaderRow(); 
TableHeaderCell hc1 = new TableHeaderCell();
hc1.Text =商品说明;
h.Cells.Add(hc1);
TableHeaderCell hc2 = new TableHeaderCell();
hc2.Text =商品价格;
h.Cells.Add(hc2);
TableHeaderCell hc3 = new TableHeaderCell();
hc3.Text =物品数量;
h.Cells.Add(hc3);
TableHeaderCell hc4 = new TableHeaderCell();
hc4.Text =商品总计;
h.Cells.Add(hc4);
Table1.Rows.Add(h);

何时应该真正按以下顺序添加:

  TableHeaderRow h =新的TableHeaderRow(); 
Table1.Rows.Add(h);
TableHeaderCell hc1 = new TableHeaderCell();
h.Cells.Add(hc1);
hc1.Text =商品说明;
TableHeaderCell hc2 = new TableHeaderCell();
h.Cells.Add(hc2);
hc2.Text =商品价格;
TableHeaderCell hc3 = new TableHeaderCell();
h.Cells.Add(hc3);
hc3.Text =物品数量;
TableHeaderCell hc4 = new TableHeaderCell();
h.Cells.Add(hc4);
hc4.Text =商品总计;

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


Getting the following error

Multiple controls with the same ID 'ltlItemCode' were found. FindControl requires that controls have unique IDs.

This Error does not happen on page loads but when I change the value of a drop down which has AutoPostBack="true".

Code is

    //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 += "&lt;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 = "&euro;" + reader_list["unitPrice"].ToString();

                Literal ItemTotal = (Literal)additionalServices[cell - 1].FindControl("ltlTotalPrice") as Literal;
                ItemTotal.Text = "&euro;" + "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();
    }`

解决方案

I agree that it looks like you are adding the same Control over and over which causes a naming conflict. It also looks like you may be adding controls to other controls' Controls collections in the wrong order. For example, you are adding in this order:

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

When you should really be adding in this order:

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

As a rule, I always try to add a new control to the parent Controls collection as soon as possible to ensure that any child controls inherit the correct UniqueID. If you create a new Control X and start adding to X.Controls BEFORE adding X to the parent Controls collection then the child controls may not inherit the right UniqueID.

这篇关于ASP.NET找到多个具有相同ID“ x”的控件。 FindControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆