Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Setting values of properties in user controls from the parent form

Reply
Thread Tools

Setting values of properties in user controls from the parent form

 
 
Mark Rae
Guest
Posts: n/a
 
      05-11-2006
Hi,

Just a general quickie on setting properties of user controls from the
parent form.

Let's say I have a user control called note.ascx which displays a datagrid.
That datagrid is populated according to an integer variable in the ViewState
of the parent form.

The code behind (still in v1.1) of the user control contains the following:

public class note : System.Web.UI.UserControl
{
protected int pParentID;
public int ParentID
{
set{pParentID = value;}
}


Let's say the parent form has the following:

<%@ Register TagPrefix="ucNote" TagName="ucNotes" Src="~/note.ascx" %>
..
..
..
<ucNote:ucNotes ID=ucNotes runat=server ParentID=<%=ViewState["ParentID"]%>
/>

While that certainly works, would it be more efficient to move the ParentID
tag out of the form page and set it in the code behind instead? This would
also prevent the ParentID value from being visible in View Source, though
that's not reallly much of a consideration in this case.

Any assistance gratefully received.

Mark


 
Reply With Quote
 
 
 
 
Parag
Guest
Posts: n/a
 
      05-11-2006
Hi Mark,

Setting the property for the User control in ASPX page is OK. But I dont think you can set it in code behind. Whenever you try to set it, it will be either init event or page load. In both the cases the user control is already compiled and its output is added to the page accordingly. But you can try one thing ... set the value in the session init and keep on changing the values as per the page in the session and make the user control pick the those values form the session. In this way you can avoid the problem of writing the property value in viewstate.

I hope I have understood your problem and answered it accordingly. In case I am wrong at any point .. then please let me know as it will also add to my knowledge of ASP.NET

Regards,
Parag Kulkarni
MTS,
Persistent Systems Private Limited.




"Mark Rae" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> Hi,
>
> Just a general quickie on setting properties of user controls from the
> parent form.
>
> Let's say I have a user control called note.ascx which displays a datagrid.
> That datagrid is populated according to an integer variable in the ViewState
> of the parent form.
>
> The code behind (still in v1.1) of the user control contains the following:
>
> public class note : System.Web.UI.UserControl
> {
> protected int pParentID;
> public int ParentID
> {
> set{pParentID = value;}
> }
>
>
> Let's say the parent form has the following:
>
> <%@ Register TagPrefix="ucNote" TagName="ucNotes" Src="~/note.ascx" %>
> .
> .
> .
> <ucNote:ucNotes ID=ucNotes runat=server ParentID=<%=ViewState["ParentID"]%>
> />
>
> While that certainly works, would it be more efficient to move the ParentID
> tag out of the form page and set it in the code behind instead? This would
> also prevent the ParentID value from being visible in View Source, though
> that's not reallly much of a consideration in this case.
>
> Any assistance gratefully received.
>
> Mark
>
>

 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      05-11-2006
"Parag" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

Pareg,

>Setting the property for the User control in ASPX page is OK. But I dont
>think you can set it in code behind.


??? Of course you can, otherwise I wouldn't have asked the question! A class
is a class is a class...

>Whenever you try to set it, it will be either init event or page load.


OK.

>In both the cases the user control is already compiled and its output is
>added to the page accordingly.


The parent form's Page_Load fires before the user control's Page_Load...

>But you can try one thing ... set the value in the session init and keep on
>changing the values as per the
>page in the session and make the user control pick the those values form
>the session. In this way you
>can avoid the problem of writing the property value in viewstate.


You can't possibly be serious...

>I hope I have understood your problem and answered it accordingly.


I think you've answered your own question...!

Just to clarify, I wasn't asking how a parent control can set properties on
its user controls - that's dead easy - I was just asking whether that was
the best way to do it...

Thanks for the reply anyway...

Mark


 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      05-12-2006
Mark,

I am not sure you are looking for an answer. Parent controls /
containers set child properties all the time, and you as the developer
choose the best time to do it.

It the TextBox_TextChanged event handler you may read the value in a
child TextBox and clear the contents by TextBox.Text = null; if they
are invalid.

In your Page_Load you may call a method to assign a DataSource to a
child DataGrid and the call it's DataBind.

To answer your rhetorical questions: 1. Yes you can set the properties
of a child control from a parent container. 2. Only you can decide if
it's the best way. Me I would use the inherent
System.Web.UI.Control.Parent to access parent properties.
(base.Response.Write(this.Parent.ID)

-Rick

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Setting values on parent form jodleren Javascript 1 02-14-2007 08:04 PM
Dynamically adding user controls, and then setting custom properties on those instances? ASP .Net 6 12-23-2006 09:12 AM
How do I set the property for a Web User Control properties from the parent form (VB) David Webb ASP .Net Web Controls 6 12-30-2004 09:21 PM
example of setting a user controls properties using code behind instead of script tags? Dave ASP .Net Web Controls 7 05-11-2004 01:00 AM



Advertisments