oncommand of dynamically generated button

  • Thread starter Matthew MacDonald
  • Start date
M

Matthew MacDonald

Hi all,

I am adding controls to a table dynamically. I have a list that I am putting into the session to keep track of them on postback. What I need to know though, is how to add the oncommand method to the buttons as I create them? For example:

btn.ID = "btnDelete" & list.Count

btn.Text = "Remove"

btn.CommandName = "RemoveSW"

btn.CommandArgument = list.Count

btn.oncommand = "Command"

list.add(btn)

Doesn't work because the oncommand method is protected.

Any thoughts?



Thanks,

Matt
 
P

Prodip Saha

Create the control and attach an event handler to it. The btn_Command method(private) must be predefined in order for the deligate to work.
Button btn=new Button();

btn.ID="id";

btn.Text="Remove";

btn.CommandName = "RemoveSW";

btn.CommandArgument = list.Count;

btn.Command +=new CommandEventHandler(btn_Command);

...................

private void btn_Command(object sender, CommandEventArgs e)

{

//Add your code here

}

Hope this helps.

Prodip



Hi all,

I am adding controls to a table dynamically. I have a list that I am putting into the session to keep track of them on postback. What I need to know though, is how to add the oncommand method to the buttons as I create them? For example:

btn.ID = "btnDelete" & list.Count

btn.Text = "Remove"

btn.CommandName = "RemoveSW"

btn.CommandArgument = list.Count

btn.oncommand = "Command"

list.add(btn)

Doesn't work because the oncommand method is protected.

Any thoughts?



Thanks,

Matt
 
M

Matthew MacDonald

OK that works,
The only problem I'm having now is that after I click the newly created button (whose job is to remove some controls from the table row) I need to refresh the browser manually to see the changes correctly. Is there any way to force the browser to refresh?
Create the control and attach an event handler to it. The btn_Command method(private) must be predefined in order for the deligate to work.
Button btn=new Button();

btn.ID="id";

btn.Text="Remove";

btn.CommandName = "RemoveSW";

btn.CommandArgument = list.Count;

btn.Command +=new CommandEventHandler(btn_Command);

..................

private void btn_Command(object sender, CommandEventArgs e)

{

//Add your code here

}

Hope this helps.

Prodip



Hi all,

I am adding controls to a table dynamically. I have a list that I am putting into the session to keep track of them on postback. What I need to know though, is how to add the oncommand method to the buttons as I create them? For example:

btn.ID = "btnDelete" & list.Count

btn.Text = "Remove"

btn.CommandName = "RemoveSW"

btn.CommandArgument = list.Count

btn.oncommand = "Command"

list.add(btn)

Doesn't work because the oncommand method is protected.

Any thoughts?



Thanks,

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top