accessing a value of a user control from the calling page

Discussion in 'ASP .Net' started by evandelagrammaticas@hotmail.com, Mar 18, 2006.

  1. Guest

    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.
     
    , Mar 18, 2006
    #1
    1. Advertising

  2. Teemu Keiski Guest

    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


    <> wrote in message
    news:...
    > 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.
    >
     
    Teemu Keiski, Mar 18, 2006
    #2
    1. Advertising

  3. Guest

    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.
     
    , Mar 18, 2006
    #3
  4. Teemu Keiski Guest

    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


    <> wrote in message
    news:...
    > 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.
    >
     
    Teemu Keiski, Mar 18, 2006
    #4
  5. Guest

    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 :)
     
    , Mar 18, 2006
    #5
  6. Teemu Keiski Guest

    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






    <> wrote in message
    news:...
    > 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 :)
    >
     
    Teemu Keiski, Mar 19, 2006
    #6
  7. Guest

    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.
     
    , Mar 19, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Knoop

    Accessing control on calling page

    John Knoop, Jul 19, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    352
    John Knoop
    Jul 19, 2003
  2. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    600
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  3. =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=

    Accessing value of dropdown in user control from main page

    =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=, Dec 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,729
    bhawin13
    Dec 22, 2005
  4. David Hearn
    Replies:
    6
    Views:
    340
    David Hearn
    May 4, 2006
  5. Kiran More
    Replies:
    2
    Views:
    339
    Kiran More
    Nov 14, 2006
Loading...

Share This Page