Tuesday, April 20, 2010

Dynamically add LinkButton to placeholder

Sometimes you just feel real stupid when you wrote a piece of code which worked and then it didnt 2 months later when you wrote the same thing... here is a small piece of code I wrote to add dynamic linkbuttons to placeholder which call a function when clicked...

LinkButton filterButton = new LinkButton();
filterButton.ID = "SomeUniqueID";
filterButton.Text = "WhateverNeedsDisplayedInHyperlink";
filterButton.Click += (showFile);
filterButton.DataBind();
ShowFileLinks.Controls.Add(filterButton);
ShowFileLinks.Controls.Add(new LiteralControl("
"));

private void showFile(object sender, EventArgs e1)
{
//this is your unique buttonID...do whatever you want with it..Prob query the Database
int btnIndex = Convert.ToInt32(((LinkButton)sender).ID);

}

No comments: