1

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

Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • Have a look at using StringBuilder to replace all the string concatenation - it will improve your apps performance http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx – StuartLC Aug 30 '12 at 04:54

2 Answers2

3

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);
    }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • thanks, can you give me an example of how to do it? I must render my controls in a literal, can I define an ASP.NET control in a literal? – Ali_dotNet Aug 30 '12 at 04:31
  • thanks again, but I have rendered a lot of HTML in my literal, I'm dynamically creating a table and I should add this button in my table rows, how can I render the button in my preferred position? – Ali_dotNet Aug 30 '12 at 04:36
  • I'd suggest to use DataControls like - `GridView`,`DataList`, `ListView` instead of generating table markup. However post *short* and *concise* code so I can help you. – KV Prajapati Aug 30 '12 at 04:39
  • I've inserted some parts of my code in my question, please view it, thanks – Ali_dotNet Aug 30 '12 at 04:50
1

As AVD points out, this isn't a very good idea.

However, if you do decide to continue down this path, you will need to post back by calling the client script function __doPostBack() function with the correct __EVENTTARGET and __EVENTARGUMENT properties set.

This SO post shows a quick and dirty hack workaround here for doing this, by placing a hidden asp:button on the screen, and then changing your code to call the hidden button click.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285