Null reference in my user control

Discussion in 'ASP .Net' started by o0JoeCool0o@gmail.com, May 23, 2006.

  1. Guest

    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!
    , May 23, 2006
    #1
    1. Advertising

  2. 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/



    <> wrote in message
    news:...
    >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!
    >
    Karl Seguin [MVP], May 23, 2006
    #2
    1. Advertising

  3. o0JoeCool0o Guest

    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")
    o0JoeCool0o, May 24, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YWJqb3Jr?=
    Replies:
    1
    Views:
    3,786
    Jeffrey Palermo
    May 5, 2004
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    617
    Mad Scientist Jr
    Mar 22, 2006
  3. Replies:
    5
    Views:
    26,429
    Mike Schilling
    Mar 29, 2006
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    518
    Jonathan Wood
    Feb 2, 2010
  5. Harry Higbie

    User control property reference gives null error

    Harry Higbie, Jul 15, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    129
    Harry Higbie
    Jul 15, 2004
Loading...

Share This Page