[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

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

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
CharlieDev
Asked:
CharlieDev
  • 4
  • 3
  • 3
2 Solutions
 
b_levittCommented:
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
 
CharlieDevAuthor Commented:
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
 
vbwizardryCommented:
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
Technology Partners: 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!

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

and it hasnt passed the value
0
 
b_levittCommented:
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
 
vbwizardryCommented:
Yes but do not assigne  HatID='<%#DataBinder.Eval(Container.DataItem,"HatID")%>'  since you control is not part of databound container such as repeater ;)
0
 
b_levittCommented:
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
 
vbwizardryCommented:
yp
0
 
CharlieDevAuthor Commented:

Thanks using
 MyHatPic.ID= "my value";

worked :)
0
 
CharlieDevAuthor Commented:
cheers!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now