Modify user control at runtime

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
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?
 
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?
 
User_control.vb
Code:
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
Code:
<%@ Register TagPrefix="blah" TagName="myControl" Src="mypath" %>

main_page.aspx.vb
Code:
Protected WithEvents myControl As myproject.myControl

Sub page_load
   myControl.myVariable1 = "blah blah"
end sub
 
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
Code:
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
Code:
<%@ Register TagPrefix="uc1" TagName="Footer" Src="../includes/Footer.ascx" %>

main_page.aspx.vb
Code:
 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
 
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.
 
Properties good
Public variables bad

I got it and I'll start putting that to work.

Thanks for the help all.
 
Back
Top