Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access a Web Control on another page ?

Status
Not open for further replies.

vilrbn

Programmer
Oct 29, 2002
105
FR
Hello,

I have two ASP pages (1 and 2). When I am closing 2, I want to populate a Listbox in 1 using data in page 2.
I am adding data in the Listbox through a Method as the Control is protected and can not directly access it. When the method is called I get the following error in page 1:

Object reference not set to an instance of an object.


Code Behind Class 1:

Public Class One

Protected WithEvents lstBox As System.Web.UI.WebControls.ListBox

Public Sub AddList(ByVal lstItem As String)
lstBox.Items.Add(lstItem)
End Sub
End class

Code Behind Class 2:

Imports Project.One

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim clsOne As New Project.One()

If DataGrid.Items.Count <> 0 Then
Dim item As DataGridItem
For Each item In DataGrid.Items
clsOne.AddList(item.Cells(1).Text)
Next item
End If
Response.Write(&quot;<Script Language='Javascript'>window.close();</script>&quot;)

End Sub

Don't know how to instantiate the Listbox. If I use the keyword New in the declaration of the listbox, get no errors, but it's not working.

Thanks for your help.
 
it's &quot;normal&quot;.
If you want to manipulate functions or even variables of an other form, you must first make a instance of this form.
I recommand you to deal with this in your global.asax, to make for example :

dim myForm as Project.One = new Project.One

and then to make this variable accessible from any other form for exemple (you can create an application variable Application(&quot;The_name_of_my_var_app&quot;)

If you use this method only one time simply put the instanciation code in your form number 2

Hope it helps Best regards,
Elise, XML learning girl X-)
 
i mean you can't instanciate the listbox, only the full form Best regards,
Elise, XML learning girl X-)
 
I don't manage to access a variable declared in the global.asax from a form of my project (never used this file before).

Can you help me ?
Many Thanks.
 
the global.asax is the wery first code exacuted by your application.
in the Sub Application_Start you can for exemple do this code :

Dim Form1 as One = new One
Application(&quot;Form1&quot;) = Form1

Application(&quot;Form1&quot;) is your application variable. This variable will be accessible from any class that inherists from System.Web.UI.Page.

So in your form called &quot;One&quot; you should declare this way :

Public Class One
Inherits System.Web.UI.Page
private MyVar as One
Myvar = Application(&quot;Form1&quot;)
Myvar.doSomething()
end Class




that's all ! Best regards,
Elise, XML girl X-)
 
I still have a problem, my application variable is correctly instanciated in the global file, I can see all the objects of my form.
When I call my AddList function, application(&quot;Form1&quot;) is set back to Nothing.
Isn't it a global variable ?

Hereafter an extract of all my code:

Global.asax:

Dim Form1 as Project.One = new Project.One
Application(&quot;Form1&quot;) = Form1


Public Class One

inherits System.Web.UI.Page
Protected WithEvents lstBox As System.Web.UI.WebControls.ListBox
Private myVar as One

Public Sub AddList(ByVal lstItem As String)
myvar = application(&quot;Form1&quot;)
myvar.lstbox.items.add(lstItem)
End Sub
End class

Public Class 2

Imports Project.One

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim clsOne As New Project.One()

If DataGrid.Items.Count <> 0 Then
Dim item As DataGridItem
For Each item In DataGrid.Items
clsOne.AddList(item.Cells(1).Text)
Next item
End If

End Sub

Thanks a lot for your help.
 
it's normal
you forgot to instanciate your variable in the form2, so you created a new instanciation instead of getting the one of the global.asax

you must do this (if i'm not wrong) :
Public Class 2

Imports Project.One

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Code:
        Dim clsOne As Project.One = Application(&quot;Form1&quot;) 'here


        If DataGrid.Items.Count <> 0 Then
            Dim item As DataGridItem
            For Each item In DataGrid.Items
                clsOne.AddList(item.Cells(1).Text)
            Next item
        End If

    End Sub
Best regards,
Elise, XML girl X-)
 
I have updated the code, but I still loose the content of the application variable when the addList method of form1 is called.
I got the following error message:

Object reference not set to an instance of an object

How can I loose the instance of a global variable if I don't create another instance elsewhere in the code ?

I'm lost.
 
use debuger, and you will exactly konw where you loose your data, and if the error is for the application variable or not. Best regards,
Elise, XML girl X-)
 
i mean add break points, and look at each variable step by step thanks to &quot;right click > Quickwatch&quot;, you will get all the info you need.

Best regards,
Elise, XML girl X-)
 
That's why I did and how I saw that the problem is coming from this variable.
As I am only instantiating it in the Global.asax file, I don't understand why it looses its value when I call the Addlist method.
I never used the global.asax file but regarding its name, I suppose that an application variable is global to the project.
Anyway, thanks for your all your explanations, it helped me.
 
Anybody else has an idea where the problem is ?
 
if your code is quite small, you can send me the files and i'll try to find where the problem is, because i think that it's the only visible solution, here in a forum it's not obvious.
send it at kea@mailcity.com

Best regards,
Elise, XML girl X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top