2017-05-07 10 views
0

私は、Webフォームで使用しているユーザーコントロールにいくつかのテキストボックスがあります。ユーザーコントロールの[追加]ボタンをクリックすると、対応するセクションがユーザーコントロールに表示されなくなります。 問題は、テキストボックスが追加ボタンをクリックして数回だけ追加されることです。後続のクリックで重複IDエラーが返されます。これを解決する方法は不明です。ここにWebform、Userコントロール、.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) 

Webフォーム:

<%@ 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

関連する問題