-
Jun 10th, 2013, 01:22 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] How to reference web user control
Hello: I have created a web user control. It contains a table that has several image buttons. I want them to behave link a tab control. Here's the souce view:
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ProcessMenuContrl.ascx.vb" Inherits="ProcessMenuContrl" %>
<table >
<tr>
<td><asp:ImageButton ID="imgCustomerInformation" runat="server" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgOrderInformation" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgRefrigeration" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgGeneralPurpose" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgOutDoor" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgPreviewQuote" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
</tr>
</table>
From another page, I have placed this user control into the main content of the form. I have registered it at the top of the page:
Code:
<%@ Register src="ProcessMenuContrl.ascx" tagname="ProcessMenuContrl" tagprefix="uc1" %>
Here's portion of the source where you can see the control is placed under the MainContent of the page. :
Code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<link href="Styles/QuoteStyles.css" rel="stylesheet" type="text/css" />
<uc1:ProcessMenuContrl ID="ProcessMenuContrl1" imgOrderInformation="" runat="server" />
When the user saves information that's on a form from this same page, I want to enable one of the image buttons (imgOrderInformation).
The problem is that i don't know how to reference this image button that's on the user control.
I've been trying to use Findcontrol, but must be doing it wrong.
Thank you for your help.
Proctor
-
Jun 10th, 2013, 02:06 PM
#2
Re: How to reference web user control
Hello,
The best way to do this would be to create public properties within your UserControl. Within these properties, do the work of enabling and disabling the image buttons, and from there, you can call these properties from the page that is hosting the user controls.
Does that make sense?
Gary
-
Jun 10th, 2013, 03:29 PM
#3
Thread Starter
Fanatic Member
Re: How to reference web user control
Hi gep13: Thank you for your reply. I believe I understand the jift of what you're saying and it makes sense, but can you give a short example of
how to create a public property?
Thank you again for your help.
proctor
-
Jun 10th, 2013, 08:46 PM
#4
Re: How to reference web user control
Your problem is you don't know how to create a public property in Visual Studio or how would you call the public property from page.
If you don't know how to create a public property then you can google and find a million examples. If you don't know how to call the public property then i believe, after you have created it inside the control code you will be able to reference it at any page your control resides, both from markup and code.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 11th, 2013, 02:11 AM
#5
Re: How to reference web user control
-
Jun 11th, 2013, 12:03 PM
#6
Thread Starter
Fanatic Member
Re: How to reference web user control
Thank you for your replies.
gep13, to test, I placed this simple call in my user control:
Code:
Public Class ProcessMenuContrl
Inherits System.Web.UI.UserControl
Public Shared Sub myMessage()
MsgBox("went here!!")
End Sub
Then, from another page, I try to access it:
Code:
Dim myCtrl As New ProcessMenuContrl
myCtrl.myMessage()
But after I enter myCtrl, I get no intellisense and it gives me this msg "myMessage is not a memember of ProcessMenuContrl"
Can you please tell me what I'm doing wrong?
thanks again.....
Proctor
-
Jun 11th, 2013, 02:22 PM
#7
Re: How to reference web user control
Did you rebuild the application before you tried to access the method?
Gary
-
Jun 11th, 2013, 03:22 PM
#8
Thread Starter
Fanatic Member
Re: How to reference web user control
I can't believe I didn't rebuild and so now that's working!!!
however, now I try to add this one line - to enable the image button that's on the control:
Code:
Public Shared Sub myMessage()
imgOrderInformation.Enabled = True
End Sub
and it's giving me the message "Cannot refer to an instance member of a class from within a shared method"
How can I reference this object from within my shared sub?
Thanks again for all your help/patience.
Proctor
-
Jun 12th, 2013, 05:40 AM
#9
Re: How to reference web user control
Hey,
We are making progress
You have marked your method as Shared, or static in C# language. As a result, you can't access any member variables, or objects, that are not also marked as Shared. Since you are creating an instance of your user control in the parent page, you don't need to mark your method as Shared. Remove this access modifier, and your code should work.
Gary
-
Jun 12th, 2013, 10:59 AM
#10
Thread Starter
Fanatic Member
Re: How to reference web user control
Hi Gary: thanks again for all the help.
I took away the shared and so that was good...
and now, another issue - when it goes in to the sub to enable the imagebutton,
it gives me this message: "object reference not set to to an instance of an object"
This was when my code was like this in the sub:
Code:
imgOrderInformation.Enabled = True
so then, i tried a few things....like this:
Code:
Dim myimgOrderInformation As New ImageButton
myimgOrderInformation = CType(FindControl("imgOrderInformation"), ImageButton)
myimgOrderInformation.Enabled = True
however, it's still returning nothing when it tries to find the control and so i get the same error message.
thanks again,
Proctor
-
Jun 12th, 2013, 02:17 PM
#11
Re: How to reference web user control
Is it possible that you can post all the code that you are currently using?
Gary
-
Jun 12th, 2013, 04:23 PM
#12
Thread Starter
Fanatic Member
Re: How to reference web user control
Hi Gary: Happy to send my code. Here's the code behind the user control - sub enableDisableControls is where the code can't find the image button:
Code:
Public Class ProcessMenuContrl
Inherits System.Web.UI.UserControl
Protected Sub imgCustomerInformation_Click(sender As Object, e As ImageClickEventArgs) Handles imgCustomerInformation.Click
Response.Redirect("CustomerInformation.aspx")
End Sub
Protected Sub imgOrderInformation_Click(sender As Object, e As ImageClickEventArgs) Handles imgOrderInformation.Click
Response.Redirect("OrderInformation.aspx")
End Sub
Protected Sub imgRefrigeration_Click(sender As Object, e As ImageClickEventArgs) Handles imgRefrigeration.Click
Response.Redirect("Refrigeration.aspx")
End Sub
Protected Sub imgGeneralPurpose_Click(sender As Object, e As ImageClickEventArgs) Handles imgGeneralPurpose.Click
Response.Redirect("GeneralPurpose.aspx")
End Sub
Protected Sub imgOutDoor_Click(sender As Object, e As ImageClickEventArgs) Handles imgOutDoor.Click
Response.Redirect("Outdoor.aspx")
End Sub
Protected Sub imgPreviewQuote_Click(sender As Object, e As ImageClickEventArgs) Handles imgPreviewQuote.Click
Response.Redirect("Preview.aspx")
End Sub
Public Sub enableDisableControls()
Dim myimgOrderInformation As ImageButton = FindControl("imgOrderInformation")
If (Not myimgOrderInformation Is Nothing) Then
Response.Write("Control found.....")
Else
Response.Write("Control not found.....")
End If
End Sub
End Class
Here's the source of this same user control:
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ProcessMenuContrl.ascx.vb" Inherits="ProcessMenuContrl" %>
<table >
<tr>
<td><asp:ImageButton ID="imgCustomerInformation" runat="server" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgOrderInformation" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgRefrigeration" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgGeneralPurpose" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgOutDoor" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/TabButton_CustomerInformation.jpg" /></td>
<td><asp:ImageButton ID="imgPreviewQuote" runat="server" Enabled="false" CausesValidation="false" ImageUrl="~/Images/Preview.png" /></td>
</tr>
</table>
Here's a portion of the code behind of the page I'm making the call enableDisableControls() from. I'll just send you a bit of this part, but if you want more, let me know. :
Code:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text.RegularExpressions
Public Class CustomerInformation
Inherits System.Web.UI.Page
Public Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ddCompanyName As AjaxControlToolkit.ComboBox = CType(frmCustomerInformation.FindControl("ddCompanyName"), AjaxControlToolkit.ComboBox)
Dim squoteCustomerInformationId As Integer
Dim ProcessMenuContrl As New ProcessMenuContrl
squoteCustomerInformationId = Request.QueryString("quoteCustomerInformationId")
If Page.IsPostBack Then
Else
If squoteCustomerInformationId > 0 Then
frmCustomerInformation.ChangeMode(FormViewMode.Edit)
ProcessMenuContrl.enableDisableControls()
If Session("saved") = "saved" Then
litMessage.Text = "Customer Information saved."
End If
End If
ddCompanyName.Focus()
If frmCustomerInformation.CurrentMode = FormViewMode.Insert Then
liInsertSave.Visible = True
liUpdateSave.Visible = False
ElseIf frmCustomerInformation.CurrentMode = FormViewMode.Edit Then
liUpdateSave.Visible = True
liInsertSave.Visible = False
End If
End If
End Sub
And here's the top portion of the source from that same page ....and it shows that i register the user control:
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/QuotesMain.master" AutoEventWireup="false" CodeFile="CustomerInformation.aspx.vb" Inherits="CustomerInformation" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register src="ProcessMenuContrl.ascx" tagname="ProcessMenuContrl" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
Thank you again.......
Proctor
-
Jun 19th, 2013, 08:37 PM
#13
Re: How to reference web user control
As suggested better to use public property to get/set controls on the usercontol.
usercontrol
Code:
Public Property customerButton() As Boolean
Get
Return imgOrderInformation.Enabled
End Get
Set(ByVal value As Boolean)
imgOrderInformation.enabled = value
End Set
End Property
page
Code:
sub accessUserControl()
Dim ProcessMenuContrl As New ProcessMenuContrl
'get the enabled state of customer button
dim btnState as boolean = ProcessMenuContrl.customerButton
'set the enabled state of customer button
ProcessMenuContrl.customerButton = false 'disable it or true to enable it
end sub
The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.
-
Jun 20th, 2013, 12:18 PM
#14
Thread Starter
Fanatic Member
Re: How to reference web user control
Brin351: thank you so much for your reply. Ok, i copied verbandam what you sent....placed the user control code in the code behind the user control and the page control behind my aspx page.
I added a call to accessUserControl() in my page load routine:
Public Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
Call accessUserControl()
However, once it hits this line: Return imgOrderInformation.Enabled
it gives the same error msg "object reference not set to an instance"
Do you know what I need to do to get past this msg?
thank you again,
Proctor
-
Jun 22nd, 2013, 02:03 AM
#15
Re: How to reference web user control
Sorry my bad just delete the line 'Dim ProcessMenuContrl As New ProcessMenuContrl' you don't want to create a new instance you can refrence the usercontrol by its ID directly. (<uc1:ProcessMenuContrl ID="ProcessMenuContrl1" runat="server" />). You will also be able to set properties of the control by selecting it on the page and viewing the property pannel, the declared properties will appear there.
Secondly you'll need to be aware of when the page loads the usercontrol and that you will get different states if you refrence it say on pageLoad, int and or in a button click, have a play at calling this sub on the page.
Code:
Protected Sub testUserControl()
'get the enabled state of customer button
Dim btnState As Boolean = ProcessMenuContrl1.customerButton
'I put a label on the page to display the value returned
Label2.Text = btnState
'set the enabled state of customer button, I'm just toggling it
If btnState = False Then
ProcessMenuContrl1.customerButton = True
Else
ProcessMenuContrl1.customerButton = False
End If
End Sub
Last edited by brin351; Jun 22nd, 2013 at 02:14 AM.
The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.
-
Jun 24th, 2013, 11:41 AM
#16
Thread Starter
Fanatic Member
Re: How to reference web user control
Hello brin351 - well, it's working!!!! I'm so happy - it's finally working!!!!
Thanks to everyone for the help -
AND thank you brin351 for showing me how to do this and staying with me on this.
I truly appreciate it very much!!!!!!
Proctor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|