Null reference in my user control

O

o0JoeCool0o

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').focus();</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').focus();</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!
 
K

Karl Seguin [MVP]

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
 
O

o0JoeCool0o

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")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top