0

The following button provides the exact functionality I require, but it must be done programmatically as I will be making more than one based on some variables, but the latter I can figure out if I can just create the button below as a starter:

<asp:Button runat="server" OnCommand="Load_Items" CommandArgument="1" text="Submit" />

More information on what I've been doing here.

Edit:

I have looked at questions like this, but don't see where to go from Button btnSave = new Button(); to seeing a button on the page. Also, most questions I've found seem to go about using C# to handle clicking, but I want to have an OnCommand property to handle it.

If I do the following and search using the developer tools for "btn", I get no results.

Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.CssClass = "btn";
Community
  • 1
  • 1
dudledok
  • 2,800
  • 5
  • 24
  • 36
  • So, what search terms have you used so far to research this? – Garrison Neely Jul 26 '13 at 16:17
  • You have the "this is what I want" part, but what about the "this is what I have done so far" part? And why can't you just make a button in code? – gunr2171 Jul 26 '13 at 16:18
  • I can't make a button because I don't know how, hence my question. I have edited the OP to add some clarification on previous searches and how this is different. – dudledok Jul 26 '13 at 16:27

2 Answers2

11

Here is how you create a Button with Command event.

Once it is created, you need to find a way of displaying it. The following example uses PlaceHolder; you can also substitute with Panel if you want.

ASPX

<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        var button = new Button
        {
            ID = "Button" + i,
            CommandArgument = i.ToString(),
            Text = "Submit" + i
        };
        button.Command += Load_Items;
        PlaceHolder1.Controls.Add(button);
    }
}

private void Load_Items(object sender, CommandEventArgs e)
{
    int id = Convert.ToInt32(e.CommandArgument);
    // Do something with id
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • +1 for giving the complete answer. – Kai Hartmann Jul 26 '13 at 17:33
  • Thanks for the complete answer, and it does indeed work, the only issue is this assumes the number of buttons is known, but the reason I am creating them programmatically is that it may vary. Any tips? – dudledok Jul 29 '13 at 09:53
  • I tried programmatically adding placeholders based on the number of buttons and then replacing those but it sounds horrible and doesn't work. – dudledok Jul 29 '13 at 10:07
  • Just create a panel at desgin time, and add all the programmatically created buttons to this single panel. I don't know, but you should be able to do that even with the placeholder control. Do not replace anything, just add the buttons to it. – Kai Hartmann Jul 29 '13 at 12:57
  • 1
    @dudledok Kai's comment is correct. You need a Panel or a PlaceHolder at design time. (Or you can add controls directly to `form` control, but I do not like that approach.) I updated my answer to display multiple buttons. – Win Jul 29 '13 at 14:11
0

You need a container control on your page. Try an asp panel for example. Then in your codebehind you do panel.controls.add(btnSave); And don't forget to do it on every site load. http://support.microsoft.com/kb/317515/en-us

Kai Hartmann
  • 3,106
  • 1
  • 31
  • 45