Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Problem with setting Properties to UserControls

Reply
 
Old 12-03-2008   #1 (permalink)
goldenrate


 
 

Problem with setting Properties to UserControls

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[i] 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

My System SpecsSystem Spec
Old 12-04-2008   #2 (permalink)
\Ji Zhou [MSFT]\


 
 

RE: Problem with setting Properties to UserControls

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/libr...rol.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 (v-jzho@xxxxxx, 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:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Old 12-04-2008   #3 (permalink)
goldenrate


 
 

Re: Problem with setting Properties to UserControls

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]""" <v-jzho@xxxxxx> wrote in message
news:fraeyUhVJHA.3380@xxxxxx
Quote:

> 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/libr...rol.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 (v-jzho@xxxxxx, 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:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Setting COM Port Properties, Named pipe VB Script
Setting object properties PowerShell
Error 0x88982f50 Setting JPG File Properties Vista file management
setting up standard user with admin-like properties Vista account administration


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46