JavaScript events and postbacks

Discussion in 'ASP .Net' started by David Beardsley, Apr 23, 2004.

  1. My web form has one edit box, a button, a list box and a combo box.
    When the button is clicked the text in the edit box is added to the
    list box and the combo box. After the new text is added I want to
    clear the text of the edit box and set focus back to the text box.


    I setup the event here:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    btnAdd.Attributes.Add("OnClick", ECMA.SetFocus("txtNewItem"));
    }
    }


    Relevant html output:

    <input name="txtNewItem" type="text" id="txtNewItem" />
    <input name="TextBox2" type="text" id="TextBox2" style />

    <select name="cbItem" id="cbItem">...</select>

    <input type="submit" name="btnAdd" value="Add" id="btnAdd"
    OnClick="document.getElementById('txtNewItem').focus();"/>

    <select name="lbItem" size="4" id="lbItem">...</select>


    When I run the application the focus is not set as desired. What
    appears to happen is the focus is set momentarily, then the postback
    event fires and the focus shifts again.

    My question is, how do you properly manage the client side focus
    around the postback events?

    TIA!

    David Beardsley
     
    David Beardsley, Apr 23, 2004
    #1
    1. Advertising

  2. Hi David,
    Try this :
    if (!IsPostBack)
    {
    Page.RegisterStartupScript("SetFocus", "<script language= 'Jscript' >
    document.getElementById('txtNewItem').focus(); </script>");

    }

    Hope this helps.

    Regards,

    Marshal Antony

    ..NET Developer

    http://www.dotnetmarshal.com





    "David Beardsley" <> wrote in message
    news:...
    > My web form has one edit box, a button, a list box and a combo box.
    > When the button is clicked the text in the edit box is added to the
    > list box and the combo box. After the new text is added I want to
    > clear the text of the edit box and set focus back to the text box.
    >
    >
    > I setup the event here:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > btnAdd.Attributes.Add("OnClick", ECMA.SetFocus("txtNewItem"));
    > }
    > }
    >
    >
    > Relevant html output:
    >
    > <input name="txtNewItem" type="text" id="txtNewItem" />
    > <input name="TextBox2" type="text" id="TextBox2" style />
    >
    > <select name="cbItem" id="cbItem">...</select>
    >
    > <input type="submit" name="btnAdd" value="Add" id="btnAdd"
    > OnClick="document.getElementById('txtNewItem').focus();"/>
    >
    > <select name="lbItem" size="4" id="lbItem">...</select>
    >
    >
    > When I run the application the focus is not set as desired. What
    > appears to happen is the focus is set momentarily, then the postback
    > event fires and the focus shifts again.
    >
    > My question is, how do you properly manage the client side focus
    > around the postback events?
    >
    > TIA!
    >
    > David Beardsley
     
    Marshal Antony, Apr 23, 2004
    #2
    1. Advertising

  3. I believe that will set the inital focus when the form loads. What I am
    trying to do is (re)set the focus after the button is clicked.

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    David Beardsley, Apr 23, 2004
    #3
  4. David Beardsley

    Teemu Keiski Guest

    Hi,

    there's also free control to achieve this:

    http://www.metabuilders.com/Tools/FirstFocus.aspx

    The control it sets focus to can be specified in code despite that at the
    page is said "after the page has loaded" :)

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke


    "David Beardsley" <> wrote in message
    news:...
    My web form has one edit box, a button, a list box and a combo box.
    When the button is clicked the text in the edit box is added to the
    list box and the combo box. After the new text is added I want to
    clear the text of the edit box and set focus back to the text box.


    I setup the event here:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    btnAdd.Attributes.Add("OnClick", ECMA.SetFocus("txtNewItem"));
    }
    }


    Relevant html output:

    <input name="txtNewItem" type="text" id="txtNewItem" />
    <input name="TextBox2" type="text" id="TextBox2" style />

    <select name="cbItem" id="cbItem">...</select>

    <input type="submit" name="btnAdd" value="Add" id="btnAdd"
    OnClick="document.getElementById('txtNewItem').focus();"/>

    <select name="lbItem" size="4" id="lbItem">...</select>


    When I run the application the focus is not set as desired. What
    appears to happen is the focus is set momentarily, then the postback
    event fires and the focus shifts again.

    My question is, how do you properly manage the client side focus
    around the postback events?

    TIA!

    David Beardsley
     
    Teemu Keiski, Apr 24, 2004
    #4
    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. burn1down

    Javascript postbacks and viewstate

    burn1down, May 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    538
    burn1down
    May 31, 2004
  2. JezB
    Replies:
    5
    Views:
    450
  3. Replies:
    0
    Views:
    454
  4. Ant

    Events & postbacks

    Ant, Mar 25, 2008, in forum: ASP .Net
    Replies:
    5
    Views:
    395
    Michael Nemtsev [MVP]
    Mar 26, 2008
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    387
    Chris
    Aug 30, 2005
Loading...

Share This Page