Click here to Skip to main content
13,144,309 members (32,245 online)
Rate this:
Please Sign up or sign in to vote.
How do I generate the controls in a div(Main div), the markup of the controls that I want to add in the Main Div is defined in database(SQL-server). I want to read this mark up using and then render the control in the main div.

for eg

in db, i have a table called elementsMarkup :
ElementType Id Runat Text to be displayed css class
Label lbl1 server Test Label Lable
TextBox txt1 server Not Applicable Text

Is it possible? If yes, then how? can anyone give me resources?

Posted 9-Jul-13 8:50am
Updated 9-Jul-13 18:18pm
jkirkerx 9-Jul-13 16:21pm
You should improve your question, and draw a better picture of your scenario.

I understand generating markup, and in a div, but not the latter.
Mico Perez 10-Jul-13 1:32am
Hi Pratik

Just wondering why do you need to generate control on the database?

Is this for display purposes only?

How about the code behind codes?
Pratik Gaikwad 10-Jul-13 2:13am
Hi Mico,

The code behind code will remain same because the controls for which it is written will be static and their id's also will be static. But there are some controls which our business group always like modify their way. And I want to allow them that extension without waiting for another product release date. Instead they will ask us modify the display properties(like label,text,place or verbiage). Start a new session and the work is done. No need to generate new MSI and wait for another release date.
Mico Perez 10-Jul-13 2:20am
if that's the case, you can try to use literal control and pass all the user control markup text to that control.

Pratik Gaikwad 12-Jul-13 15:45pm
But if I used that, will it will other elements render? for example check box, button, radio button list or update panel.
Mico Perez 14-Jul-13 21:49pm
Yes it will the downside of this is that, you must construct your control.

Have you tried it?
Pratik Gaikwad 15-Jul-13 0:58am
Hi Mico, I did not understand your above control... But I will try it tonight. I was waiting for your reply and had some other urgent coding to do. So if you can just explain to me what you meant I will start and will let you know by tomorrow. Also can you give me some links which I can use as reference?
Pratik Gaikwad 15-Jul-13 1:10am
Thanks for quick reply but I want to use ASP controls not html controls. is it possible?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


1. fetch the data in the database. example data anything

2. on your page add literal control

E.g found this on[^]

<pre lang="xml">Dim sHtml As String = _
  &quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;hello world&lt;/body&gt;&lt;/html&gt;&quot;
  litContents.Text = sHtml</pre>

3. This.LiteralControl.text = "data fetch from database";[^]
Pratik Gaikwad 20-Jul-13 1:36am
Hi Mico, I have tried below code, but the problem is the control is not visible on page but appears in view source...

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:Literal ID="lit" runat="server" ClientIDMode="Static" Mode="Transform">
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
public partial class WebForm1 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
SqlConnection _newConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

SqlCommand storeProc = new SqlCommand("sp_getMarkup", _newConnection);
storeProc.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(storeProc);

DataTable dt = ds.Tables["Table"];
string s = (from str in dt.AsEnumerable()
where str.Field<int>("Id").Equals(1)
select str.Field<string>("elemMarkup")).SingleOrDefault().ToString();

this.lit.Text = s;

in the data base I have stored <asp:CheckBox ID="chk" runat="server" ClientIDMode="Static" />
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

you can read the table data and check the ElementType then use if else
<asp:panel runat="server" id="pnl" xmlns:asp="#unknown">

for(int i=0;i<datatable.rows.count;i++)>
      TextBox txt=new TextBox();
   else if(DataTable.Rows[i]["ElementType"].ToString()=="Button")
        Button btn=new Button();
Pratik Gaikwad 15-Jul-13 2:28am
Your solution is much more easy to implement... but what about the positioning of the elements like div or table structure? things that we can not control through external CSS?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

you can do arrange it in table structure like this (Quite Easy)
<asp:panel id="pnl" runat="server" xmlns:asp="#unknown" />

HtmlTable htbl=new HtmlTable();
for(int i=0;i<datatable.rows.count;i++)>
  HtmlTableRow hrow=new HtmlTableRow();
  HtmlTableCell lblCell=new HtmlTableCell();
  HtmlTableCell ctrlCell=new HtmlTableCell();
      TextBox txt=new TextBox();
   else if(DataTable.Rows[i]["ElementType"].ToString()=="Button")
        Button btn=new Button();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 15 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100