Solved

c#,asp.net- passing data to user controls

Posted on 2009-03-30
10
461 Views
Last Modified: 2012-05-07
Hi,
I want to pass a value to a user control on my aspx page , I have done this when i have data in a repeater, but dont know how to pass the data when I dont have a repeater. My control is just in a div

I want to pass the value of the HatID, i have a method that returns the hatid which is GetRandomImageURL()

Thanks
<div>
<HatPhotoControl:HatPic HatID='<%#DataBinder.Eval(Container.DataItem,"HatID")%>' runat="server" />
</div>

Open in new window

0
Comment
Question by:CharlieDev
  • 4
  • 3
  • 3
10 Comments
 
LVL 11

Expert Comment

by:b_levitt
ID: 24019631
i beleive  you just have to add a property to the code behind of the user control:
private string hatID="";
public string HatID
{
  get
  {
     return this.hatID;
  }
  set
  {
     this.hatID = value;
  }

}
0
 

Author Comment

by:CharlieDev
ID: 24019644
thanks  b levitt, i do already have that code , its just the passing of the actual value on the aspx page that I cant get
0
 
LVL 7

Expert Comment

by:vbwizardry
ID: 24019665
Use <%=[Your Value]%> if you binding to a property on the page. Also You can pass a value in you user control's onInit or OnLoad events ;) by assigning the value in code.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:CharlieDev
ID: 24019725
Thanks, how would i put in the hatId?
I tried <HatPhotoControl:HatPic HatID='<%"HatID"%>' runat="server" />

and it hasnt passed the value
0
 
LVL 11

Expert Comment

by:b_levitt
ID: 24019749
Oh I think I see what you're saying.

Give the UserControl an ID....
<HatPhotoControl:HatPic ID="MyHatPic" HatID='<%#DataBinder.Eval(Container.DataItem,"HatID")%>' runat="server" />

Then assign the property in the code behind of the aspx page...
MyHatPic.HatID = "my value";
0
 
LVL 7

Assisted Solution

by:vbwizardry
vbwizardry earned 100 total points
ID: 24019953
Yes but do not assigne  HatID='<%#DataBinder.Eval(Container.DataItem,"HatID")%>'  since you control is not part of databound container such as repeater ;)
0
 
LVL 11

Accepted Solution

by:
b_levitt earned 400 total points
ID: 24019984
thanks vb, i meant to take that out.  So the correct answer is:
Give the UserControl an ID....
<HatPhotoControl:HatPic ID="MyHatPic" runat="server" />

Then assign the property in the code behind of the aspx page...
MyHatPic.HatID = "my value";
0
 
LVL 7

Expert Comment

by:vbwizardry
ID: 24020084
yp
0
 

Author Comment

by:CharlieDev
ID: 24020371

Thanks using
 MyHatPic.ID= "my value";

worked :)
0
 

Author Closing Comment

by:CharlieDev
ID: 31597469
cheers!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question