473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Null reference in my user control

I am trying to create a User Control, that will be a message box with
input options

if I call okconf.visible = true in the page load of the user control it
works fine, but if i then try to call it later by calling the user
controls alert method i get a reference null error.

heres the class code
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>> Public Class AlertBox1
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnOk As System.Web.UI.WebControls.Button
Protected WithEvents okconf As
System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents oktitle As
System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents okmsg As
System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents btnYes As System.Web.UI.WebControls.Button
Protected WithEvents btnNo As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
okconf.Visible = False
End Sub
Public Enum msgType
okMsg
YesNo
End Enum

Public Sub alert(ByVal MsgText As String, ByVal msgTitle As String,
Optional ByVal OKMethod As String = "", Optional ByVal AlertType As
msgType = msgType.okMsg, Optional ByVal YesMethod As String = "",
Optional ByVal NoMethod As String = "")
Select Case AlertType
Case Is = msgType.okMsg
okconf.Visible = True
btnYes.Visible = False
btnNo.Visible = False
btnOk.Visible = True
Session("OKProc") = OKMethod
okmsg.InnerHtml = MsgText
oktitle.InnerHtml = msgTitle
okconf.Style.Item("left") = 300
okconf.Style.Item("TOP") = 300

'focus ok button
Page.RegisterStartupScript("CallSetFocus", "<SCRIPT
language=javascript>GetElementByName('btnOk').focu s();</SCRIPT>")

Case Is = msgType.YesNo
okconf.Visible = True
btnYes.Visible = True
btnNo.Visible = True
btnOk.Visible = False
Session("YProc") = YesMethod
Session("NProc") = NoMethod
Session("YNAns") = ""
okmsg.InnerHtml = MsgText
oktitle.InnerHtml = msgTitle
okconf.Style.Item("left") = 300
okconf.Style.Item("TOP") = 200

'Focus Yes Button
Page.RegisterStartupScript("CallSetFocus", "<SCRIPT
language=javascript>GetElementByName('btnYes').foc us();</SCRIPT>")
End Select
End Sub
End Class
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>

heres the code behind for the main page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim alert As New AlertBox1
alert.alert("a message in the box", "a msg title", ,
AlertBox1.msgType.okMsg)
End Sub
so it crashes on the user control code behind when it tries to set
okconf.visible to true
please help!

May 23 '06 #1
2 2560
Although controls and pages are classes, they aren't created via the "new"
terminology..you need to use Page.LoadControl in order to dynamically load
controls, ala:

Dim alert As as AlertBox = ctype(Page.LoadControl("alertbox.ascx",
AlertBox))
somePlaceHolder.Controls.Add(alert);

I'm not sure why alert() needs to be externally called? Why not simply call
the Alert() method from the user control's load and pass in properties,
something like:

Dim alert As as AlertBox = ctype(Page.LoadControl("alertbox.ascx",
AlertBox))
alert.Message = "Oh no!"
somePlaceHolder.Controls.Add(alert);

and simply not create the alert control if you don't want it displayed..

Karl
--
http://www.openmymind.net/

<o0*********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
I am trying to create a User Control, that will be a message box with
input options

if I call okconf.visible = true in the page load of the user control it
works fine, but if i then try to call it later by calling the user
controls alert method i get a reference null error.

heres the class code
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>> Public Class AlertBox1
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnOk As System.Web.UI.WebControls.Button
Protected WithEvents okconf As
System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents oktitle As
System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents okmsg As
System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents btnYes As System.Web.UI.WebControls.Button
Protected WithEvents btnNo As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
okconf.Visible = False
End Sub
Public Enum msgType
okMsg
YesNo
End Enum

Public Sub alert(ByVal MsgText As String, ByVal msgTitle As String,
Optional ByVal OKMethod As String = "", Optional ByVal AlertType As
msgType = msgType.okMsg, Optional ByVal YesMethod As String = "",
Optional ByVal NoMethod As String = "")
Select Case AlertType
Case Is = msgType.okMsg
okconf.Visible = True
btnYes.Visible = False
btnNo.Visible = False
btnOk.Visible = True
Session("OKProc") = OKMethod
okmsg.InnerHtml = MsgText
oktitle.InnerHtml = msgTitle
okconf.Style.Item("left") = 300
okconf.Style.Item("TOP") = 300

'focus ok button
Page.RegisterStartupScript("CallSetFocus", "<SCRIPT
language=javascript>GetElementByName('btnOk').focu s();</SCRIPT>")

Case Is = msgType.YesNo
okconf.Visible = True
btnYes.Visible = True
btnNo.Visible = True
btnOk.Visible = False
Session("YProc") = YesMethod
Session("NProc") = NoMethod
Session("YNAns") = ""
okmsg.InnerHtml = MsgText
oktitle.InnerHtml = msgTitle
okconf.Style.Item("left") = 300
okconf.Style.Item("TOP") = 200

'Focus Yes Button
Page.RegisterStartupScript("CallSetFocus", "<SCRIPT
language=javascript>GetElementByName('btnYes').foc us();</SCRIPT>")
End Select
End Sub
End Class
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>

heres the code behind for the main page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim alert As New AlertBox1
alert.alert("a message in the box", "a msg title", ,
AlertBox1.msgType.okMsg)
End Sub
so it crashes on the user control code behind when it tries to set
okconf.visible to true
please help!

May 23 '06 #2
well mainly because of ease of use, I just wanna be able to drag the
control onto any page and then call an alert function like you can in
javascript. I dont want to have to set a bunch of properties either
which is why im trying to pass paramaters to the method rather then use
properties

I just wanna go alertbox.alert("ERROR!", "Error Title")

May 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
7
by: odonel | last post by:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: here is my code: HttpCookie cookie = Request.Cookies;
16
by: Paul S. Natanson | last post by:
What is a Null Reference error and how do I fix it? My newly installed VB.Net2003 gives me a "Microsoft Development Environment" error message box EVERY time I try to run/start ANY project -...
2
by: kens | last post by:
I've found some odd behavior in dynamically loaded user controls under ASP.NET 2.0. Specifically, if I load a control dynamically, in the "Page_Load" event, the code-behind for the user control is...
3
by: Larry | last post by:
Hi all, I am coming over from VB 6, learning VB.NET. I have read and have in front of me, the language reference from the msdn, on; Behavior of Null has changed, Isdbnull function and...
4
by: Sam Martin | last post by:
I've got a really annoying problem, and I feel like i've tried everything .... I've got a really simple usercontrol, containing a single dropdown. In the Page_Load of the usercontrol, i want to...
3
by: needin4mation | last post by:
In this code: protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRowView rowView =...
2
by: Simon Rigby | last post by:
Hi folks, A bizarre problem I am having. I have a treeview which is bound to an XmlDataSource. The XMLDataSource.Data property is set to the result of a function that generates an XML...
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.