0

I was trying to create "asp:buttons" dynamically with special asp tags (<%%>) as I am using a loop in the codebehind to generate x amount of buttons based on the database. However, when the special tag tries to write out the asp button, the button does not work.

I know that the problem is due to it not converting when it's being written out.

For example, when I view source an asp button is suppose to look like this:

<input type="submit" name="ctl00$ContentPlaceHolder1$Button1" value="Hello" id="ContentPlaceHolder1_Button1" class="button" style="color:White;background-color:Transparent;border-style:None;" />

but the asp button being written on the view source was shown as:

<asp:Button ID='Button1' CssClass='button' BackColor='Transparent' runat='server' BorderStyle='None' ForeColor='white' Text='Hello' OnCommand='Button1_Click' CommandArgument='cat0002' />

If there are other ways to create the buttons please do recommend me too as I have to create them dynamically while trying to display them in the individual table data I created. And I would need to pass the values through to the code behind as I have to call the SQL database when its clicked.

1
  • You can use Button constructor like new Button() { ID = "buttonID", Text = "ButtonText" } and then put button.Click += (sender, e) { // click event here }. Don't forget to add button control into the form afterwards with FormName.Controls.Add. Commented Apr 23, 2018 at 7:51

1 Answer 1

1

try it:

protected void Page_Load()

    {
      Button ButtonChange = new Button();

      ButtonChange.Text = "New Button";
      ButtonChange.ID = "btnNew_" + i.ToString();
      ButtonChange.Font.Size = FontUnit.Point(7);
      ButtonChange.ControlStyle.CssClass = "button";
      ButtonChange.Click += new EventHandler(test);
    }

more info : http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

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

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.