user control issue

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
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???
 
Set the id of your control to ucFocus.

<YTCIWebUserControls:SetFocus id="ucFocus" runat="server"></YTCIWebUserControls:SetFocus>
 
Back
Top