Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/12/2004, 09:48
Avatar de CarlosAndres
CarlosAndres
 
Fecha de Ingreso: julio-2004
Ubicación: Bogotá, Colombia
Mensajes: 80
Antigüedad: 19 años, 10 meses
Puntos: 0
Crear controles dinamicos

Me han puesto una tarea que no se como voy a llevar a cabo, necesito crear controles (text box, drop down, labels...) desde una clase *.cs, esto con el fin de generarlos dinamicamente, para más adelante desarrollar un control generador de controles completamente parametrizable.

He partido del WebUserControl que contiene el code behind:

if (dtsProductCaseDef.Tables.Count>0)
{
if (dtsProductCaseDef.Tables[0].Rows.Count>0)
{
Session["dtsProductCaseInfo"]=dtsProductCaseInfo;
foreach (DataRow dtrRow in dtsProductCaseDef.Tables[0].Rows)
{

TableRow newRow = new TableRow();
TableCell newCell = new TableCell();
TableCell newCell2 = new TableCell();

Label lblText = new Label();
lblText.ID = "lbl_"+dtrRow["COLUMN_NAME"].ToString();
lblText.Text = dtrRow["CAPTION"].ToString();


foreach (DataRow dtrRowInf in dtsProductCaseInfo.Tables[0].Rows)
{

if (dtrRow["DATA_TYPE"].ToString() == "VARCHAR2")
{
TextBox txtVal = new TextBox();
newCell2.Controls.Add(txtVal);
txtVal.ID = "txt_"+dtrRow["COLUMN_NAME"].ToString();
Unit uWidth = new Unit(dtrRow["LENGTH"].ToString());
txtVal.Text=dtrRowInf[dtrRow["COLUMN_NAME"].ToString()].ToString();
txtVal.Width = uWidth;
//txtVal.Attributes.Add("onChange","javascript:var"+ txtVal.ID.ToString()+"="+dtrRowInf[dtrRow["COLUMN_NAME"].ToString()].ToString()+";");

dtrOrig[dtrRow["COLUMN_NAME"].ToString()]=txtVal.ID;

}
else if (dtrRow["DATA_TYPE"].ToString() == "CHAR")
{
CheckBox cbxVal = new CheckBox();
newCell2.Controls.Add(cbxVal);
cbxVal.ID = "cbx_"+dtrRow["COLUMN_NAME"].ToString();
if(dtrRowInf[dtrRow["COLUMN_NAME"].ToString()].ToString()=="T")
cbxVal.Checked=true;
else
cbxVal.Checked=false;
//cbxVal.Attributes.Add("onClick","javascript:viewBt ns()");
dtrOrig[dtrRow["COLUMN_NAME"].ToString()]=cbxVal.ID;
}
newCell.Controls.Add(lblText);
newRow.Cells.Add(newCell);

newRow.Cells.Add(newCell2);
Table2.Rows.Add(newRow);
dtrOrig[0]=dtsProductCaseDef.Tables[0].Rows.Count.ToString();

}
}
}
}

dtsProductCaseInfo.Tables[0].Rows.Add(dtrOrig);
Session["dtsProductCaseDef"]=dtsProductCaseDef;


El problema es que Table2 contiene los controles y está definida en en la parte gráfica (*.ascx) y debo hacerlo en el *.cs sin usar la parte grafica.

alguien ayudeme por favor