Problem with setting Properties to UserControls

  • Thread starter Thread starter goldenrate
  • Start date Start date
G

goldenrate

Hi,

I was trying to set properties to a user control I was writing, name it
'uc1'. I placed uc1 in another user control I'd wrote: 'mainUc'. In uc1 I
have set a few WebControls and some public properties (e.g., Double[]
_prices; Prices) and initialized them on Page_Load.

When the application runs (a web form) it loads 'mainUc' that contains
'uc1'. As long as no post back is being applied uc1.Prices contains the
value that was set on Page_Load (not on PostBack), but if i triy to access
this property (or any other) I get nothing or exception in case of array.

Is there a way to define properties that will still be 'alive' after
postback (without using WebControls)? Am I doing something wrong by applying
win application techniques to web environment?

Thanks,
David
 
Hello David,

Based on your description, I guess you may write code like the following,

In the uc1.ascx.cs,
public double[] Prices;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Prices = new double[4] { 1.1, 2.2, 3.3, 4.4 };
}
}

In the Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (double d in this.mainUc1.uc11.Prices)
{
Debug.Print (d.ToString());
}
}

In this case, when the web form starts, the uc1's Page_Load function is
called. Since this is first time the page gets loaded, the Prices array's
initial codes will be executed. After that, if we click the button on the
default page, the child user control gets post back. This time, the initial
codes is not executed. Therefore later when we are trying to access the
Prices array in the button click event handle, it will throws an exception.

To keep the variable still available after the uc1 is post back, we cannot
store it in the class property. We need to store it in the ApplicationState
collection as described in this MSDN document
http://msdn.microsoft.com/en-us/library/system.web.ui.usercontrol.ispostback
(VS.71).aspx.

The following code works fine in my side,

In the uc1.ascx.cs,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Application.Add("Prices",new double[4] { 1.1, 2.2,
3.3, 4.4 });
}
}

In the Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (double d in
(double[])this.mainUc1.uc11.Application.Get("Prices"))
{
Debug.Print (d.ToString());
}
}

If I have misunderstood your questions, please feel free to let me know.
And I will try my best to follow up. Have a nice day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Ji,

It looks like a solution for my problem. I'll study it and let you know.

Thank you so much,
David
""Ji Zhou [MSFT]"" said:
Hello David,

Based on your description, I guess you may write code like the following,

In the uc1.ascx.cs,
public double[] Prices;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Prices = new double[4] { 1.1, 2.2, 3.3, 4.4 };
}
}

In the Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (double d in this.mainUc1.uc11.Prices)
{
Debug.Print (d.ToString());
}
}

In this case, when the web form starts, the uc1's Page_Load function is
called. Since this is first time the page gets loaded, the Prices array's
initial codes will be executed. After that, if we click the button on the
default page, the child user control gets post back. This time, the
initial
codes is not executed. Therefore later when we are trying to access the
Prices array in the button click event handle, it will throws an
exception.

To keep the variable still available after the uc1 is post back, we cannot
store it in the class property. We need to store it in the
ApplicationState
collection as described in this MSDN document
http://msdn.microsoft.com/en-us/library/system.web.ui.usercontrol.ispostback
(VS.71).aspx.

The following code works fine in my side,

In the uc1.ascx.cs,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Application.Add("Prices",new double[4] { 1.1, 2.2,
3.3, 4.4 });
}
}

In the Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (double d in
(double[])this.mainUc1.uc11.Application.Get("Prices"))
{
Debug.Print (d.ToString());
}
}

If I have misunderstood your questions, please feel free to let me know.
And I will try my best to follow up. Have a nice day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Back
Top