How to set a property in a user control

J

J-T

I have a aspx page which loaded different user contols based on the users'
entries.In one of my child user controls I have a property like below:

public String PostID
{
get
{
return (String) ViewState["PostID"];
}
set
{
ViewState["PostID"] = value;
}
}

Now from my aspx page (parent page) ,I'd like to se this propert so I can
maintain the PostID across multiple postbacks of my user control.How can I
do that?
Here is the way I load the child user control in my aspx page (parent page):

Control dynamicUC = null;

dynamicUC= LoadControl("~/PageControls/GetPostDetails.ascx");

dynamicUC.EnableViewState=true;

ViewState["PostID"]=postID;





Thanks a lot
 
K

Karl Seguin

You need to cast your dynamicUC to the specific type of the user control
(not shown below, so I have no idea)

((MyUserControlType)dynamicUC).PostID = postId;

since this is only one of multiple user controls and the only one to expose
the PostID field, you might want to make the userControl fetch the value
from a public property of the page (ie, go fetch it instead of setting it).

PostID = ((PageType)Page).PostID;

Karl
 
J

J-T

I didn;t get the second part.What do you mean by "Page" in PostID =
((PageType)Page).PostID;?? do you mean the Parent page or the userControl
page (child )).

Thanks

Karl Seguin said:
You need to cast your dynamicUC to the specific type of the user control
(not shown below, so I have no idea)

((MyUserControlType)dynamicUC).PostID = postId;

since this is only one of multiple user controls and the only one to
expose the PostID field, you might want to make the userControl fetch the
value from a public property of the page (ie, go fetch it instead of
setting it).

PostID = ((PageType)Page).PostID;

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


J-T said:
I have a aspx page which loaded different user contols based on the users'
entries.In one of my child user controls I have a property like below:

public String PostID
{
get
{
return (String) ViewState["PostID"];
}
set
{
ViewState["PostID"] = value;
}
}

Now from my aspx page (parent page) ,I'd like to se this propert so I can
maintain the PostID across multiple postbacks of my user control.How can
I do that?
Here is the way I load the child user control in my aspx page (parent
page):

Control dynamicUC = null;

dynamicUC= LoadControl("~/PageControls/GetPostDetails.ascx");

dynamicUC.EnableViewState=true;

ViewState["PostID"]=postID;





Thanks a lot
 
K

Karl Seguin

Check out:
http://openmymind.net/index.aspx?documentId=9#3.1



--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


J-T said:
I didn;t get the second part.What do you mean by "Page" in PostID =
((PageType)Page).PostID;?? do you mean the Parent page or the userControl
page (child )).

Thanks

Karl Seguin said:
You need to cast your dynamicUC to the specific type of the user control
(not shown below, so I have no idea)

((MyUserControlType)dynamicUC).PostID = postId;

since this is only one of multiple user controls and the only one to
expose the PostID field, you might want to make the userControl fetch the
value from a public property of the page (ie, go fetch it instead of
setting it).

PostID = ((PageType)Page).PostID;

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


J-T said:
I have a aspx page which loaded different user contols based on the
users' entries.In one of my child user controls I have a property like
below:

public String PostID
{
get
{
return (String) ViewState["PostID"];
}
set
{
ViewState["PostID"] = value;
}
}

Now from my aspx page (parent page) ,I'd like to se this propert so I
can maintain the PostID across multiple postbacks of my user control.How
can I do that?
Here is the way I load the child user control in my aspx page (parent
page):

Control dynamicUC = null;

dynamicUC= LoadControl("~/PageControls/GetPostDetails.ascx");

dynamicUC.EnableViewState=true;

ViewState["PostID"]=postID;





Thanks a lot
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top