Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > accessing a value of a user control from the calling page

Reply
Thread Tools

accessing a value of a user control from the calling page

 
 
evandelagrammaticas@hotmail.com
Guest
Posts: n/a
 
      03-18-2006
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.

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      03-18-2006
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)


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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
 
 
 
evandela@bigpond.net.au
Guest
Posts: n/a
 
      03-18-2006
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.

 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      03-18-2006
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)

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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
evandela@bigpond.net.au
Guest
Posts: n/a
 
      03-18-2006
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">
<uc1roductList 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

 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      03-19-2006
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-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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">
> <uc1roductList 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
>



 
Reply With Quote
 
evandela@bigpond.net.au
Guest
Posts: n/a
 
      03-19-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing USER CONTROL which is inside Masterpagethrough Another USER Control inside normal page. Kiran More ASP .Net Web Controls 2 11-14-2006 12:58 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
Accessing value of dropdown in user control from main page =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?= ASP .Net 2 12-22-2005 05:27 AM
accessing the web user control's control from a web page and set a value from another web page Reny J Joseph Thuthikattu ASP .Net 1 12-30-2004 12:21 PM
Accessing control on calling page John Knoop ASP .Net 3 07-19-2003 04:46 AM



Advertisments