Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Wanna-Be C# Superstar
Posted

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?

Wanna-Be C# Superstar
Posted

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

Posted

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

Wanna-Be C# Superstar
  • Moderators
Posted

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.

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...