Solved

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

Posted on 2009-03-30
10
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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