By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,099 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,099 IT Pros & Developers. It's quick & easy.

Setting values of properties in user controls from the parent form

P: n/a
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
May 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <ma**@markN-O-S-P-A-M.co.uk> wrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
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

May 11 '06 #2

P: n/a
"Parag" <pa************@persistent.co.in> wrote in message
news:eL**************@TK2MSFTNGP03.phx.gbl...

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
May 11 '06 #3

P: n/a
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

May 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.