Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2008, 05:37 PM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing variable from .aspx to .ascx.vb

I'm trying to pass a variable from an .aspx page to my custom user control on an .ascx.vb page. I have the 3 files below:

[u]homepage.aspx</u>
Code:
<%@ Page Language="VB" MasterPageFile="~/templates/level_two.master" %>
<%@ Register Src="calendar_control.ascx" TagName="CustomCalendar" TagPrefix="CAL" %>

<script runat="server">
  '?????????
  'Initialize an integer variable here to use in "calendar_control.ascx.vb"
  '?????????
</script>

<asp:Content ID="levelTwo" ContentPlaceHolderID="levelTwo" Runat="Server">
  My Custom Calendar<br />
  <CAL:CustomCalendar id="EventList" runat="server"></CAL:CustomCalendar>
</asp:Content>
[u]calendar_control.ascx</u>
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="calendar_control.ascx.vb" Inherits="CustomCalendar" %>
<asp:Xml ID="Xml1" runat="server" TransformSource="../XmlFiles/event_list.xsl"></asp:Xml>
[u]calendar_control.ascx.vb</u>
Code:
Partial Class CustomCalendar
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connect As New EkDb
        Using connection As New Data.SqlClient.SqlConnection(connect.connectionString)
            connection.Open()

            '??????????
            'Accept an integer variable from "homepage.aspx"
            'so I can use it in my SQL query.
            '??????????

            connection.Close()
        End Using
    End Sub
End Class
Is something like this possible?
Thanks!
Steve
 
Old March 25th, 2008, 06:12 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Yes but you can do this where you drop the control on the page.

Add this to your ascx.vb page

Private _SomeInteger as Integer
Public Property SomeInteger() as Integer
Get
    Return _SomeInteger
End Get
Protected Set(ByVal Value as Integer)
    _SomeInteger = Value
End Set
End Property

then on your page:

<CAL:CustomCalendar id="EventList" SomeInteger="1" runat="server"></CAL:CustomCalendar>

You can then access the value inside your user control class by calling either _SomeInteger or SomeInteger()

hth.


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 26th, 2008, 10:02 AM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the response, however it still doesn't seem to work:

[u]homepage.aspx</u>
Code:
<%@ Page Language="VB" MasterPageFile="~/templates/level_two.master" %>
<%@ Register Src="calendar_control.ascx" TagName="CustomCalendar" TagPrefix="CAL" %>

<asp:Content ID="levelTwo" ContentPlaceHolderID="levelTwo" Runat="Server">
  My Custom Calendar<br />
  <CAL:CustomCalendar id="EventList" CalendarId="18" runat="server"></CAL:CustomCalendar>
</asp:Content>
[u]calendar_control.ascx</u>
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="calendar_control.ascx.vb" Inherits="CustomCalendar" %>
<asp:Xml ID="Xml1" runat="server" TransformSource="../XmlFiles/event_list.xsl"></asp:Xml>
[u]calendar_control.ascx.vb</u>
Code:
Partial Class CustomCalendar
    Inherits System.Web.UI.UserControl

    '????????????????
    'Changing "Private" to "Public" below doesn't do anything.

    Private _CalendarId As Integer
    Public Property CalendarId() As Integer
        Get
            Return _CalendarId
        End Get
        Protected Set(ByVal Value As Integer)
            _CalendarId = Value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connect As New EkDb
        Using connection As New Data.SqlClient.SqlConnection(connect.connectionString)
            connection.Open()

            '????????????????
            'The value of _CalendarId and CalendarId() is always zero.

            Response.Write(_CalendarId) 'Outputs "0"

            connection.Close()
        End Using
    End Sub
End Class
What am I missing here?
Thanks!
Steve
 
Old March 26th, 2008, 10:17 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm I am not sure. I threw this together real quick and it works as expected (in C# though)

Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="web1.ascx" TagName="web1" TagPrefix="uc1" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <uc1:web1 ID="Web1_1" Foo="2" runat="server" />
    </asp:Content>
Default.aspx.cs is empty

web1.ascx
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="web1.ascx.cs" Inherits="web1" %>
web1.ascx.cs
Code:
    private int _foo;
    public int Foo
    {
        get { return _foo; }
        set { _foo = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(_foo.ToString());
    }
Default.aspx writes the value 2 to the screen??

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 26th, 2008, 10:28 AM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hmmm, should nested master pages make any difference? it seems like this should work.
 
Old March 26th, 2008, 10:36 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I don't see why it would. The usercontrol should act dependant of all the other controls and just because it is within a nested MasterPage shouldn't affect its execution. Of course there could be an issue with this that I am unaware of.

Can you see your property in Intellisense? Try this in your page load:

EventList.CalendarId = 5

Does that make a difference?


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 26th, 2008, 10:47 AM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that's wierd - Inetllisense doesn't even recognize EventList?
 
Old March 26th, 2008, 10:59 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. If I remember correctly VS doesn't have design time support of Nested MasterPages which may be causing the issue with Intellisense not picking up the control reference (it still seems weird though). Regardless, if you call EventList.CalendarId = 5 in the page load of Default.aspx does your application build?

I would also set a break point on your property to see if it is even being hit.

BTW, in your post you said that changing Public to Private doesn't do anything when, in fact, it does quite a bit. If you were to set the property to Private you wouldn't be able to access it outside of your control. By using the keyword Public that exposes to all of the other classes within your application.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old March 26th, 2008, 11:07 AM
Authorized User
 
Join Date: Jan 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the "calendar_control.ascx.vb" page_load is definitely being hit as i can output other variables.

on "homepage.aspx" when i add the following code, my page doesn't compile:

Code:
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        EventList.CalendarId = 5
    End Sub
</script>
this throws and error on the "EventList.CalendarId = 5" line above:
Code:
Compiler Error Message: BC31102: 'Set' accessor of property 'CalendarId' is not accessible.
and thanks for the explanation of public version private - good to know.

steve
 
Old March 26th, 2008, 11:11 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Change Protected Set to Public Set. >< This was my fault in my original code.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing variable to a public variable Hudson40 Access VBA 2 February 25th, 2005 06:23 AM
Passing values between Asp to Aspx page jayaraj General .NET 2 May 25th, 2004 01:16 AM
Passing values between Asp to Aspx page jayaraj VS.NET 2002/2003 2 May 23rd, 2004 06:54 AM
HelpX_x; How do I include a variable file in aspx? CSharp1314 Classic ASP Basics 16 May 5th, 2004 06:15 PM
converting Forum.aspx to Forum.ascx (help) drfunkie BOOK: ASP.NET Website Programming Problem-Design-Solution 1 July 11th, 2003 12:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.