Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
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 ado.net 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?

Thanks.
Posted
Updated 9-Jul-13 18:18pm
v2
Comments
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 asp.net 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.

regards
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.

Solution:

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

2. on your asp.net page add literal control

E.g found this on http://stackoverflow.com/questions/16160954/generating-pages-through-a-single-literal-control[^]

XML
<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";

http://forums.asp.net/t/1805930.aspx/1[^]
 
Share this answer
 
Comments
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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="lit" runat="server" ClientIDMode="Static" Mode="Transform">
</div>
</form>
</body>
</html>
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);
_newConnection.Open();
da.Fill(ds);
_newConnection.Close();

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" />
you can read the table data and check the ElementType then use if else
C#
<asp:panel runat="server" id="pnl" xmlns:asp="#unknown">
</asp:panel>

C#
for(int i=0;i<datatable.rows.count;i++)>
{
   if(DataTable.Rows[i]["ElementType"].ToString()=="TextBox")
   {
      TextBox txt=new TextBox();
      pnl.Controls.Add(txt);
   }
   else if(DataTable.Rows[i]["ElementType"].ToString()=="Button")
   {
        Button btn=new Button();
        btn.Text=DataTable.Rows[i]["Text"].ToString();
        btn.ID=DataTable.Rows[i]["ID"].ToString();
        pnl.Controls.Add(btn);
   }
 ........
.........
}
 
Share this answer
 
Comments
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?
you can do arrange it in table structure like this (Quite Easy)
C#
<asp:panel id="pnl" runat="server" xmlns:asp="#unknown" />


C#
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();
   if(DataTable.Rows[i]["ElementType"].ToString()=="TextBox")
   {
      TextBox txt=new TextBox();
      ctrlCell.Controls.Add(txt);
      lblCell.InnerHtml=DataTable.Rows[i]["LabelText"].ToString();
   }
   else if(DataTable.Rows[i]["ElementType"].ToString()=="Button")
   {
        Button btn=new Button();
        ctrlCell.Controls.Add(btn);
        lblCell.InnerHtml="";
   }
 ........
.........
hrow.Cells.Add(lblCell);
hrow.Cells.Add(ctrlCell);
htbl.Rows.Add(hrow);
}
pnl.Controls.Add(htbl);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900