468,549 Members | 2,323 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

Dynamic LinkButton and Click event

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);
}

Nov 17 '05 #1
3 36379
It's ok, i figured it out.
the code works , but only if you create the linkbuttons from the page_load
event.
i was trying to call it from within another event. ... for somereason, it
didnt like it.

"CodeRazor" wrote:
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);
}

Nov 17 '05 #2
hi

remember that teh controls that you create from code are not recreated in
the postback, you have to create them always.

if you want to pass an argument you can use Command instead of OnClick and
then use CommandArgument to store the value you want.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:BF**********************************@microsof t.com...
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);
}

Nov 17 '05 #3
Ah, that explains why it worked when i changed where i put the code.

cheers!
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Assaf Weinberg | last post: by
2 posts views Thread by Grant | last post: by
1 post views Thread by RSB | last post: by
reply views Thread by Demetri | last post: by
6 posts views Thread by Mel | last post: by
1 post views Thread by Jack | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.