2017-05-07 82 views
0

我在用戶控件中有一些文本框,我在Web窗體中使用。單擊用戶控件上的添加按鈕時,相應的部分隱藏在用戶控件中。 現在的問題是,單擊「添加」按鈕時,文本框只會添加幾次。隨後的點擊返回重複的ID錯誤。不知道如何解決這個問題。這裏是Webform,用戶控件和.ascx.cs文件。在用戶控件中動態添加控件拋出重複的ID錯誤

錯誤:

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> 

UserControl.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> 

Usercontrol.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); 
      } 
     } 
    } 
} 

回答

0

嘗試將文本框的名稱附加到您的ID。

例如

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

看問題是否仍然存在。

+0

我相信這個錯誤是由於Rand發生器在多次調用時生成相同的ID。如果是這樣的話,追加相同的文本將不會工作,但我會試一試 – Programmerzzz