0

I'm trying to add a LinkButton dynamically

This is the html code:

         <div id="resultDivText" runat="server">
            <asp:PlaceHolder ID="PlaceHolder1" runat="server">

            </asp:PlaceHolder>
        </div>

This is the c# code

            LinkButton lb = new LinkButton();
            lb.Text = songName + "</br>"; //LinkButton Text
            lb.ID = song.Key.ToString(); // LinkButton ID’s
            lb.CommandArgument = Convert.ToString(song.Key); 
            lb.CommandName = Convert.ToString(song.Key); 
            lb.Click += new EventHandler(test_Click);                      
            this.form1.Controls.Add(lb);
            PlaceHolder1.Controls.Add(lb);

And this is the "test_Click" function

    protected void test_Click(object sender, EventArgs e)
    {
        showAllSong("let it be");
    }

When i run the code it's show me the linkButton list but when i click on it nothing happens.

4 Answers 4

2

Try this code ..

    static bool enable = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DynamicButton();
        }
        else if (enable)
        {
            DynamicButton();
        }
    }
    protected void btnBindMapping_Click(object sender, EventArgs e)
    {
        enable = true;
        DynamicButton();
    }
    protected void DynamicButton()
    {
        LinkButton lb = new LinkButton();
        lb = new LinkButton();
        lb.Text = songName + "</br>"; //LinkButton Text
        lb.ID = song.Key.ToString(); // LinkButton ID’s
        lb.CommandArgument = Convert.ToString(song.Key);
        lb.CommandName = Convert.ToString(song.Key);
        lb.Click += new EventHandler(test_Click);
        this.form1.Controls.Add(lb);
        PlaceHolder1.Controls.Add(lb);
    }
    protected void test_Click(object sender, EventArgs e)
    {
        Response.Write("<script>alert('done'); </script>");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

ok when call the DynamicButton function i send also a Dictionary when i put the Dictionary static it solve the problem
0

You code is fine ..it works as expected ..But change this line

this.form1.Controls.Add(lb);

to

this.Controls.Add(lb);

Note:It postback to your server not a client sided event

Comments

0

Since you use CommandName and CommandArgument in the LinkButton, you have to delegate a Command not a Click.

lb.Command += new CommandEventHandler(test_Click);

protected void test_Click(object sender, CommandEventArgs e)
{
    Response.Write(e.CommandArgument + "<br>" + e.CommandName);
}

And remove the line this.form1.Controls.Add(lb);

And this is not clear from your question, but you have to create that button every time the page is loaded, and that includes a PostBack.

Comments

0

In give detail you have not mention. when this dynamic button is created.

Because that may create a issue.

If you call your c# code in page load event then it works.

        LinkButton lb = new LinkButton();
        lb = new LinkButton();
        lb.Text = songName + "</br>"; //LinkButton Text
        lb.ID = song.Key.ToString(); // LinkButton ID’s
        lb.CommandArgument = Convert.ToString(song.Key); 
        lb.CommandName = Convert.ToString(song.Key); 
        lb.Click += new EventHandler(test_Click);                      
        this.form1.Controls.Add(lb);
        PlaceHolder1.Controls.Add(lb);

if that button create dynamically on oninit or onload event then it works.

2 Comments

i'm actually create this linkButton on a OnClick event
You are creating linkbutton dynamically and it is adding when you click on any button, but when you click on you dyanmically create button . It fire a postback event and after postback it deleteing the control from the page. So, you must add the control in "page_load" again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.