Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic controls

Status
Not open for further replies.

fmt

Programmer
Aug 5, 2005
53
US
Hello,
I have sql table which stores controlnames and control types

controltype controlname
TextBox txt1
TextBox txt2

I want to give controlids dynamically from controlname but I am getting stuck when I am looping through controls,please advice.here is code

private void dditem_SelectedIndexChanged(object sender, System.EventArgs e)
{

SqlConnection dataConn= new SqlConnection();
dataConn.ConnectionString="xxxxx";
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 = "Computer";

SqlCommand TxtCountCmd = new SqlCommand("p_NADA_GetTextCount",dataConn);
TxtCountCmd.CommandType=CommandType.StoredProcedure;
SqlParameter TxtCountParm = TxtCountCmd.Parameters.Add("@Device", SqlDbType.NVarChar, 25);
TxtCountParm.Value = "Computer";
SqlDataReader TxtCountReader = TxtCountCmd.ExecuteReader();
while (TxtCountReader.Read())
{
tcount=(int)TxtCountReader["ct"];
}

TxtCountReader.Close();


SqlDataReader DeviceReader = deviceCmd.ExecuteReader();

while (DeviceReader.Read())
{
string ctype = (string)DeviceReader["ControlType"];


phControl.Visible=true;
phControl.Controls.Add(new LiteralControl("<table>"));



if (ctype=="TextBox")
{

for (int i = 0; i < tcount; i++)
{

TextBox txtTextBox= new TextBox();
txtTextBox.ID = (string)DeviceReader["ControlName"];
phControl.Visible=true;

//phControl.Controls.Add(new LiteralControl("<tr>"));
phControl.Controls.Add(new LiteralControl("<td class='TextBox'>"));
phControl.Controls.Add(txtTextBox);

}
}

}

DeviceReader.Close();
dataConn.Close();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top