NoviceToDotNet
NoviceToDotNet

Reputation: 10805

How to write asp button control at run time?

I am wring a button control like below. i have got a method that has got a little code like below i mentioned. i tried it in two ways but both don't work. onw with btn object, second with direct writing it. i need to create all three buttons on run times for N number of items, ans all buttons will have some common things except there identity. Please suggest me a little how to make it work.

 Button btn = new Button();
            btn.ID = "btnDress" + x;
            btn.CommandName = "DressBtn";
            btn.Text = "Dressing";
            btn.CommandArgument = x.ToString();
            btn.UseSubmitBehavior = true;
            btn.Click += new EventHandler(btnDress_Click);

void draw()
{
 drawItem = drawItem + @" <div class=new_options>
                    <ul>
                        <li>
                            "+btn+ @"</li>
                        <li>
                            <asp:Button ID='btnSpInst_'" + x + @" runat='server' CommandName='SpeInstBtn' Text='Special Instruction'
                                OnClick='btnDress_Click' CommandArgument='" + x + @"' /></li>
                        <li>
                            <asp:Button ID='btnTopp_'" + x + @" runat='server' CommandName='ToppBtn' Text='Toppings' OnClick='btnDress_Click' CommandArgument='" + x + @"' /></li>
                    </ul>
                    <div class='clear'>
                    </div>";

            x++;
}

the problem with this code is that for ever div i need to write 3 buttons on fly which has got some fix parameters, like i mentioned for the asp buttons which i tried to farm but those write as an string and don't work, could some body tell me how can i achieve the above thing to work.

this is the html which i need to redraw for items. on fly, here i have kept button but in reality i want to generate it on fly.

 <div class="new_options">
                    <ul>
                        <li>
                            <asp:Button ID="Button4" runat="server"   Visible="true" CommandName="DressBtn" Text="Dressing" OnClick="btnDress_Click" /></li>
                        <li>
                            <asp:Button ID="Button5" runat="server" Visible="true" CommandName="SpeInstBtn" Text="Special Instruction"
                                OnClick="btnDress_Click" /></li>
                        <li>
                            <asp:Button ID="Button6" runat="server" CommandName="ToppBtn" Visible="true" Text="Toppings" OnClick="btnDress_Click" /></li>
                    </ul>
                    <div class="clear">
                    </div>
                </div>

Upvotes: 0

Views: 1850

Answers (2)

Induster
Induster

Reputation: 733

Do this from the code behind. Build the control like you started to here, and then choose somewhere on your page to place it into... something like someContainerControl.Controls.Add(MyNewButton);

Upvotes: 1

Mubarek
Mubarek

Reputation: 2689

You cannot create server controls this way. Create them using code (you already did) and add them to the form. You can use placeholder also.

  Button btn = new Button();
        btn.ID = "btnDress" + x;
        btn.CommandName = "DressBtn";
        btn.Text = "Dressing";
        btn.CommandArgument = x.ToString();
        btn.UseSubmitBehavior = true;
        btn.Click += new EventHandler(btnDress_Click);
    form1.Controls.Add(btn);

Upvotes: 1

Related Questions