Click here to Skip to main content
Rate this: bad
good
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 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 9-Jul-13 9:50am
Edited 9-Jul-13 19:18pm
v2
Comments
jkirkerx at 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 at 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 at 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 at 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 at 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 at 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 at 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 at 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
good
Please Sign up or sign in to vote.

Solution 1

Solution:
 
1. fetch the data in the database. example data <html>anything </html>
 
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[^]
 
<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[^]
  Permalink  
Comments
Pratik Gaikwad at 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("Id").Equals(1)
select str.Field("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
good
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">
</asp:panel>
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);
   }
 ........
.........
}
  Permalink  
Comments
Pratik Gaikwad at 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
good
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();
   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);
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 15 Jul 2013
Copyright © CodeProject, 1999-2014
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