wsyeager Posted August 11, 2004 Posted August 11, 2004 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??? Quote Thanks, Bill Yeager (MCP, BCIP) Microsoft Certified Professional Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer YeagerTech Consulting, Inc.
Cassio Posted August 12, 2004 Posted August 12, 2004 Set the id of your control to ucFocus. <YTCIWebUserControls:SetFocus id="ucFocus" runat="server"></YTCIWebUserControls:SetFocus> Quote Stream of Consciousness (My blog)
Moderators Robby Posted August 12, 2004 Moderators Posted August 12, 2004 Also, is the control declared at the top of your code-behind? Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.