reading labels from an user control

  • Thread starter Thread starter Hans
  • Start date Start date
H

Hans

Hello,

I have the following user control called encabezado (I cut some parts
of the code to make it shorter):

public partial class Encabezado : System.Web.UI.UserControl {
private String n;
public Encabezado()
{
//
// TODO: Add constructor logic here
//
}
public String estado
{
get
{
return lbl_estado.Text.ToString();
}
set
{
lbl_estado.Text = value;
}
}
public String numero
{
get
{
return n;
}
set
{
n = value;
}
}
private void page_load()
{
mostrar_encabezado(n);
}
private void mostrar_encabezado(String numero)
{

//Here I have a SQL Query and then:

lbl_nro.Text = Request.QueryString["reclamo"].ToString();
lbl_sucu.Text = dt.Rows[0]["nombre_sucu"].ToString();
lbl_titulo.Text = dt.Rows[0]["titulo_rec"].ToString();
lbl_usuario.Text = dt.Rows[0]["usuario_rec"].ToString();
lbl_asignado.Text = dt.Rows[0]["asignado_a_rec"].ToString();
lbl_estado.Text = dt.Rows[0]["estado_rec"].ToString();
lbl_icono_asig.Text = "";

// later use these to make a table, but it's not usefull for this
context.
}
}


To use the control I do:

<hparis:encabezado ID="encabezado1" runat="server" />

On the code:
encabezado1.numero = Request.QueryString["reclamo"];


And it works. Even the labels (lbl_estado.Text) are printed correctly
according to the specified number.
But when I try to use encabezado1.estado (i.e.:
Response.Write(encabezado1.estado)) I receive an empty String.
I only can access to encabezado1.numero, but no the others. Why is that
?

What am I doing wrong ?

greetings,

hans
 
Hans said:
And it works. Even the labels (lbl_estado.Text) are printed correctly
according to the specified number.
But when I try to use encabezado1.estado (i.e.:
Response.Write(encabezado1.estado)) I receive an empty String.

Hi,
It depends on the place where you want to use Response.Write. It is not
clearly stated so I make some supposition...
It can happen if you place one control (ChildControl.ascx) on another
control or page (ParentControl.ascx) and call a property of
ChildControl in the page load event handler of ParentControl. Because
page load event handler of ChildControl does not occur your property is
empty.
 
Hello and thanks for your answer.

I use my user control on a page called "mostrar.aspx" and I put the
"Response.Write" on the same page. Any ideas ?

hans
 
Hans said:
Hello and thanks for your answer.

I use my user control on a page called "mostrar.aspx" and I put the
"Response.Write" on the same page. Any ideas ?

hans

Hello,
Where specifically on a page? If it happens in the Page_Load event
hadler then I discribed this situation above, if in another place - I
have no idea :(
 
Hello,

This is what I do on the Page_load event of "mostrar.aspx.cs".

protected void Page_Load()
{
encabezado1.numero = Request.QueryString["reclamo"];

Response.Write("asignado: '" + encabezado1.asignado + "'");
//it prints an empty string
Response.Write("numero: "' + encabezado1.numero + "'");
//it prints the string I asigned above.

//some other code that isn't relevant.
}

Where should I put the 1st. response write ? Why numero is returned
correctly if it's also asigned on the Page_Load event ? I'm kinda new
to the .NET.

hans
 
Hans said:
Hello,

This is what I do on the Page_load event of "mostrar.aspx.cs".

protected void Page_Load()
{
encabezado1.numero = Request.QueryString["reclamo"];

Response.Write("asignado: '" + encabezado1.asignado + "'");
//it prints an empty string
Response.Write("numero: "' + encabezado1.numero + "'");
//it prints the string I asigned above.

//some other code that isn't relevant.
}

Where should I put the 1st. response write ? Why numero is returned
correctly if it's also asigned on the Page_Load event ? I'm kinda new
to the .NET.

Hi, Hans
Request pipeline:
1.Page load (Page, mostrar.aspx in your case).
2.Page load (Control, encabezado.ascx in your case).
3.Event handlers of controls placed on mostrar.aspx.

You are trying to access at step 1 to property that will be initilized
at step 2 only.
One of the solutions:
Rewrite some code in Encabezado:

.....
public String numero
{
get
{
return n;
}
set
{
n = value;
mostrar_encabezado(n);
}
}
private void page_load()
{
}
.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top