VBAHole22 Posted March 15, 2004 Posted March 15, 2004 I have a user control that I use on every page of my site. It has a menu in it and also a small image with a link to my home page. What I would like to do is add a placeholder in one cell of a table in the user control. Then when I load the page I want to declare a url to the image I want in the contorl. Basically I would be placing a title image in the user control for each page, but they will be different based on the page. I know that you can control aspects of user controls at runtime but I don't know how to expose those attributes and get them set. I am using VB.NET and I use code behind. Any suggestions? Quote Wanna-Be C# Superstar
kahlua001 Posted March 15, 2004 Posted March 15, 2004 Set a "Property" of the user control that is exposed to your main page. Quote
VBAHole22 Posted March 15, 2004 Author Posted March 15, 2004 Um, how exactly would I do that? When I add my user control to a web form and then open the code behind page it doesn't even recognize the user control as being there. What I mean is there are no properties or methods exposed on the user control because it isn't recognixed (doesn't show up in IntellinonSense). Here is what I have so far: A user control with an image placeholder on it. In the code behind I dim a Public variable called strLinkURL as String Then in the Page_Load of the user control I set the imageURL of the placeholder= strLinkURL. So I think all I need to do now is set that public variable at runtime on the form that I have placed the user control, right? Quote Wanna-Be C# Superstar
Moderators Robby Posted March 15, 2004 Moderators Posted March 15, 2004 I think what kahlua001 is suggesting is to create your own property where you can set whatever you want. (either at runtime or design) Quote Visit...Bassic Software
kahlua001 Posted March 15, 2004 Posted March 15, 2004 User_control.vb Public MustInherit Class myControl Inherits System.Web.UI.UserControl Private _myVariable1 As String Public WriteOnly Property myVariable1() As String Set(ByVal Value As String) _myVariable1 = Value End Set End Property End Class main_page.aspx <%@ Register TagPrefix="blah" TagName="myControl" Src="mypath" %> main_page.aspx.vb Protected WithEvents myControl As myproject.myControl Sub page_load myControl.myVariable1 = "blah blah" end sub Quote
VBAHole22 Posted March 15, 2004 Author Posted March 15, 2004 Thanx I arrived at a similar solution that also worked. I'm sure your answer is 'more proper' (not sarcasm) and I was wondering why? User_control.vb Public strLinkURL As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Image1.ImageUrl = strLinkURL End Sub main_page.aspx <%@ Register TagPrefix="uc1" TagName="Footer" Src="../includes/Footer.ascx" %> main_page.aspx.vb Protected WithEvents MiniBanner1 As New MiniBanner Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.MiniBanner1.strLinkURL = "../images/member_list.gif" End Sub Quote Wanna-Be C# Superstar
Moderators Robby Posted March 16, 2004 Moderators Posted March 16, 2004 Kaklua01's example is not only proper but will make more sense if you in the future may need to set that property at design time, because using a public variable (strLinkURL) is not something you should even consider. Get in to the habit of using Properties and get rid of those public variables. Quote Visit...Bassic Software
VBAHole22 Posted March 16, 2004 Author Posted March 16, 2004 Properties good Public variables bad I got it and I'll start putting that to work. Thanks for the help all. Quote Wanna-Be C# Superstar
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.