Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:54:17+00:00 2026-06-10T11:54:17+00:00

I need to create an HTML button dynamically in my ASP.NET C# codebehind, I

  • 0

I need to create an HTML button dynamically in my ASP.NET C# codebehind, I must insert this button in my literal control, I use following syntax:

literal.Text+=<button id=\"MyButton\" runat=\"server\" OnServerClick=\"MyButton_Click\">Click Me</Button>";

button is displayed, it causes postback but my server click function (MyButton_Click) is not called, what is going wrong here? I should certainly use a literal, can I use ASP.NET controls in my literal?

I want to call a function after this button causes postback, can I use another objects in my literal? I should certainly use literals

thanks for your answers, I have a lot of codes in my literal, something like this:

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

dynTable += "</td></tr>";
                        dynTable += "<tr><td></td></tr>";
                        dynTable += "<tr><td></td></tr>";
                        dynTable += "<tr><td></td></tr>";
                        dynTable += "<tr><td></td></tr>";

                        dynTable += "<tr><td align=\"left\">";
                        dynTable += "<button id=\"MyButton\" type=\"submit\" runat=\"server\" OnServerClick=\"MyButton_Click\">Foo</button>";
                        dynTable += "</tr></td> </table>  ";
                    }
                }

                //=====================================
                dynTable += "                   </td>";
                dynTable += "               </tr>";
                dynTable += "             </table>";
                dynTable += "           </td>";
                dynTable += "         </tr>";
                dynTable += "       </table>";

                dynTable += "     </td>";
                dynTable += "  </tr>";

            }
            dynTable += "</table>";

        }
        ReviewPlace.Text = dynTable;

ReviewPlace is my asp.net literal object

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:54:19+00:00Added an answer on June 10, 2026 at 11:54 am

    You can’t add Server – Html/Web controls that way. You have to create an object of Control in Page_Init/Page_Load event and add it to PlaceHolder's Controls collection.

    Add PlaceHolder1 control in markup (.aspx)

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

    and write Page_Init handler,

     protected void Page_Init(object sender, EventArgs e)
        {
            System.Web.UI.HtmlControls.HtmlButton button = new System.Web.UI.HtmlControls.HtmlButton();
            button.InnerText = "OK";
            //Event handler - Lambda (anonymous method) expression
            button.ServerClick += (sa, ea) =>
                {
                    Response.Write("button is pressed");
                };
            //Named Event handler
            button.ServerClick += new EventHandler(button_ServerClick);
            PlaceHolder1.Controls.Add(button);
        }
    
     void button_ServerClick(object sender, EventArgs e)
       {
        Response.Write("Another handler");
       }
    

    EDIT: I suggest you to use jQuery/JavaScript – ajax to request server resources instead of handling server-events if you wish/want to generate markup by hand. Alternative solution to this issue is to use Server constrols – especially GridView/ListView and I’m sure these controls and binding mechanism will solve that issue.

    Another possibility is use of Table Server control. Have a look at sample:

    //For ease of use I'v subclass the `TableRow`
     public class Row : TableRow
        {
            public Row(int col)
            {
                for(int i=0;i<col;i++)
                Cells.Add(new TableCell());
            }
            public TableCell this[int index]
            {
                get { return Cells[index]; }
            }
        }
    

    and Page_Init code to populate Table object.

    protected void Page_Init(object sender, EventArgs e)
        {
            Table table = new Table();
    
            Row row1 = new Row(3);
            row1[0].ColumnSpan = 3;
            row1[0].Text = "Search";
            table.Rows.Add(row1);
    
            Row row2 = new Row(3);
            row2[0].Text = "Enter keyword";
            row2[1].Controls.Add(new TextBox());
            table.Rows.Add(row2);
    
            Row row3 = new Row(3);
            row3[0].ColumnSpan = 3;
            Button button = new Button();
            button.Text = "Search";
            button.Click += (sa, ea) => Response.Write("Button is pressed");
            row3[0].Controls.Add(button);
    
            table.Rows.Add(row3);
    
            PlaceHolder1.Controls.Add(table);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create lines like this: <li data-target=c1.html><span>I. </span><span>Het tijdperk der goden</span></li> I
I'm trying to create a generic <input type=button> button in ASP.Net using an ASP.Net
I need to create HTML pop-up menu using Perl. Can anyone help me?
I need to create a log file containing HTML (tables, etc). It seems that
I need to create more than one UIView with MARQUEE effect like HTML <marquee>
I need to understand how to create a simple animation in HTML 5 or
I am developing a Java desktop application. I have a need to create HTML
i need to create html code, in which if user selects an item from
So I have this menu and when I click on a button I need
I need to create a series of dynamically created buttons inside of a repeater.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.