Solved

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

Posted on 2009-03-30
10
458 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now