accessing a value of a user control from the calling page

  • Thread starter evandelagrammaticas
  • Start date
E

evandelagrammaticas

Hi all. I have spent the better part of a day scouring the newsgroups
and I am sure that I must have come across the solution a number of
times - but I am still a real newbie at asp.net so please forgive me
beforehand if I ask anything really simple or ridiculous.
I am going to simplify my query... the structure of my example is
---> page1.aspx
------> page1.aspx.vb
--->control1.ascx

for the sake of simplicity this is what I am trying to achieve.
control1.ascx generates a value, say - number 1. I need display that
value in the onload event of page1.aspx.vb

The code is as follows:
I am showing the code for page1.aspx to show how I am declaring the
control on the site.
-------------- page1.aspx -------------------------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="page1.aspx.vb"
Inherits="_Default" %>
<%@ Register Src="control1.ascx" TagName="control1" TagPrefix="uc1" %>
..
..
..
<uc1:control1 ID="control11" runat="server" /> 'including the user
control here
-------------- end of page1.aspx ------------------------------



I want to display the value generated in the user control here on
page1.aspx.vb
-------------- page1.aspx.vb -------------------------------------
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim selectedValue = Page.LoadControl("control1.ascx")
Response.Write(selectedValue.selectedProduct)
End Sub
End Class
-------------- end of page1.aspx.vb ------------------------------



The control generates a single number - in this case I am just saying 1
-------------- control.ascx-------------------------------------
<%@ Control Language="VB" ClassName="WebUserControl" %>
<script language="VB" runat="server">
Dim strSelectedProduct As String
Public Property selectedProduct() As String
Get
Return strSelectedProduct
End Get
Set(ByVal value As String)
strSelectedProduct = "1"
End Set
End Property
</script>
-------------- end of control.ascx-------------------------------------

Where am I going wrong? Any help would be really appreciated.
 
T

Teemu Keiski

Hi,

since properties etc are declared in the inline user control, you would need
to use @Reference directive also to make those properties available

<%@ Register Src="control.ascx" TagName="control" TagPrefix="uc1" %>

<%@ Reference Control="~/control.ascx" %>

And in code accessing it

Dim selectedValue As ASP.WebUserControl =
DirectCast(Page.LoadControl("control1.ascx"), ASP.WebUserControl)

Response.Write(selectedValue.selectedProduct)
 
E

evandela

Thank you SO much for the help... I have integrated this part of your
code into the sample.
Dim selectedValue As ASP.WebUserControl = _
DirectCast(Page.LoadControl("control1.ascx"),
ASP.WebUserControl)
Response.Write(selectedValue.selectedProduct)

However when I attempt to integrate these 2 lines into the
control1.ascx,
<%@ Register Src="control.ascx" TagName="control" TagPrefix="uc1" %>
<%@ Reference Control="~/control.ascx" %>
I get the error - " Circular file references are not allowed"

I know this must seem VERY simplistic to you, but this would be my
second asp.net program so please forgive me if I am making stupid
mistakes here.
 
T

Teemu Keiski

You would use @register and @reference directives on the Page using the
control, not in the UC itself (that's what causing the circular reference
since UC's tries to register itself)
 
E

evandela

Hi again Teemu, thank you so much. The page loads now... but the value
is still not transmitted... dont get any errors though, so I suppose
that is a good thing. Here is the actual code... maybe it makes a
difference.

CONTROL CODE: (productList.ascx)
<%@ Control Language="VB" ClassName="WebUserControl" %>
<script language="VB" runat="server">
Dim strSelectedProduct As String
Public Property selectedProduct() As String
Get
Return strSelectedProduct
End Get
Set(ByVal value As String)
strSelectedProduct = "added to id - " &
gViewProductList.SelectedDataKey.Value.ToString()
End Set
End Property
</script>

<asp:GridView ID="gViewProductList" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="Database" AllowPaging="True" AllowSorting="True"
Font-Names="Arial"
Font-Size="11px" CellPadding="3" EmptyDataText="Not Entered"
ForeColor="#333333"
HorizontalAlign="Left" Width="100%">
..
..
..

SEPERATED CODE PAGE (default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim selectedValue As ASP.WebUserControl =
DirectCast(Page.LoadControl("ProductList.ascx"), ASP.WebUserControl)
Response.Write("TEST: " & selectedValue.selectedProduct())

End Sub
End Class
..
..
..
MAIN PAGE (default.aspx)
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Src="ProductList.ascx" TagName="ProductList"
TagPrefix="uc1" %>
<%@ Reference Control="ProductList.ascx" %>
....
<form id="form1" runat="server">
<uc1:productList ID="ProductList1" runat="server" />
</form>


That is the code as I have it. when I select an row in the gridview on
the control page, I want the selected product ID to be avaliable from
the default.aspx page. Currently, when I click it, I can get AND
display the value - but only from within the control itself...

Teemu, if you can help me here... I will make a Temmu action figure and
sell it to children all over the world :)
 
T

Teemu Keiski

Hi,

I didn't pay yet attention to the correctness of the logic since you were
fightinf with the error msg.

Now, first of all:

1. strSelectedProduct being a local member in the UC, doesn't do what you
intend. Controls are recreated for every request and this variable wouldn't
keep state over postbacks. Second, using the property would make sense by
mapping directly to the SelectedDataKey

Public ReadOnly Property selectedProduct() As String
Get
Return gViewProductList.SelectedDataKey.Value.ToString()
End Get
End Property

Second, seems that loading the UC in Page_Load is not needed since you
declare it directly on the Page. Using @reference directive should make
typed access via 'ProductList1' member available.

E.g you could just have

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Response.Write("TEST: " & ProductList1.selectedProduct())

End Sub
End Class


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
 
E

evandela

Teemu, thank you so so much. I have been battling with this for so long
now - and now that I look at the code, it seems so obvious. You are a
start :) thanks heaps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top