Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Null reference in my user control

Reply
Thread Tools

Null reference in my user control

 
 
o0JoeCool0o@gmail.com
Guest
Posts: n/a
 
      05-23-2006
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!

 
Reply With Quote
 
 
 
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      05-23-2006
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/



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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!
>



 
Reply With Quote
 
 
 
 
o0JoeCool0o
Guest
Posts: n/a
 
      05-24-2006
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")

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control to Control Other User Control Jonathan Wood ASP .Net 4 02-02-2010 03:41 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
User control property reference gives null error Harry Higbie ASP .Net Web Controls 0 07-15-2004 05:39 PM
Reference a user controls properties from another user control?? =?Utf-8?B?YWJqb3Jr?= ASP .Net 1 05-05-2004 02:41 PM



Advertisments