2

I'm listing the contents of my basket by adding fresh HTML to a panel so it lists it on the aspx page. I'm also trying to add an asp:button at the end which will allow users to remove the items, however the button isnt showing!

Code;

string sHTML = @"<div class='item_bar'>
                   <div class='item_id'>" + id + @"</div>
                   <div class='item_title'>" + name + @"</div>
                   <div class='item_price'>" + cost + @"</div>
                   <asp:Button class='button' Text='Remove' runat='server' CommandArgument='"+name+@"' OnClick='removeItem' />
                 </div>";

basketDiv.Controls.Add(new LiteralControl(sHTML));

thanks

1
  • 'asp:button` button is a control itself and we can not use it like there, you can use the HTML control for that, <input type='submit' value='remove'/> Commented Apr 15, 2014 at 11:07

2 Answers 2

2

If you are going to add controls such as the ASP:BUTTON, you would need to add them not via string but actually doing it

        this.pnlFrame.Controls.Add(new Button() { 
      ID = "buttonId1",
       Text = "Text for new button"
    });

Keep in mind that this needs to be registered when the page is actually being compiled so that is why it is not working in your case.

Check this link also Adding button to panel dynamically and getting it's parent ID

If you do not want events being captured at runtime, and would like to do the actions via jquery, you can add it using string like you are doing, and add a normal HTML input with type submit

 <input type='submit' value='New button'/>

However then when clicked, you need to create scripts on the client side to capture those events.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers dude helped a lot, as did the other reply. I've managed to add a button succesfully and it responds to the onlick event also!
1

Create button and then set the properties u want then add in panel

     // add the litenal control - DIVs
     basketDiv.Controls.Add(new LiteralControl("<div class='item_bar'><div class='item_id'>" + id + "</div><div class='item_title'>" + name + "</div><div class='item_price'>" + cost + "</div>"));

    // create button and Add it to basketDiv
    Button button = new Button();
    button.Name = "Button1";

    // you can added other attribute here.
    button.Text = "New Button";
    button.Location = new Point(70,70);
    button.Size = new Size(100, 100);        
    basketDiv.Controls.Add(button);


     // close the parent Liternal "DIV class='item_bar'"
    basketDiv.Controls.Add(new LiteralControl("</div>"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.