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
You can’t add Server – Html/Web controls that way. You have to create an object of Control in
Page_Init/Page_Loadevent and add it toPlaceHolder'sControls collection.Add PlaceHolder1 control in markup (.aspx)
and write
Page_Inithandler,EDIT: I suggest you to use
jQuery/JavaScript–ajaxto request server resources instead of handling server-events if you wish/want to generate markup by hand. Alternative solution to this issue is to useServerconstrols – especially GridView/ListView and I’m sure these controls and binding mechanism will solve that issue.Another possibility is use of
TableServer control. Have a look at sample:and Page_Init code to populate
Tableobject.