Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MY code

ASP.NET
<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
     <asp:GridView ID="gvAll" runat="server" 
    AutoGenerateColumns = "false" Font-Names = "Arial" 
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
    HeaderStyle-BackColor = "green" AllowPaging ="true"   
    OnPageIndexChanging = "OnPaging" PageSize = "10" >
   <Columns>
    <asp:TemplateField>
        <HeaderTemplate>
            <asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" 
            AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged"/>
        </HeaderTemplate> 
        <ItemTemplate>
            <asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)" 
            AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged" />
        </ItemTemplate>
    </asp:TemplateField> 
    <asp:BoundField DataField = "ConId" HeaderText = "Customer ID" 
     HtmlEncode = "false" />
    <asp:BoundField DataField = "Name" HeaderText = "Contact Name" 
    HtmlEncode = "false" />
    <asp:BoundField DataField = "CompanyName" HeaderText = "City" 
    HtmlEncode = "false" />
   </Columns> 
   <AlternatingRowStyle BackColor="#C2D69B"  />
</asp:GridView> 
            <br />
<asp:GridView ID="gvSelected" runat="server" 
AutoGenerateColumns = "false" Font-Names = "Arial" 
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
HeaderStyle-BackColor = "green" EmptyDataText = "No Records Selected"  >
<Columns>
    <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
            <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("ConId") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
            <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Company">
        <ItemTemplate>
            <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("CompanyName") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>


   <%-- <asp:BoundField DataField = "ConId" HeaderText = "Customer ID" />
    <asp:BoundField DataField = "Name" HeaderText = "Contact Name" />
    <asp:BoundField DataField = "CompanyName" HeaderText = "City" />--%>
 </Columns>
</asp:GridView>
        </div>
    </form>


C#
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPrimaryGrid();
            BindSecondaryGrid();
        }
    }

    private void BindPrimaryGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["ConnectionDB"].ConnectionString;
        //string query = "select CustomerID, ContactName, City from customers";
        string query = "select ConId,Name,CompanyName from Contact";
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        gvAll.DataSource = dt;
        gvAll.DataBind();  
    }

protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GetData();
    gvAll.PageIndex = e.NewPageIndex;
    BindPrimaryGrid();
    SetData(); 
}

private void GetData()
{
    DataTable dt;
    if (ViewState["SelectedRecords"] != null)
        dt = (DataTable)ViewState["SelectedRecords"];
    else
        dt = CreateDataTable();
    CheckBox chkAll = (CheckBox)gvAll.HeaderRow
                        .Cells[0].FindControl("chkAll");
    for (int i = 0; i < gvAll.Rows.Count; i++)
    {
        if (chkAll.Checked)
        {
            dt = AddRow(gvAll.Rows[i], dt);
        }
        else
        {
            CheckBox chk = (CheckBox)gvAll.Rows[i]
                            .Cells[0].FindControl("chk");
            if (chk.Checked)
            {
                dt = AddRow(gvAll.Rows[i], dt);
            }
            else
            {
                dt = RemoveRow(gvAll.Rows[i], dt);
            }
        }
    }
    ViewState["SelectedRecords"] = dt;
}

    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gvAll.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        if (ViewState["SelectedRecords"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRecords"];
            for (int i = 0; i < gvAll.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvAll.Rows[i].Cells[0].FindControl("chk");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("ConId = '" + gvAll.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0; 
                    if (!chk.Checked)
                    {
                        chkAll.Checked = false;
                    }
                }
            }
        }
    }

private DataTable CreateDataTable()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ConId");
    dt.Columns.Add("Name");
    dt.Columns.Add("CompanyName");
    dt.AcceptChanges();
    return dt;
}

private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
    DataRow[] dr = dt.Select("ConId = '" + gvRow.Cells[1].Text + "'");
    if (dr.Length <= 0)
    {
        dt.Rows.Add();

        for (int i = 0; i < gvAll.Rows.Count; i++)
        {

            dt.Rows[dt.Rows.Count - 1]["ConId"] = gvRow.Cells[2].Text;
            dt.Rows[dt.Rows.Count - 1]["Name"] = gvRow.Cells[2].Text;
            dt.Rows[dt.Rows.Count - 1]["CompanyName"] = gvRow.Cells[3].Text;
            dt.AcceptChanges();
        }
    }
    return dt;
}

private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
    DataRow[] dr = dt.Select("ConId = '" + gvRow.Cells[1].Text + "'");
    if (dr.Length > 0)
    {
        dt.Rows.Remove(dr[0]);
        dt.AcceptChanges();
    }
    return dt;
}

protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
    GetData();
    SetData();
    BindSecondaryGrid();
}

private void BindSecondaryGrid()
{
    DataTable dt = (DataTable)ViewState["SelectedRecords"];
    gvSelected.DataSource = dt;
    gvSelected.DataBind(); 
}





Error Message on

Multiple controls with the same ID 'txtid' were found. FindControl requires that controls have unique IDs.
Posted
Comments
Thanks7872 28-Sep-15 5:00am    
Why multiple elements have same ids? Id itself means unique.

Error message look pretty clear on what is the problem.

Solution: Use the Search function of your text editor.

You should really learn to use your text editor.
 
Share this answer
 
The error is self-explanatory, you have three txtid controls. Change them to have unique IDs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900