Re: Set Focus

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jerry Pisk (jerryiii_at_hotmail.com)
Date: 09/01/04


Date: Tue, 31 Aug 2004 22:41:35 -0700

No it doesn't. You're calling it from the server side handler, rendering the
script into the page at a place where the form is not parsed. I got a little
confused here, I thought you want to handle this on the client side, not
through a postback. You should put the script either into the client side
handler or render it after the form is closed. I don't like the whole
postback architecture in Asp.Net so I won't be able to help you much here.
But if you want to do this using HTML and client side scripts it's really
simple, all you need is to render something like this:

<select onchange="document.forms['myForm'].elemwnts['txtTest'].focus();">
    <!-- Your options are here -->
</select>
<input type="text" name="txtTest" />

Jerry

"Rookie" <Rookie@discussions.microsoft.com> wrote in message
news:70BE692D-7F34-49A6-B80F-7B5AAE3541F6@microsoft.com...
> Hi Jerry,
>
> This is what I posted in my second message:
>
> Private Sub lstMyList_SelectedIndexChanged(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles lstMyList.SelectedIndexChanged
> <Here goes my listbox vb code where I used dataset and blah blah blah and
> then...>
> Dim stScript As String
> stScript = "<script language=""JavaScript"">"
> stScript +=
> "document.forms[""myForm""].elements[""txtTest""].focus();"
> stScript += "<"
> stScript += "/"
> stScript += "script>"
> If (Not IsClientScriptBlockRegistered("jsfocus")) Then
> RegisterClientScriptBlock("jsfocus", stScript)
> End If
>
> End Sub
>
> Doesn't it mean I am calling it from the listbox's event handler?
>
> Rookie
>
> "Jerry Pisk" wrote:
>
>> So you're saying you're calling it from your listbox's event handler?
>> That's
>> not what you posted. Either make that code the listbox's event handler
>> (onchange probably) or put it in a function and call it when ready. The
>> code
>> you posted does neither, it simply renders the script to be executed as
>> the
>> form is being loaded, before all the contents is processed - which is
>> going
>> to fail.
>>
>> It seems that you might want to do some research on how client scripts
>> work
>> and when they execute...
>>
>> Jerry
>>
>> "Rookie" <Rookie@discussions.microsoft.com> wrote in message
>> news:DA133FF8-CA79-4916-B40D-473819EEFB71@microsoft.com...
>> > Hi Jerry,
>> > I am doing some task with the listbox before I call the setfocus. If I
>> > put
>> > setfocus function after the form's closing tag, I need to set onclick
>> > for
>> > listbox to the function. This is OK if I dont do anything with the
>> > listbox
>> > except for setting focus to the textbox. However, I perform some data
>> > operations before I want to set focus to the textbox. How do I do that
>> > unless
>> > I render the script dynamically?
>> > Rookie
>> >
>> > "Jerry Pisk" wrote:
>> >
>> >> I found the problem - Page.RegisterClientScriptBlock causes the script
>> >> to
>> >> be
>> >> rendered just after the opening form tag. At that point the browser
>> >> doesn't
>> >> know about any elements inside the form. You need to render the script
>> >> after
>> >> your form's closing tag. Just put the script into the page, there's
>> >> really
>> >> no need to render it dynamically.
>> >>
>> >> Jerry
>>
>>
>>



Relevant Pages

  • Re: asp.net and XHTML
    ... What I meant was to use Me.ClientScript.RegisterClientScriptBlock to render ... this a script while attributes.add doesn't know what you are doing). ... > disable XHTML but it didn't work. ... ASP.NET controls do not render ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: LTT user input
    ... and to that end have taken the existing trace infrastructure ... script for that event (if there's a handler defined for the event ... This gives the script a chance to do whatever Perlish thing it ... syscall totals to individual syscall totals for each pid. ...
    (Linux-Kernel)
  • Re: asp.net and XHTML
    ... >> this a script while attributes.add doesn't know what you are doing). ... >>> disable XHTML but it didn't work. ... In ASP.NET, if controls generate IDs, as occurs in the Repeater, ... you might not want ASP.NET controls to render ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Stopping a while loop with user input ?
    ... >correctness, and begin executing the script UNTIL... ... so you can just install a handler that sets a flag indicating ...
    (comp.lang.c)
  • Re: questions about form action= and onsubmit() handling
    ... I think IE and Firefox get ... With Firebug, ... I don't know what kind of processing and validating your script does... ... from the submit handler alone. ...
    (comp.lang.javascript)