By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,385 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,385 IT Pros & Developers. It's quick & easy.

user control issue

P: n/a
I have the following user control in my project simply
for setting focus to a control:
Public Class SetFocus

Inherits System.Web.UI.UserControl

Public Sub Focus(ByVal strClientID As String)

Dim script As String = ""

script = "<SCRIPT languange='JavaScript'>" & _

"document.getElementById('" & _

strClientID & "').focus();" & _

"</SCRIPT>"

Page.RegisterStartupScript("focus", script)

End Sub


I have the following code in my HTML section of the aspx
page that tries to access this user control:
<%@ Register TagPrefix="YTCIWebUserControls"
TagName="SetFocus" Src="../UserControls/SetFocus.ascx" %>

..

..

..

<YTCIWebUserControls:SetFocus id="SetFocus1"
runat="server"></YTCIWebUserControls:SetFocus>


I have the following code in my code-behind section of
the aspx page that tries to access this user control:
Protected ucFocus As New YTCIWebUserControls.SetFocus

..

..

..

If Not Page.IsPostBack Then

ucFocus.Focus(mtbFromDate.ClientID)

End If


When I try and run the page, I get the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.NullReferenceException: Object
reference not set to an instance of an object.

Source Error:
Line 33: If Not Page.IsPostBack Then
Line 34: ucFocus.Focus(mtbFromDate.ClientID)

Source File:
C:\Inetpub\wwwroot\EmsAssist\Reports\MemberCall.as px.vb
Line: 34

Stack Trace:
[NullReferenceException: Object reference not set to an
instance of an object.]
YTCIWebUserControls.SetFocus.Focus(String strClientID)
EmsAssist.MemberCall.Page_Load(Object sender, EventArgs
e) in
C:\Inetpub\wwwroot\EmsAssist\Reports\MemberCall.as px.vb:35

System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
However, when I don't use the user control, and implement
the code from the user control in place of using the user
control everything works fine as indicated below:
If Not Page.IsPostBack Then

Dim intRandomNbr As Integer

Dim script As String = ""

script = "<SCRIPT languange='JavaScript'>" & _

"document.getElementById('" & _

mtbFromDate.ClientID & "').focus();"
& _

"</SCRIPT>"

intRandomNbr = GenerateRandomNbr()

Page.RegisterStartupScript("focus" &
intRandomNbr, script)

End If


What am I doing wrong that the user control can't be
used???

Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ed
I believe you have to set the instance of ucFocus to
..FindControl("<your control name>"), then I believe it will work as
planned.
On Wed, 11 Aug 2004 16:56:04 -0700, "bill" <ws********@msn.com> wrote:
I have the following user control in my project simply
for setting focus to a control:
Public Class SetFocus

Inherits System.Web.UI.UserControl

Public Sub Focus(ByVal strClientID As String)

Dim script As String = ""

script = "<SCRIPT languange='JavaScript'>" & _

"document.getElementById('" & _

strClientID & "').focus();" & _

"</SCRIPT>"

Page.RegisterStartupScript("focus", script)

End Sub


I have the following code in my HTML section of the aspx
page that tries to access this user control:
<%@ Register TagPrefix="YTCIWebUserControls"
TagName="SetFocus" Src="../UserControls/SetFocus.ascx" %>

.

.

.

<YTCIWebUserControls:SetFocus id="SetFocus1"
runat="server"></YTCIWebUserControls:SetFocus>


I have the following code in my code-behind section of
the aspx page that tries to access this user control:
Protected ucFocus As New YTCIWebUserControls.SetFocus

.

.

.

If Not Page.IsPostBack Then

ucFocus.Focus(mtbFromDate.ClientID)

End If


When I try and run the page, I get the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and
where it originated in the code.

Exception Details: System.NullReferenceException: Object
reference not set to an instance of an object.

Source Error:
Line 33: If Not Page.IsPostBack Then
Line 34: ucFocus.Focus(mtbFromDate.ClientID)

Source File:
C:\Inetpub\wwwroot\EmsAssist\Reports\MemberCall.a spx.vb
Line: 34

Stack Trace:
[NullReferenceException: Object reference not set to an
instance of an object.]
YTCIWebUserControls.SetFocus.Focus(String strClientID)
EmsAssist.MemberCall.Page_Load(Object sender, EventArgs
e) in
C:\Inetpub\wwwroot\EmsAssist\Reports\MemberCall.a spx.vb:35

System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
However, when I don't use the user control, and implement
the code from the user control in place of using the user
control everything works fine as indicated below:
If Not Page.IsPostBack Then

Dim intRandomNbr As Integer

Dim script As String = ""

script = "<SCRIPT languange='JavaScript'>" & _

"document.getElementById('" & _

mtbFromDate.ClientID & "').focus();"
& _

"</SCRIPT>"

intRandomNbr = GenerateRandomNbr()

Page.RegisterStartupScript("focus" &
intRandomNbr, script)

End If


What am I doing wrong that the user control can't be
used???


Nov 18 '05 #2

P: n/a
Ed I don't know why this works but it does - I was having the same
problem and page.findcontrol("myusercontrolname") added to the
beginning of the subroutine where I was calling the property of the
user control from made my whole control spring to life :)
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.