找到了具有相同ID“lebelId1”的多个控件。 FindControl要求控件具有唯一ID。 [英] Multiple controls with the same ID 'lebelId1' were found. FindControl requires that controls have unique IDs.

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

问题描述

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

 使用系统; 
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;
使用 ControlMethods_BL;
使用 CheckList_DAL;

命名空间 CheckList
{
public partial class 主页:System.Web.UI.Page
{
static int number = 2 ;
ControlMethod controlMethod = new ControlMethod();
string strConn = connection.cn;
SqlConnection conn = new SqlConnection();
SqlCommand cmd;
// UserRegistrationBL registration = new UserRegistrationBL();
TaskMaster taskMaster = < span class =code-keyword> new TaskMaster();
SqlDataReader rdr;
受保护 void Page_Load( object sender,EventArgs e)
{
DataReaderMethod();
}

private void DataReaderMethod()
{
conn.ConnectionString = strConn;
string strQuery = 选择S.Id ,G.GMName,T.CheckPointTaskType,T.CheckRemark,来自tblTask​​MasterStatus S的S.TaskDate,tblTask​​Master T,tblGeneralMaster G其中S.TaskId = T.Id和T.ServerId = G.ID;

cmd = new SqlCommand(strQuery,conn);
尝试
{
conn.Open();
rdr = cmd.ExecuteReader();

while (rdr.Read())
{
DynamicControlMethod(rdr);
}
}
catch (例外情况)
{
throw new 例外(ex.Message);
}

最后
{
conn.Close();
}
}

私有 void DynamicControlMethod( SqlDataReader rdr)
{
for int i = 1 ; i<数字模式= hold /> ; {
表t = new 表();
TableRow行= new TableRow();

TableCell cellId = new TableCell();
cellId.Width = Unit.Pixel( 1 );
标签labelID = new Label();
labelID.ID = lebelId + i.ToString();
labelID.Text = rdr [ Id]。ToString();
cellId.Controls.Add(labelID);
row.Cells.Add(cellId);
pnlID.Controls.Add(row);

TableCell cellGMName = new TableCell();
cellId.Width = Unit.Pixel( 70 );
标签lebelGMName = 标签();
lebelGMName.ID = lebelGMName + i.ToString();
lebelGMName.Text = rdr [ GMName]。ToString();
cellGMName.Controls.Add(lebelGMName);
row.Cells.Add(cellGMName);
pnlServerName.Controls.Add(row);

TableCell cellCheckPointTaskType = new TableCell();
cellId.Width = Unit.Percentage( 10 );
Label lebelCheckPointTaskType = new Label();
lebelCheckPointTaskType.ID = lebelCheckPointTaskType + i.ToString();
lebelCheckPointTaskType.Text = rdr [ CheckPointTaskType]。ToString();
cellCheckPointTaskType.Controls.Add(lebelCheckPointTaskType);
row.Cells.Add(cellCheckPointTaskType);
pnlCheckPointTaskType.Controls.Add(row);

TableCell cellCheckRemark = new TableCell();
cellId.Width = Unit.Percentage( 30 );
标签lebelCheckRemark = 标签();
lebelCheckRemark.ID = lebelCheckRemark + i.ToString();
lebelCheckRemark.Text = rdr [ CheckRemark]。ToString();
cellCheckRemark.Controls.Add(lebelCheckRemark);
row.Cells.Add(cellCheckRemark);
pnlCheckRemark.Controls.Add(row);

TableCell cellSpace = new TableCell();
cellId.Width = Unit.Percentage( 10 );
TextBox textboxSpace = new TextBox();
textboxSpace.ID = textboxSpace + i.ToString();
cellSpace.Controls.Add(textboxSpace);
row.Cells.Add(cellSpace);
pnlSpace.Controls.Add(row);

TableCell cellComment = new TableCell();
cellId.Width = Unit.Percentage( 5 );
CheckBox checkBoxComment = new CheckBox();
checkBoxComment.ID = checkBoxComment + i.ToString();
cellComment.Controls.Add(checkBoxComment);
row.Cells.Add(cellComment);
pnlCheck.Controls.Add(row);

TableCell cellRemarks = new TableCell();
cellId.Width = Unit.Percentage( 10 );
TextBox remarkTextBox = new TextBox();
remarkTextBox.ID = remarkTextBox + i.ToString();
cellRemarks.Controls.Add(remarkTextBox);
row.Cells.Add(cellRemarks);
pnlRemarks.Controls.Add(row);
}
}

public void btnSubmit_Click( object sender,EventArgs e)
{

string strCon = ConfigurationManager.ConnectionStrings [ RealWorldConnectionString]。ConnectionString;
SqlConnection con = new SqlConnection(strCon);

for int i = 0 ; i < 1 ; i ++)
{
string SpaceData = textboxSpace + i.ToString();
TextBox txt =(TextBox)pnlSpace.FindControl(SpaceData);
}
}
}
}



请帮助我无法将数据插入数据库。

解决方案

错误非常明确:

找到了具有相同IDlebelId1的多个控件.DindControl要求控件具有唯一ID 。



因此,不是命名所有控件,而是添加相同的内容:

标签labelID = 标签(); 
labelID.ID = lebelId + i.ToString();

创建一个类级整数,在开始从数据库读取之前将其设置为1:

 labelNumber =  1 ; 
while (rdr.Read())
{
DynamicControlMethod(rdr);
}

并在您的标签构造代码中使用它:

标签labelID = 标签(); 
labelID.ID = lebelId + labelNumber ++。ToString();


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

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using ControlMethods_BL;
using CheckList_DAL;

namespace CheckList
{
    public partial class Home : System.Web.UI.Page
    {
        static int number = 2;
        ControlMethod controlMethod = new ControlMethod();
        string strConn = connection.cn;
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd;
        //UserRegistrationBL registration = new UserRegistrationBL();
        TaskMaster taskMaster = new TaskMaster();
        SqlDataReader rdr;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataReaderMethod();
        }

        private void DataReaderMethod()
        {
            conn.ConnectionString = strConn;
            string strQuery = "select S.Id, G.GMName, T.CheckPointTaskType, T.CheckRemark, S.TaskDate from tblTaskMasterStatus S, tblTaskMaster T, tblGeneralMaster G where S.TaskId=T.Id and T.ServerId=G.ID";

            cmd = new SqlCommand(strQuery, conn);
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {                  
                        DynamicControlMethod(rdr);                   
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                conn.Close();
            }
        }

        private void DynamicControlMethod(SqlDataReader rdr)
        {                  
                for (int i = 1; i <number mode="hold" />                {                    
                    Table t = new Table();
                    TableRow row = new TableRow();

                    TableCell cellId = new TableCell();                   
                    cellId.Width = Unit.Pixel(1);
                    Label labelID = new Label();
                    labelID.ID = "lebelId" + i.ToString();
                    labelID.Text = rdr["Id"].ToString();
                    cellId.Controls.Add(labelID);
                    row.Cells.Add(cellId);                   
                    pnlID.Controls.Add(row);

                    TableCell cellGMName = new TableCell();
                    cellId.Width = Unit.Pixel(70);
                    Label lebelGMName = new Label();
                    lebelGMName.ID = "lebelGMName" + i.ToString();
                    lebelGMName.Text = rdr["GMName"].ToString();
                    cellGMName.Controls.Add(lebelGMName);
                    row.Cells.Add(cellGMName);                    
                    pnlServerName.Controls.Add(row);

                    TableCell cellCheckPointTaskType = new TableCell();
                    cellId.Width = Unit.Percentage(10);
                    Label lebelCheckPointTaskType = new Label();
                    lebelCheckPointTaskType.ID = "lebelCheckPointTaskType" + i.ToString();
                    lebelCheckPointTaskType.Text = rdr["CheckPointTaskType"].ToString();
                    cellCheckPointTaskType.Controls.Add(lebelCheckPointTaskType);
                    row.Cells.Add(cellCheckPointTaskType);                    
                    pnlCheckPointTaskType.Controls.Add(row);

                    TableCell cellCheckRemark = new TableCell();
                    cellId.Width = Unit.Percentage(30);
                    Label lebelCheckRemark = new Label();
                    lebelCheckRemark.ID = "lebelCheckRemark" + i.ToString();
                    lebelCheckRemark.Text = rdr["CheckRemark"].ToString();
                    cellCheckRemark.Controls.Add(lebelCheckRemark);
                    row.Cells.Add(cellCheckRemark);                   
                    pnlCheckRemark.Controls.Add(row);
                   
                    TableCell cellSpace = new TableCell();
                    cellId.Width = Unit.Percentage(10);
                    TextBox textboxSpace = new TextBox();
                    textboxSpace.ID = "textboxSpace" + i.ToString();
                    cellSpace.Controls.Add(textboxSpace);
                    row.Cells.Add(cellSpace);                    
                    pnlSpace.Controls.Add(row);

                    TableCell cellComment = new TableCell();
                    cellId.Width = Unit.Percentage(5);
                    CheckBox checkBoxComment = new CheckBox();
                    checkBoxComment.ID = "checkBoxComment" + i.ToString();
                    cellComment.Controls.Add(checkBoxComment);
                    row.Cells.Add(cellComment);                   
                    pnlCheck.Controls.Add(row);

                    TableCell cellRemarks = new TableCell();
                    cellId.Width = Unit.Percentage(10);
                    TextBox remarkTextBox = new TextBox();
                    remarkTextBox.ID = "remarkTextBox" + i.ToString();
                    cellRemarks.Controls.Add(remarkTextBox);
                    row.Cells.Add(cellRemarks);                   
                    pnlRemarks.Controls.Add(row);                
                }            
        }

        public void btnSubmit_Click(object sender, EventArgs e)
        {

            string strCon = ConfigurationManager.ConnectionStrings["RealWorldConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strCon);

            for (int i = 0; i < 1; i++)
            {
                string SpaceData = "textboxSpace" + i.ToString();
                TextBox txt = (TextBox)pnlSpace.FindControl(SpaceData);          
            }               
        }
    }
}


Kindly help I am unbale to insert data into database.

解决方案

The error is pretty explicit:
"Multiple controls with the same ID 'lebelId1' were found. FindControl requires that controls have unique IDs."

So instead of naming all the controls you add the same:

Label labelID = new Label();
labelID.ID = "lebelId" + i.ToString();

Create a class level integer, set it to one before you start reading from your DB:

labelNumber = 1;
while (rdr.Read())
   {
   DynamicControlMethod(rdr);
   }

And use that in your label construction code:

Label labelID = new Label();
labelID.ID = "lebelId" + labelNumber++.ToString();


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

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