Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi this is my code for creating dynamic link button in Panel. My problem is it is creating two dynamic link button instead of one. Kindly Help.
C#
con.Open();
using (SqlCommand comm = new SqlCommand("select [View] from Role1 where Role_Name=@Role and Pages=@Pages", con))
{
    string Role = (string)(Session["Role"]);
    // 2. define parameters used in command object
    SqlParameter para = null;
    para = new SqlParameter();
    para.ParameterName = "@Role";
    para.Value = Role;
    comm.Parameters.Add(para);

    //Pass @Pages parameter
    para = new SqlParameter();
    para.ParameterName = "@Pages";
    para.Value = "Checkout";
    comm.Parameters.Add(para);
    SqlDataReader reade = comm.ExecuteReader();

    while (reade.Read())
    {
        Session["View"] = Convert.ToString(reade["View"]);
    }
    //Button Add = (Button)PreviousPage.FindControl("Button2");
    if (Session["View"].ToString() == "Y")
    {

        LinkButton la=new LinkButton();
        la.Text = "Checkout";

        //a.ID="link1";     
        la.Click += new System.EventHandler(LinkButtonTest_Click);
        Panel1.Controls.Add(la);
Panel1.Controls.Add(new LiteralControl("<br />"));
      //tr2.Visible = true;
    }
    else
Posted
v2
Comments
Did you debug? There must be something else which is also creating one LinkButton. You can analyze the source HTML after the page is rendered. You can assign ID to the LinkButton while it is created dynamically.

So, on rendered HTML, just see what are the IDs of those LinkButtons. That would give you the hint.
Member 10578683 28-Apr-14 4:49am    
When i am assigning ID then it is showing error
Multiple controls with the same ID 'link1' were found. FindControl requires that controls have unique IDs.
But i have created one ID link1 and adding one control to it
Paulo Augusto Kunzel 28-Apr-14 14:43pm    
Does that happen on the first time you create it or after the first one is created?
There must be something else with that Id. Try to change its name to anything else and check if it will happen again.
You could try to keep these links in a array stored in session, this way it would be easier to keep track of them and their Id.
Er Daljeet Singh 28-Apr-14 6:49am    
Comment your If condition and check whether your application is creating linkbutton or not if not then debug your code again.

1 solution

XML
Please make some change in your code according to below code block...

Try this:

<pre lang="c#">

    while (reade.Read())
    {
        Session["View"] = Convert.ToString(reade["View"]);

        //Button Add = (Button)PreviousPage.FindControl("Button2");
        if (Session["View"].ToString() == "Y")
        {

            LinkButton la=new LinkButton();
            la.Text = "Checkout";

            //a.ID="link1";
            la.Click += new System.EventHandler(LinkButtonTest_Click);
            Panel1.Controls.Add(la);
            Panel1.Controls.Add(new LiteralControl("<br />"));
            //tr2.Visible = true;
        }
        else .....

    }


Instead of this:

C#
while (reade.Read())
{
    Session["View"] = Convert.ToString(reade["View"]);
}
//Button Add = (Button)PreviousPage.FindControl("Button2");
if (Session["View"].ToString() == "Y")
{

    LinkButton la=new LinkButton();
    la.Text = "Checkout";

    //a.ID="link1";
    la.Click += new System.EventHandler(LinkButtonTest_Click);
    Panel1.Controls.Add(la);
    Panel1.Controls.Add(new LiteralControl("<br />"));
    //tr2.Visible = true;
}
else .....




I think it's work fine for you because "While" loop read multiple value from database then generate multiple control for you. If you write control generation code out side of "While" loop ti's generate only one control for you.
 
Share this answer
 
v6

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