ASP.NET - Urge! Problema con controles dinamicos

 
Vista:

Urge! Problema con controles dinamicos

Publicado por Johann (1 intervención) el 29/03/2012 01:21:43
Estimados,

por favor si me pueden ayudar con este tema....
Estoy creando controles dinámicos a partir de un valor ingresado en un textbox (TxtNPag), el cual pide total pagina de un diario y automáticamente me genera la tabla con cada página y su respectiva sección periodística. El codigo de controles dinámicos se ejecuta al momento de presionar el Button1_Click (GENERAR). Ahora el problema es que al momento de querer imprimir (BtnPrint_Click) o presionar otro Boton cualquiera sea, desaparecen todos los controles en el PANEL1 y los controles que se pasan por session al webform print.aspx llegan todos seteados nuevamente, con los DropDownList sin seleccionar (yo previamente armo todo el planner del diario, por secciones con el DDL de cada pagina). ¿ Como lo hago para que los controles queden ahí y que no se eliminen con el postback ni sus valores ddl ya seleccionados?. Leí que había que crear los controles dinamicos en el page_init pero a mi no me sirve porque debo ingresar el N° de paginas del diario primero para poder generar los controles.
POR FAVOR AYUDA!

este es el codigo.

protected void Button1_Click(object sender, EventArgs e)
{

CrearPlanner();
//CrearInsertoFirst();
//CrearInsertoSecond();
//CrearInsertoThirth();

}


public void CrearPlanner()
{
if ((TxtNPag.Text == "" ) || (Int32.Parse(TxtNPag.Text) > 300))
{


Class_Cls_alert.Show("VALOR INCORRECTO");

}
else
{

HtmlTable table1 = new HtmlTable();


// Set the table's formatting-related properties.

table1.Border = 1;

table1.CellPadding = 3;

table1.CellSpacing = 3;

table1.BorderColor = "Green";

table1.Width = "100%";




// Start adding content to the table.
HtmlTableRow row;

HtmlTableCell cell;


int Cnt;

Cnt = Convert.ToInt32(TxtNPag.Text);
Cnt = int.Parse(TxtNPag.Text);
Cnt = Cnt / 2;


for (int i = 1; i <= Cnt; i++)
{


// Create a new row and set its background color.

row = new HtmlTableRow();

row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");

for (int j = 1; j <= 8; j++)
{

// Create a cell and set its text.

cell = new HtmlTableCell();

cell.ID = i + "." + j;



//cell.InnerHtml = "Row: " + i.ToString() + "<br />Cell: " + j.ToString();



//MyLabel.Text = "Row:" + i.ToString() + " " + "Col:" + " " + j.ToString();
Label MyLabel = new Label();
MyLabel.EnableViewState = true;
cell.Controls.Add(MyLabel);

row.Cells.Add(cell);
// Add the cell to the current row.

row.Cells.Add(cell);

if (j == 2)
{

DropDownList Myddl = new DropDownList();
Myddl.EnableViewState = true;
Myddl.ID = "Ddl" + i + "." + j;
Myddl.DataTextField = "SeccionNombre";
Myddl.DataSourceID = "SqlDataSource1";

cell.Controls.Add(Myddl);
row.Cells.Add(cell);
cell.Align = "Center";

// TextBox MyTextBox = new TextBox();
// //MyTextBox.Text = "Row:" + i.ToString() + " " + "Col:" + " " + j.ToString();
// MyTextBox.ID = "TxtTiempo" + i + "." + j;
// MyTextBox.MaxLength = 10;
// MyTextBox.Width = 100;
// cell.Controls.Add(MyTextBox);
// row.Cells.Add(cell);

// Button MyButton = new Button();
// MyButton.ID = "BtnCptura" + i + "." + j;
// MyButton.Text = "Capturar";
// cell.Controls.Add(MyButton);
// row.Cells.Add(cell);
}
if (j == 7)
{
DropDownList Myddl = new DropDownList();
Myddl.EnableViewState = true;
Myddl.ID = "Ddl" + i + "." + j;
Myddl.DataTextField = "SeccionNombre";
Myddl.DataSourceID = "SqlDataSource1";
Session["ddl"] = Myddl;

cell.Controls.Add((DropDownList)Session["ddl"]);

row.Cells.Add(cell);
cell.Align = "Center";
// TextBox MyTextBox = new TextBox();
// // MyTextBox.Text = "Row:" + i.ToString() + " " + "Col:" + " " + j.ToString();
// MyTextBox.ID = "TextBox" + i + "." + j;
// MyTextBox.MaxLength = 10;
// MyTextBox.Width = 100;
// cell.Controls.Add(MyTextBox);
// row.Cells.Add(cell);

// Button MyButton = new Button();
// MyButton.ID = "BtnCptura" + i + "." + j;
// MyButton.Text = "Capturar";
// cell.Controls.Add(MyButton);
// row.Cells.Add(cell);

}
if (j == 1)
{
Label MyLabelO = new Label();
MyLabelO.EnableViewState = true;
MyLabelO.Text = "COLOR ";
MyLabelO.ForeColor = Color.Red;
cell.Controls.Add(MyLabelO);

row.Cells.Add(cell);

CheckBox cbox = new CheckBox();
cbox.EnableViewState = true;
cbox.Checked = true;
cbox.ID = "cbox" + i + "." + j;

cell.Controls.Add(cbox);
row.Cells.Add(cell);

//DropDownList Myddl = new DropDownList();
//Myddl.ID = "Ddl" + i + "." + j;
//Myddl.DataTextField = "SeccionNombre";
//Myddl.DataSourceID = "SqlDataSource1";
//cell.Controls.Add(Myddl);
//row.Cells.Add(cell);

}
if (j == 8)
{

Label MyLabelO = new Label();
MyLabelO.EnableViewState = true;
MyLabelO.Text = "COLOR ";
MyLabelO.ForeColor = Color.Red;
cell.Controls.Add(MyLabelO);

row.Cells.Add(cell);

CheckBox cbox = new CheckBox();
cbox.EnableViewState = true;
cbox.Checked = true;
cbox.ID = "cbox" + i + "." + j;
cell.Controls.Add(cbox);

row.Cells.Add(cell);


//DropDownList Myddl = new DropDownList();
//Myddl.ID = "Ddl" + i + "." + j;
//Myddl.DataTextField = "SeccionNombre";
//Myddl.DataSourceID = "SqlDataSource1";

//cell.Controls.Add(Myddl);
//row.Cells.Add(cell);

}

if (j == 3)
{

Label MyLabelO = new Label();
MyLabelO.EnableViewState = true;
MyLabelO.Text = i.ToString();
cell.Controls.Add(MyLabelO);

row.Cells.Add(cell);
}

if (j == 6)
{
int y;


Label MyLabelO = new Label();
MyLabelO.EnableViewState = true;
y = ((Cnt * 2) + 1) - i;
MyLabelO.Text = y.ToString();
cell.Controls.Add(MyLabelO);

row.Cells.Add(cell);
}
if (j == 4)
{


Label MyLabelO = new Label();
MyLabelO.EnableViewState = true;
MyLabelO.Text = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
cell.Controls.Add(MyLabelO);

row.Cells.Add(cell);
}
if (j == 5)
{


Label MyLabelO = new Label();
MyLabelO.EnableViewState = true;
MyLabelO.Text = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
cell.Controls.Add(MyLabelO);

row.Cells.Add(cell);
}
}






// Add the row to the table.

table1.Rows.Add(row);

}
string cnString = "Data Source=localhost;Initial Catalog=PRODUCCION_FLUJO_DIARIO;User Id=sa;Password=jantesh10;";

SqlConnection _CNN = new SqlConnection(cnString);

// Crear comando sql

SqlCommand nonqueryCommand = _CNN.CreateCommand();

try
{
_CNN.Open();

// INSERTAMOS DATOS A BASE DE DATOS - EDICION PLANNER Y PRODUCTO DIARIO

nonqueryCommand.CommandText = "INSERT INTO tblPlannerNewsPaper (Fecha,Edicion,TotalProducto,TotalInsertoFirst,TotalInsertoSecond,TotalInsertoThirth,NameInsertoFirst,NameInsertoSecond,NameInsertoThirth) VALUES (@Fecha,@Edicion,@TotalProducto,@InsertoFirst,@InsertoSecond,@InsertoThirth,@NameInserto1,@NameInserto2,@NameInserto3)";

nonqueryCommand.Parameters.Add("@Fecha", SqlDbType.DateTime, 10);
nonqueryCommand.Parameters["@Fecha"].Value = txtFechaEdicionPlanner.Text;

nonqueryCommand.Parameters.Add("@Edicion", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@Edicion"].Value = "BOTH";

nonqueryCommand.Parameters.Add("@TotalProducto", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@TotalProducto"].Value = TxtNPag.Text;


// INSERTAMOS DATOS DE LOS INSERTOS EN BASE DATOS


nonqueryCommand.Parameters.Add("@InsertoFirst", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@InsertoFirst"].Value = TxtCantInsert1.Text;

nonqueryCommand.Parameters.Add("@InsertoSecond", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@InsertoSecond"].Value = TxtCantInsert2.Text;

nonqueryCommand.Parameters.Add("@InsertoThirth", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@InsertoThirth"].Value = TxtCantInsert3.Text;


nonqueryCommand.Parameters.Add("@NameInserto1", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@NameInserto1"].Value = TxtNameInserto.Text;

nonqueryCommand.Parameters.Add("@NameInserto2", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@NameInserto2"].Value = TxtNameInsertoSecond.Text;

nonqueryCommand.Parameters.Add("@NameInserto3", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@NameInserto3"].Value = TxtNameInsertothirth.Text;


nonqueryCommand.ExecuteNonQuery();

// VERIFICAMOS EL ID DEL USUARIO QUE CREÓ PLANNER

nonqueryCommand.CommandText = "UPDATE tblPlannerNewsPaper SET IdUsuario = (SELECT IdUsuario FROM tblUsuarios WHERE Usuario= @NameUsuario) WHERE IdUsuario IS NULL "; ;
nonqueryCommand.Parameters.Add("@NameUsuario", SqlDbType.VarChar, 50);
nonqueryCommand.Parameters["@NameUsuario"].Value = var._gUser;


nonqueryCommand.ExecuteNonQuery();



}

catch (SqlException ex)
{

// Muestra Error
lblErrMsg.Text = "Error Encontrado en base de datos: </BR></BR>" + ex.ToString();
lblErrMsg.Visible = true;
}


finally
{

_CNN.Close();


}



Label GenerationDate = new Label();

GenerationDate.Text = "Edición: " + Session["GenerationDate"].ToString();

Panel1.Controls.Add(GenerationDate);
Panel1.HorizontalAlign = HorizontalAlign.Center;
GenerationDate.Font.Bold = true;
GenerationDate.Font.Size = 15;

this.Panel1.Controls.Add(table1);

Session["PanelControl"] = Panel1;






// Add the table to the page.




}





}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Urge! Problema con controles dinamicos

Publicado por roger (45 intervenciones) el 07/04/2012 15:45:32
Setea una variable de sesion cuando ya se haya ingresado el N° de paginas del diario, y crea los controles en el Page_init, solo cuando esa variable de sesion tenga algun valor.

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar