asp.net 2: Re-use code with ClientScript.RegisterStartupScript

Discussion in 'ASP .Net' started by sck10, Nov 9, 2005.

  1. sck10

    sck10 Guest

    Hello,
    I have the following sub in a class in my "App_Code" directory. The script
    is for setting focus on a particular control, but I get the error,
    "Name ClientScript Not declared". Also, I am using MasterPages. How do I
    use "ClientScript.RegisterStartupScript" in a class?

    Public Shared Sub SetFocusControl(ByVal FocusControl As Control)

    Dim Script As New System.Text.StringBuilder
    Dim ClientID As String = FocusControl.ClientID
    With Script
    .Append("<script language='javascript'>")
    .Append("document.getElementById('")
    .Append(ClientID)
    .Append("').focus();")
    .Append("</script>")
    End With
    ' ClientScript.RegisterStartupScript(GetType(String), "ShowInfoPage",
    Script.ToString())

    End Sub
     
    sck10, Nov 9, 2005
    #1
    1. Advertising

  2. sck10

    Brock Allen Guest

    You need to do:

    Me.Page.ClientScript.RegisterClienbtScriptBlock(...)

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen

    > Hello,
    > I have the following sub in a class in my "App_Code" directory. The
    > script
    > is for setting focus on a particular control, but I get the error,
    > "Name ClientScript Not declared". Also, I am using MasterPages. How
    > do I
    > use "ClientScript.RegisterStartupScript" in a class?
    > Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    >
    > Dim Script As New System.Text.StringBuilder
    > Dim ClientID As String = FocusControl.ClientID
    > With Script
    > .Append("<script language='javascript'>")
    > .Append("document.getElementById('")
    > .Append(ClientID)
    > .Append("').focus();")
    > .Append("</script>")
    > End With
    > ' ClientScript.RegisterStartupScript(GetType(String),
    > "ShowInfoPage",
    > Script.ToString())
    > End Sub
    >
     
    Brock Allen, Nov 9, 2005
    #2
    1. Advertising

  3. Thanks for Brock's suggestion.

    Hi Sck10,

    For Control class, we can access its container page instance through the
    "Page" property. Also, if the control is dynamically created and added on
    page, please make sure that before we access that Control.Page propety, the
    control has been added into the Page's control structure , otherwise, the
    "Page" property remains Nothing....

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | Message-ID: <>
    | From: Brock Allen <>
    | Subject: Re: asp.net 2: Re-use code with
    ClientScript.RegisterStartupScript
    | References: <>
    | MIME-Version: 1.0
    | Content-Transfer-Encoding: 8bit
    | Content-Type: text/plain; charset=iso-8859-1; format=flowed
    | X-Newsreader: JetBrains Omea Reader 671.6
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | Date: Wed, 09 Nov 2005 14:49:00 -0800
    | NNTP-Posting-Host: c-66-30-185-175.hsd1.ma.comcast.net 66.30.185.175
    | Lines: 1
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:356844
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | You need to do:
    |
    | Me.Page.ClientScript.RegisterClienbtScriptBlock(...)
    |
    | -Brock
    | DevelopMentor
    | http://staff.develop.com/ballen
    |
    | > Hello,
    | > I have the following sub in a class in my "App_Code" directory. The
    | > script
    | > is for setting focus on a particular control, but I get the error,
    | > "Name ClientScript Not declared". Also, I am using MasterPages. How
    | > do I
    | > use "ClientScript.RegisterStartupScript" in a class?
    | > Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    | >
    | > Dim Script As New System.Text.StringBuilder
    | > Dim ClientID As String = FocusControl.ClientID
    | > With Script
    | > .Append("<script language='javascript'>")
    | > .Append("document.getElementById('")
    | > .Append(ClientID)
    | > .Append("').focus();")
    | > .Append("</script>")
    | > End With
    | > ' ClientScript.RegisterStartupScript(GetType(String),
    | > "ShowInfoPage",
    | > Script.ToString())
    | > End Sub
    | >

    |
    |
     
    Steven Cheng[MSFT], Nov 10, 2005
    #3
  4. sck10

    sck10 Guest

    Thanks Steve,

    I'm still having problems. I get the following error when trying to use
    "Me.Page". Also, I am only using "Imports Microsoft.VisualBasic".

    error: 'Me' is valid only within an instance method.

    ---------------------------------
    Imports Microsoft.VisualBasic

    Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    Dim Script As New System.Text.StringBuilder
    Dim ClientID As String = FocusControl.ClientID
    With Script
    .Append("<script language='javascript'>")
    .Append("document.getElementById('")
    .Append(ClientID)
    .Append("').focus();")
    .Append("</script>")
    End With
    'Me.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    "SetFocusOnControl", Script.ToString())
    'Me.Page.ClientScript.RegisterStartupScript(GetType(String),
    "SetFocusOnControl", Script.ToString())


    End Sub


    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Thanks for Brock's suggestion.
    >
    > Hi Sck10,
    >
    > For Control class, we can access its container page instance through the
    > "Page" property. Also, if the control is dynamically created and added on
    > page, please make sure that before we access that Control.Page propety,

    the
    > control has been added into the Page's control structure , otherwise, the
    > "Page" property remains Nothing....
    >
    > Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    > --------------------


    > |
    > | You need to do:
    > |
    > | Me.Page.ClientScript.RegisterClienbtScriptBlock(...)
    > |
    > | -Brock
    > | DevelopMentor
    > | http://staff.develop.com/ballen
     
    sck10, Nov 10, 2005
    #4
  5. Thanks for your response Sck10,

    What's the class of the "SetFocusControl" method, from your code snippet ,
    the "SetFocusControl" method is a static function so that it can not access
    its container class's "Me" property. In addition, only class derived from
    System.Web.UI.Control has the "Page" propety... Is this class only a
    helper component class? If so, you need to use the passed in "Control"
    parameter 's "Page" property to register the script, e.g:

    =========================
    Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    Dim Script As New System.Text.StringBuilder
    Dim ClientID As String = FocusControl.ClientID
    With Script
    .Append("<script language='javascript'>")
    .Append("document.getElementById('")
    .Append(ClientID)
    .Append("').focus();")
    .Append("</script>")
    End With


    FocusControl.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    "SetFocusOnControl", Script.ToString())
    ...................................


    =====================

    Also, as I've mentioend in the former message, the "FocusControl" must
    already been added into a certain page's control structure before we access
    its "Page" property, otherwise, that property remains Nothing.

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)


    --------------------
    | From: "sck10" <>
    | References: <>
    <>
    <>
    | Subject: Re: asp.net 2: Re-use code with
    ClientScript.RegisterStartupScript
    | Date: Thu, 10 Nov 2005 16:28:19 -0600
    | Lines: 62
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:357202
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Thanks Steve,
    |
    | I'm still having problems. I get the following error when trying to use
    | "Me.Page". Also, I am only using "Imports Microsoft.VisualBasic".
    |
    | error: 'Me' is valid only within an instance method.
    |
    | ---------------------------------
    | Imports Microsoft.VisualBasic
    |
    | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    | Dim Script As New System.Text.StringBuilder
    | Dim ClientID As String = FocusControl.ClientID
    | With Script
    | .Append("<script language='javascript'>")
    | .Append("document.getElementById('")
    | .Append(ClientID)
    | .Append("').focus();")
    | .Append("</script>")
    | End With
    | 'Me.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    | "SetFocusOnControl", Script.ToString())
    | 'Me.Page.ClientScript.RegisterStartupScript(GetType(String),
    | "SetFocusOnControl", Script.ToString())
    |
    |
    | End Sub
    |
    |
    | "Steven Cheng[MSFT]" <> wrote in message
    | news:...
    | > Thanks for Brock's suggestion.
    | >
    | > Hi Sck10,
    | >
    | > For Control class, we can access its container page instance through the
    | > "Page" property. Also, if the control is dynamically created and added
    on
    | > page, please make sure that before we access that Control.Page propety,
    | the
    | > control has been added into the Page's control structure , otherwise,
    the
    | > "Page" property remains Nothing....
    | >
    | > Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | > --------------------
    |
    | > |
    | > | You need to do:
    | > |
    | > | Me.Page.ClientScript.RegisterClienbtScriptBlock(...)
    | > |
    | > | -Brock
    | > | DevelopMentor
    | > | http://staff.develop.com/ballen
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 11, 2005
    #5
  6. Hi Sck10,

    Does the suggestions in my further response helps you? If there're anything
    else we can help, please feel free to post here.

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | X-Tomcat-ID: 32396961
    | References: <>
    <>
    <>
    <>
    | MIME-Version: 1.0
    | Content-Type: text/plain
    | Content-Transfer-Encoding: 7bit
    | From: (Steven Cheng[MSFT])
    | Organization: Microsoft
    | Date: Fri, 11 Nov 2005 01:44:44 GMT
    | Subject: Re: asp.net 2: Re-use code with
    ClientScript.RegisterStartupScript
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | Lines: 108
    | Path: TK2MSFTNGXA02.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:357242
    | NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
    |
    | Thanks for your response Sck10,
    |
    | What's the class of the "SetFocusControl" method, from your code snippet
    ,
    | the "SetFocusControl" method is a static function so that it can not
    access
    | its container class's "Me" property. In addition, only class derived
    from
    | System.Web.UI.Control has the "Page" propety... Is this class only a
    | helper component class? If so, you need to use the passed in "Control"
    | parameter 's "Page" property to register the script, e.g:
    |
    | =========================
    | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    | Dim Script As New System.Text.StringBuilder
    | Dim ClientID As String = FocusControl.ClientID
    | With Script
    | .Append("<script language='javascript'>")
    | .Append("document.getElementById('")
    | .Append(ClientID)
    | .Append("').focus();")
    | .Append("</script>")
    | End With
    |
    |
    | FocusControl.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    | "SetFocusOnControl", Script.ToString())
    | ...................................
    |
    |
    | =====================
    |
    | Also, as I've mentioend in the former message, the "FocusControl" must
    | already been added into a certain page's control structure before we
    access
    | its "Page" property, otherwise, that property remains Nothing.
    |
    | Hope helps. Thanks,
    |
    | Steven Cheng
    | Microsoft Online Support
    |
    | Get Secure! www.microsoft.com/security
    | (This posting is provided "AS IS", with no warranties, and confers no
    | rights.)
    |
    |
    | --------------------
    | | From: "sck10" <>
    | | References: <>
    | <>
    | <>
    | | Subject: Re: asp.net 2: Re-use code with
    | ClientScript.RegisterStartupScript
    | | Date: Thu, 10 Nov 2005 16:28:19 -0600
    | | Lines: 62
    | | X-Priority: 3
    | | X-MSMail-Priority: Normal
    | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
    | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
    | | Message-ID: <>
    | | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
    | | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    | | Xref: TK2MSFTNGXA02.phx.gbl
    | microsoft.public.dotnet.framework.aspnet:357202
    | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | |
    | | Thanks Steve,
    | |
    | | I'm still having problems. I get the following error when trying to use
    | | "Me.Page". Also, I am only using "Imports Microsoft.VisualBasic".
    | |
    | | error: 'Me' is valid only within an instance method.
    | |
    | | ---------------------------------
    | | Imports Microsoft.VisualBasic
    | |
    | | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    | | Dim Script As New System.Text.StringBuilder
    | | Dim ClientID As String = FocusControl.ClientID
    | | With Script
    | | .Append("<script language='javascript'>")
    | | .Append("document.getElementById('")
    | | .Append(ClientID)
    | | .Append("').focus();")
    | | .Append("</script>")
    | | End With
    | | 'Me.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    | | "SetFocusOnControl", Script.ToString())
    | | 'Me.Page.ClientScript.RegisterStartupScript(GetType(String),
    | | "SetFocusOnControl", Script.ToString())
    | |
    | |
    | | End Sub
    | |
    | |
    | | "Steven Cheng[MSFT]" <> wrote in message
    | | news:...
    | | > Thanks for Brock's suggestion.
    | | >
    | | > Hi Sck10,
    | | >
    | | > For Control class, we can access its container page instance through
    the
    | | > "Page" property. Also, if the control is dynamically created and
    added
    | on
    | | > page, please make sure that before we access that Control.Page
    propety,
    | | the
    | | > control has been added into the Page's control structure , otherwise,
    | the
    | | > "Page" property remains Nothing....
    | | >
    | | > Thanks,
    | | >
    | | > Steven Cheng
    | | > Microsoft Online Support
    | | >
    | | > Get Secure! www.microsoft.com/security
    | | > (This posting is provided "AS IS", with no warranties, and confers no
    | | > rights.)
    | | > --------------------
    | |
    | | > |
    | | > | You need to do:
    | | > |
    | | > | Me.Page.ClientScript.RegisterClienbtScriptBlock(...)
    | | > |
    | | > | -Brock
    | | > | DevelopMentor
    | | > | http://staff.develop.com/ballen
    | |
    | |
    | |
    |
    |
     
    Steven Cheng[MSFT], Nov 14, 2005
    #6
  7. sck10

    sck10 Guest

    Thanks Steve,

    I am still having difficulties getting the sub procedure to work once moved
    to the "App_code" folder. When I call the class using "Call
    class_General.SetFocusControl(Me.txtSearch)", I don't get an error, but it
    doesn't go to the control.

    Previously you said,
    > | Also, as I've mentioned in the former message, the "FocusControl" must
    > | already been added into a certain page's control structure before we
    > access its "Page" property, otherwise, that property remains Nothing.


    I was wondering if this is what is happening and how can I test for this?
    Thanks again for any insight.

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    'Set Focus
    Call class_General.SetFocusControl(Me.txtSearch)
    ' this works: Call SetFocusControl(Me.txtSearch)
    End Sub


    This works
    ---------------------
    Public Shared Sub SetFocusControl(ByVal FocusControl As Control)

    'class_General.SetFocusControl(Me.txtTravelerHRID)

    Dim Script As New System.Text.StringBuilder
    Dim ClientID As String = FocusControl.ClientID
    With Script
    .Append("<script language='javascript'>")
    .Append("document.getElementById('")
    .Append(ClientID)
    .Append("').focus();")
    .Append("</script>")
    End With

    FocusControl.Page.ClientScript.RegisterClientScriptBlock(GetType(String),
    "SetFocusOnControl", Script.ToString())

    End Sub


    "App_Code" folder
    -----------------
    Imports Microsoft.VisualBasic

    Public Class class_General
    Public Shared Sub SetFocusControl(ByVal FocusControl As Control)

    'class_General.SetFocusControl(Me.txtTravelerHRID)

    Dim Script As New System.Text.StringBuilder
    Dim ClientID As String = FocusControl.ClientID
    With Script
    .Append("<script language='javascript'>")
    .Append("document.getElementById('")
    .Append(ClientID)
    .Append("').focus();")
    .Append("</script>")
    End With

    FocusControl.Page.ClientScript.RegisterClientScriptBlock(GetType(String),
    "SetFocusOnControl", Script.ToString())

    End Sub

    Thanks again,



    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Sck10,
    >
    > Does the suggestions in my further response helps you? If there're

    anything
    > else we can help, please feel free to post here.
    >
    > Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    > --------------------
    > | X-Tomcat-ID: 32396961
    > | References: <>
    > <>
    > <>
    > <>
    > | MIME-Version: 1.0
    > | Content-Type: text/plain
    > | Content-Transfer-Encoding: 7bit
    > | From: (Steven Cheng[MSFT])
    > | Organization: Microsoft
    > | Date: Fri, 11 Nov 2005 01:44:44 GMT
    > | Subject: Re: asp.net 2: Re-use code with
    > ClientScript.RegisterStartupScript
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > | Message-ID: <>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet
    > | Lines: 108
    > | Path: TK2MSFTNGXA02.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet:357242
    > | NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
    > |
    > | Thanks for your response Sck10,
    > |
    > | What's the class of the "SetFocusControl" method, from your code snippet
    > ,
    > | the "SetFocusControl" method is a static function so that it can not
    > access
    > | its container class's "Me" property. In addition, only class derived
    > from
    > | System.Web.UI.Control has the "Page" propety... Is this class only a
    > | helper component class? If so, you need to use the passed in "Control"
    > | parameter 's "Page" property to register the script, e.g:
    > |
    > | =========================
    > | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    > | Dim Script As New System.Text.StringBuilder
    > | Dim ClientID As String = FocusControl.ClientID
    > | With Script
    > | .Append("<script language='javascript'>")
    > | .Append("document.getElementById('")
    > | .Append(ClientID)
    > | .Append("').focus();")
    > | .Append("</script>")
    > | End With
    > |
    > |
    > |

    FocusControl.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    > | "SetFocusOnControl", Script.ToString())
    > | ...................................
    > |
    > |
    > | =====================
    > |
    > | Also, as I've mentioend in the former message, the "FocusControl" must
    > | already been added into a certain page's control structure before we
    > access
    > | its "Page" property, otherwise, that property remains Nothing.
    > |
    > | Hope helps. Thanks,
    > |
    > | Steven Cheng
    > | Microsoft Online Support
    > |
    > | Get Secure! www.microsoft.com/security
    > | (This posting is provided "AS IS", with no warranties, and confers no
    > | rights.)
    > |
    > |
    > | --------------------
    > | | From: "sck10" <>
    > | | References: <>
    > | <>
    > | <>
    > | | Subject: Re: asp.net 2: Re-use code with
    > | ClientScript.RegisterStartupScript
    > | | Date: Thu, 10 Nov 2005 16:28:19 -0600
    > | | Lines: 62
    > | | X-Priority: 3
    > | | X-MSMail-Priority: Normal
    > | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
    > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
    > | | Message-ID: <>
    > | | Newsgroups: microsoft.public.dotnet.framework.aspnet
    > | | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
    > | | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    > | | Xref: TK2MSFTNGXA02.phx.gbl
    > | microsoft.public.dotnet.framework.aspnet:357202
    > | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > | |
    > | | Thanks Steve,
    > | |
    > | | I'm still having problems. I get the following error when trying to

    use
    > | | "Me.Page". Also, I am only using "Imports Microsoft.VisualBasic".
    > | |
    > | | error: 'Me' is valid only within an instance method.
    > | |
    > | | ---------------------------------
    > | | Imports Microsoft.VisualBasic
    > | |
    > | | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    > | | Dim Script As New System.Text.StringBuilder
    > | | Dim ClientID As String = FocusControl.ClientID
    > | | With Script
    > | | .Append("<script language='javascript'>")
    > | | .Append("document.getElementById('")
    > | | .Append(ClientID)
    > | | .Append("').focus();")
    > | | .Append("</script>")
    > | | End With
    > | | 'Me.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    > | | "SetFocusOnControl", Script.ToString())
    > | | 'Me.Page.ClientScript.RegisterStartupScript(GetType(String),
    > | | "SetFocusOnControl", Script.ToString())
    > | |
    > | |
    > | | End Sub
    > | |
    > | |
    > | | "Steven Cheng[MSFT]" <> wrote in message
    > | | news:...
    > | | > Thanks for Brock's suggestion.
    > | | >
    > | | > Hi Sck10,
    > | | >
    > | | > For Control class, we can access its container page instance through
    > the
    > | | > "Page" property. Also, if the control is dynamically created and
    > added
    > | on
    > | | > page, please make sure that before we access that Control.Page
    > propety,
    > | | the
    > | | > control has been added into the Page's control structure ,

    otherwise,
    > | the
    > | | > "Page" property remains Nothing....
    > | | >
    > | | > Thanks,
    > | | >
    > | | > Steven Cheng
    > | | > Microsoft Online Support
    > | | >
    > | | > Get Secure! www.microsoft.com/security
    > | | > (This posting is provided "AS IS", with no warranties, and confers

    no
    > | | > rights.)
    > | | > --------------------
    > | |
    > | | > |
    > | | > | You need to do:
    > | | > |
    > | | > | Me.Page.ClientScript.RegisterClienbtScriptBlock(...)
    > | | > |
    > | | > | -Brock
    > | | > | DevelopMentor
    > | | > | http://staff.develop.com/ballen
    > | |
    > | |
    > | |
    > |
    > |
    >
     
    sck10, Nov 16, 2005
    #7
  8. Thanks for your followup Sck10,

    In your case, I think we can check the following things:
    1. Make sure when you use the Page.SetFocus function, the passed in
    Control Parameter instance's ClientID has be the actual value which will be
    rendered out as its "id" in client side html element(you can print out this
    and view the rendered html to compare it...)

    2. Check the rendered javascript code in the rendered html in clientside
    browser to see whether the clientside code is registered correctly...

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)



    --------------------
    | From: "sck10" <>
    | References: <>
    <>
    <>
    <>
    <>
    <>
    | Subject: Re: asp.net 2: Re-use code with
    ClientScript.RegisterStartupScript
    | Date: Wed, 16 Nov 2005 17:37:20 -0600
    | Lines: 256
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:358781
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Thanks Steve,
    |
    | I am still having difficulties getting the sub procedure to work once
    moved
    | to the "App_code" folder. When I call the class using "Call
    | class_General.SetFocusControl(Me.txtSearch)", I don't get an error, but it
    | doesn't go to the control.
    |
    | Previously you said,
    | > | Also, as I've mentioned in the former message, the "FocusControl" must
    | > | already been added into a certain page's control structure before we
    | > access its "Page" property, otherwise, that property remains Nothing.
    |
    | I was wondering if this is what is happening and how can I test for this?
    | Thanks again for any insight.
    |
    | Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    | 'Set Focus
    | Call class_General.SetFocusControl(Me.txtSearch)
    | ' this works: Call SetFocusControl(Me.txtSearch)
    | End Sub
    |
    |
    | This works
    | ---------------------
    | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    |
    | 'class_General.SetFocusControl(Me.txtTravelerHRID)
    |
    | Dim Script As New System.Text.StringBuilder
    | Dim ClientID As String = FocusControl.ClientID
    | With Script
    | .Append("<script language='javascript'>")
    | .Append("document.getElementById('")
    | .Append(ClientID)
    | .Append("').focus();")
    | .Append("</script>")
    | End With
    |
    |
    FocusControl.Page.ClientScript.RegisterClientScriptBlock(GetType(String),
    | "SetFocusOnControl", Script.ToString())
    |
    | End Sub
    |
    |
    | "App_Code" folder
    | -----------------
    | Imports Microsoft.VisualBasic
    |
    | Public Class class_General
    | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    |
    | 'class_General.SetFocusControl(Me.txtTravelerHRID)
    |
    | Dim Script As New System.Text.StringBuilder
    | Dim ClientID As String = FocusControl.ClientID
    | With Script
    | .Append("<script language='javascript'>")
    | .Append("document.getElementById('")
    | .Append(ClientID)
    | .Append("').focus();")
    | .Append("</script>")
    | End With
    |
    |
    FocusControl.Page.ClientScript.RegisterClientScriptBlock(GetType(String),
    | "SetFocusOnControl", Script.ToString())
    |
    | End Sub
    |
    | Thanks again,
    |
    |
    |
    | "Steven Cheng[MSFT]" <> wrote in message
    | news:...
    | > Hi Sck10,
    | >
    | > Does the suggestions in my further response helps you? If there're
    | anything
    | > else we can help, please feel free to post here.
    | >
    | > Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | > --------------------
    | > | X-Tomcat-ID: 32396961
    | > | References: <>
    | > <>
    | > <>
    | > <>
    | > | MIME-Version: 1.0
    | > | Content-Type: text/plain
    | > | Content-Transfer-Encoding: 7bit
    | > | From: (Steven Cheng[MSFT])
    | > | Organization: Microsoft
    | > | Date: Fri, 11 Nov 2005 01:44:44 GMT
    | > | Subject: Re: asp.net 2: Re-use code with
    | > ClientScript.RegisterStartupScript
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | > | Message-ID: <>
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | > | Lines: 108
    | > | Path: TK2MSFTNGXA02.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet:357242
    | > | NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
    | > |
    | > | Thanks for your response Sck10,
    | > |
    | > | What's the class of the "SetFocusControl" method, from your code
    snippet
    | > ,
    | > | the "SetFocusControl" method is a static function so that it can not
    | > access
    | > | its container class's "Me" property. In addition, only class derived
    | > from
    | > | System.Web.UI.Control has the "Page" propety... Is this class only
    a
    | > | helper component class? If so, you need to use the passed in
    "Control"
    | > | parameter 's "Page" property to register the script, e.g:
    | > |
    | > | =========================
    | > | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    | > | Dim Script As New System.Text.StringBuilder
    | > | Dim ClientID As String = FocusControl.ClientID
    | > | With Script
    | > | .Append("<script language='javascript'>")
    | > | .Append("document.getElementById('")
    | > | .Append(ClientID)
    | > | .Append("').focus();")
    | > | .Append("</script>")
    | > | End With
    | > |
    | > |
    | > |
    | FocusControl.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    | > | "SetFocusOnControl", Script.ToString())
    | > | ...................................
    | > |
    | > |
    | > | =====================
    | > |
    | > | Also, as I've mentioend in the former message, the "FocusControl" must
    | > | already been added into a certain page's control structure before we
    | > access
    | > | its "Page" property, otherwise, that property remains Nothing.
    | > |
    | > | Hope helps. Thanks,
    | > |
    | > | Steven Cheng
    | > | Microsoft Online Support
    | > |
    | > | Get Secure! www.microsoft.com/security
    | > | (This posting is provided "AS IS", with no warranties, and confers no
    | > | rights.)
    | > |
    | > |
    | > | --------------------
    | > | | From: "sck10" <>
    | > | | References: <>
    | > | <>
    | > | <>
    | > | | Subject: Re: asp.net 2: Re-use code with
    | > | ClientScript.RegisterStartupScript
    | > | | Date: Thu, 10 Nov 2005 16:28:19 -0600
    | > | | Lines: 62
    | > | | X-Priority: 3
    | > | | X-MSMail-Priority: Normal
    | > | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
    | > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
    | > | | Message-ID: <>
    | > | | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | > | | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
    | > | | Path:
    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    | > | | Xref: TK2MSFTNGXA02.phx.gbl
    | > | microsoft.public.dotnet.framework.aspnet:357202
    | > | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | > | |
    | > | | Thanks Steve,
    | > | |
    | > | | I'm still having problems. I get the following error when trying to
    | use
    | > | | "Me.Page". Also, I am only using "Imports Microsoft.VisualBasic".
    | > | |
    | > | | error: 'Me' is valid only within an instance method.
    | > | |
    | > | | ---------------------------------
    | > | | Imports Microsoft.VisualBasic
    | > | |
    | > | | Public Shared Sub SetFocusControl(ByVal FocusControl As Control)
    | > | | Dim Script As New System.Text.StringBuilder
    | > | | Dim ClientID As String = FocusControl.ClientID
    | > | | With Script
    | > | | .Append("<script language='javascript'>")
    | > | | .Append("document.getElementById('")
    | > | | .Append(ClientID)
    | > | | .Append("').focus();")
    | > | | .Append("</script>")
    | > | | End With
    | > | |
    'Me.Page.ClientScript.RegisterClienbtScriptBlock(GetType(String),
    | > | | "SetFocusOnControl", Script.ToString())
    | > | | 'Me.Page.ClientScript.RegisterStartupScript(GetType(String),
    | > | | "SetFocusOnControl", Script.ToString())
    | > | |
    | > | |
    | > | | End Sub
    | > | |
    | > | |
    | > | | "Steven Cheng[MSFT]" <> wrote in message
    | > | | news:...
    | > | | > Thanks for Brock's suggestion.
    | > | | >
    | > | | > Hi Sck10,
    | > | | >
    | > | | > For Control class, we can access its container page instance
    through
    | > the
    | > | | > "Page" property. Also, if the control is dynamically created and
    | > added
    | > | on
    | > | | > page, please make sure that before we access that Control.Page
    | > propety,
    | > | | the
    | > | | > control has been added into the Page's control structure ,
    | otherwise,
    | > | the
    | > | | > "Page" property remains Nothing....
    | > | | >
    | > | | > Thanks,
    | > | | >
    | > | | > Steven Cheng
    | > | | > Microsoft Online Support
    | > | | >
    | > | | > Get Secure! www.microsoft.com/security
    | > | | > (This posting is provided "AS IS", with no warranties, and confers
    | no
    | > | | > rights.)
    | > | | > --------------------
    | > | |
    | > | | > |
    | > | | > | You need to do:
    | > | | > |
    | > | | > | Me.Page.ClientScript.RegisterClienbtScriptBlock(...)
    | > | | > |
    | > | | > | -Brock
    | > | | > | DevelopMentor
    | > | | > | http://staff.develop.com/ballen
    | > | |
    | > | |
    | > | |
    | > |
    | > |
    | >
    |
    |
    |
     
    Steven Cheng[MSFT], Nov 17, 2005
    #8
    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?bWFj?=

    Remove clientscript block from page (ASP.NET 1.1)

    =?Utf-8?B?bWFj?=, Jun 13, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,948
    Jacob
    Jun 20, 2006
  2. Nathan Sokalski
    Replies:
    4
    Views:
    61,058
    Alessandro Zifiglio
    Jul 3, 2006
  3. sck10
    Replies:
    3
    Views:
    7,631
  4. =?Utf-8?B?Qw==?=

    ClientScript.RegisterStartupScript

    =?Utf-8?B?Qw==?=, Aug 1, 2006, in forum: ASP .Net
    Replies:
    10
    Views:
    170,733
    zatch
    Nov 14, 2007
  5. HockeyFan
    Replies:
    1
    Views:
    4,319
    George Ter-Saakov
    Jul 20, 2007
Loading...

Share This Page