Thanks tgreer. Here is my code. I think I tried everything.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DeviceAccess
{
public class GetInfo : System.Web.UI.Page
{
public string device;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.Label lblManager;
protected System.Web.UI.WebControls.TextBox txtmanager;
protected System.Web.UI.WebControls.Label lblitem;
protected System.Web.UI.WebControls.DropDownList dditem;
protected System.Web.UI.WebControls.TextBox txtCompany;
protected System.Web.UI.WebControls.Label lblCompany;
protected System.Web.UI.WebControls.TextBox txtDepartment;
protected System.Web.UI.WebControls.Label lblDepartment;
protected System.Web.UI.WebControls.Button btnPrint;
protected System.Web.UI.WebControls.Label lblName;
public System.Data.SqlClient.SqlConnection dataConn;
protected System.Data.SqlClient.SqlCommand deviceCmd;
protected System.Data.SqlClient.SqlParameter deviceParm;
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Table tblcontrols;
protected System.Web.UI.WebControls.PlaceHolder phControl;
protected System.Data.SqlClient.SqlDataReader deviceReader;
#region PageLoad
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
if (IsPostBack)
{
if (ViewState["mode"].ToString() == "1")
{
CreateComputerControls();
}
}
else
{
ViewState.Add("mode","0");
}
}
catch(Exception X)
{
//Redirect to different page
}
}
#endregion
#region Load View state
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if( ViewState["mode"].ToString() == "1")
{
CreateComputerControls();
}
}
#endregion VLoad View State
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
PlaceHolder phControl =new PlaceHolder();
phControl.Visible=false;
btnPrint.Visible=false;
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dditem.SelectedIndexChanged += new System.EventHandler(this.dditem_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region SelectedIndexChanged
private void dditem_SelectedIndexChanged(object sender, System.EventArgs e)
{
device=dditem.SelectedItem.Value;
if (device=="Computer")
CreateComputerControls();
}
#endregion
#region CreateComputerControls
private void CreateComputerControls()
{
if (phControl.Controls.Count>0) phControl.Controls.Clear();
SqlConnection dataConn= new SqlConnection();
dataConn.ConnectionString="xxx";
dataConn.Open();
SqlCommand deviceCmd = new SqlCommand("p_NADA_GetControls",dataConn);
deviceCmd.CommandType=CommandType.StoredProcedure;
SqlParameter deviceParm = deviceCmd.Parameters.Add("@Device", SqlDbType.NVarChar, 25);
deviceParm.Value = device;
SqlDataReader DeviceReader = deviceCmd.ExecuteReader();
phControl.Visible=true;
phControl.Controls.Add(new LiteralControl("<table>"));
while (DeviceReader.Read())
{
string ctype = (string)DeviceReader["ControlType"];
if (ctype=="Label")
{
Label lblLabel= new Label();
lblLabel.Text=DeviceReader["ControlValue"].ToString();;
lblLabel.Font.Bold=true;
lblLabel.Font.Name="Arial";
lblLabel.Font.Size=System.Web.UI.WebControls.FontUnit.XSmall;
lblLabel.ForeColor=System.Drawing.Color.DarkBlue;
phControl.Controls.Add(new LiteralControl("<tr>"));
phControl.Controls.Add(new LiteralControl("<td class='label'>"));
phControl.Controls.Add(lblLabel);
phControl.Controls.Add(new LiteralControl("</td>"));
}
else if (ctype=="TextBox")
{
TextBox txtTextBox= new TextBox();
txtTextBox.ID = DeviceReader["ControlName"].ToString();
txtTextBox.EnableViewState=true;
btnPrint.Visible=true;
phControl.Controls.Add(new LiteralControl("<td class='TextBox'>"));
phControl.Controls.Add(txtTextBox);
phControl.Controls.Add(new LiteralControl("</td>"));
}
}
phControl.Controls.Add(new LiteralControl("</tr>"));
phControl.Controls.Add(new LiteralControl("</table>"));
ViewState.Add("mode","1");
DeviceReader.Close();
dataConn.Close();
}
#endregion createcontrols
#region PrintClick
private void btnPrint_submit(object sender, System.EventArgs e)
{
ViewState.Add("mode","1");
}
#endregion
}
}