G
Guest
I am creating an aspx page using C# and would like to be able to dynamically
create linkbuttons that all run the same fuction on the click event. However,
I would like the function to accept a single argument (in this case the file
name) to identify which button was clicked. I am using the Command event
handler.
The link buttons load correctly, but when i click on a link, the event
handler code does not execute.
Any ideas?
cheers.
DirectoryInfo dir = new DirectoryInfo(C://temp);
FileInfo[] files = dir.GetFiles();
HtmlTable table = new HtmlTable();
HtmlTableRow tableRow;
LinkButton lbtn;
foreach(FileInfo file in files)
{
table.Rows.Add((tableRow = new HtmlTableRow()));
//Create delete cell
tableRow.Cells.Add(tableCell = new HtmlTableCell());
lbtn= new LinkButton();
lbtn.ID = file.Name;
lbtn.Text = "Delete";
lbtn.Command += new CommandEventHandler(OnDelete);
lbtn.CommandName = file.Name;
lbtn.CommandArgument = file.Name;
tableCell.Controls.Add(lbtn);
}
//The event handler
private void OnDelete(object sender, CommandEventArgs e)
{
string fileToDelete = e.CommandArgument.ToString();
Response.Write(fileToDelete);
}
create linkbuttons that all run the same fuction on the click event. However,
I would like the function to accept a single argument (in this case the file
name) to identify which button was clicked. I am using the Command event
handler.
The link buttons load correctly, but when i click on a link, the event
handler code does not execute.
Any ideas?
cheers.
DirectoryInfo dir = new DirectoryInfo(C://temp);
FileInfo[] files = dir.GetFiles();
HtmlTable table = new HtmlTable();
HtmlTableRow tableRow;
LinkButton lbtn;
foreach(FileInfo file in files)
{
table.Rows.Add((tableRow = new HtmlTableRow()));
//Create delete cell
tableRow.Cells.Add(tableCell = new HtmlTableCell());
lbtn= new LinkButton();
lbtn.ID = file.Name;
lbtn.Text = "Delete";
lbtn.Command += new CommandEventHandler(OnDelete);
lbtn.CommandName = file.Name;
lbtn.CommandArgument = file.Name;
tableCell.Controls.Add(lbtn);
}
//The event handler
private void OnDelete(object sender, CommandEventArgs e)
{
string fileToDelete = e.CommandArgument.ToString();
Response.Write(fileToDelete);
}