How to generate asp.net control using markup in database

后端 未结 3 758
孤城傲影
孤城傲影 2021-01-23 05:45

How do I generate the controls in a div based on the markup defined in a SQL Server database? Is it possible? If yes, then how? Can anyone give me resources?

&l         


        
3条回答
  •  半阙折子戏
    2021-01-23 06:27

    You could use a placeholder on a page like so:

    
        

    And in your code behind, you could have several checks (all of which contain SQL statements querying the data in the database). The database could contain a field containing component names such as checkbox, button (various other controls). This saves you storing markup in the database fields..

    Very rough representation of how the code would look like..

    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        // SQL Code here
    
        // Datatable code here 
        // End of DataTable code
    
        // Beginning of IF blocks   
            If (DataTable.Rows.Count > 0) {
            If (your check != "checkbox") {
            this.PlaceHolder1.Controls.Add(new Button(){
                Text = "Added"}
                }
              }
         // More if statements here
           );
    
        }
    }
    

    Alternatively if you insist on storing asp markup in the database, then you could just feed the asp markup into the placeholder using a loop (if there are more than one rows in your database)

提交回复
热议问题