user control issue

B

bill

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.aspx.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.aspx.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???
 
E

Ed

I believe you have to set the instance of ucFocus to
..FindControl("<your control name>"), then I believe it will work as
planned.
 
M

Mordy

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

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

Top