【问题标题】:Adding controls dynamically in User control throwing duplicate ID error在用户控件中动态添加控件引发重复 ID 错误
【发布时间】:2017-05-07 05:50:46
【问题描述】:

我在 Web 表单中使用的用户控件中有几个文本框。当单击用户控件上的添加按钮时,相应的部分将隐藏在用户控件中。 现在的问题是,文本框仅在单击“添加”按钮时添加了几次。后续点击返回重复 ID 错误。不知道如何解决这个问题。这是 Webform、用户控件和 .ascx.cs 文件。

错误:

Multiple controls with the same ID '534011718' were found. FindControl requires that controls have unique IDs.
    at Function.Error$create [as create] (ScriptResource.axd?d=D9drwtSJ4hBA6O8UhT6CQuRknbIyh_6ghEShYes4N6ARI-DqYYcqzd-pVr-FbAymdbS_TdRP62S_zr…:237)

网络表单:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SampleWebApp.WebForm1" %>



<%@ Register Src="~/DataInputUC.ascx" TagPrefix="uc1" TagName="DataInputUC" %>


    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <div>
                Please Enter User Input:
                <uc1:DataInputUC runat="server" id="DataInputUC" />
            </div>
        </form>

    </body>
    </html>

用户控件.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DataInputUC.ascx.cs" Inherits="SampleWebApp.DataInputUC" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:HiddenField ID="numOfTB" runat="server" Value="0" />
        <asp:Table ID="UserCOntrolTable" runat="server">
            <asp:TableRow>
                <asp:TableCell>
                    <asp:Label ID="lbl_field1" runat="server" Text="Name"></asp:Label>
                    <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:Label ID="lbl_PhoneNo" runat="server" Text="Phone No:"></asp:Label>
                    <asp:TextBox ID="txt_phoneNo" runat="server"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="btn_Add_Click" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </ContentTemplate>
</asp:UpdatePanel>

用户控件.ascx.cs:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SampleWebApp
{
    public partial class DataInputUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            TableRow tRow = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
            tRow.Cells.Add(cell1);
            tRow.Cells.Add(cell2);
            tRow.Cells.Add(cell3);
            this.UserCOntrolTable.Rows.Add(tRow);
            Label lbl = new Label();
            lbl.Text = "Name";
            Label lbl2 = new Label();
            lbl2.Text = "Phone No:";
            cell1.Controls.Add(lbl);
            cell2.Controls.Add(lbl2);

            TextBox name = new TextBox();
            Random x = new Random(1);
            name.ID = x.Next().ToString();
            cell1.Controls.Add(name);
            TextBox Phnno = new TextBox();
            Phnno.ID = x.Next().ToString();
            cell2.Controls.Add(Phnno);
            Button btn = new Button();
            btn.Text = "Delete";
            cell3.Controls.Add(btn);

            Panel pan = new Panel();
            pan.Controls.Add(tRow);
            UpdatePanel1.ContentTemplateContainer.Controls.Add(pan);
            this.numOfTB.Value = (Convert.ToInt16(this.numOfTB.Value) + 2).ToString();
        }

       void PopulateControls()
        {
            int tbs = Convert.ToInt16(this.numOfTB.Value);
            for(int i=0;i<tbs/2;i++)
            {
                TableRow tRow = new TableRow();
                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();
                TableCell cell3 = new TableCell();
                tRow.Cells.Add(cell1);
                tRow.Cells.Add(cell2);
                tRow.Cells.Add(cell3);
                this.UserCOntrolTable.Rows.Add(tRow);
                Label lbl = new Label();
                lbl.Text = "Name";
                Label lbl2 = new Label();
                lbl2.Text = "Phone No:";
                cell1.Controls.Add(lbl);
                cell2.Controls.Add(lbl2);

                TextBox name = new TextBox();
                Random x = new Random(1);
                name.ID = x.Next().ToString();
                cell1.Controls.Add(name);
                TextBox Phnno = new TextBox();
                Phnno.ID = x.Next().ToString();
                cell2.Controls.Add(Phnno);
                Button btn = new Button();
                btn.Text = "Delete";
                cell3.Controls.Add(btn);

                Panel pan = new Panel();
                pan.Controls.Add(tRow);
                UpdatePanel1.ContentTemplateContainer.Controls.Add(pan);
            }
        }
    }
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    尝试将文本框的名称附加到您的 id。

    例如

    name.ID = "name_" + x.Next().ToString();
    Phnno.ID = "Phnno_" + x.Next().ToString();
    

    看看问题是否仍然存在。

    【讨论】:

    • 我认为该错误是由于 Rand 生成器在多次调用时生成相同的 id。如果是这种情况,附加相同的文本也行不通,但我会试一试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2017-01-06
    • 2010-09-27
    • 1970-01-01
    相关资源
    最近更新 更多