locked
Set Focus After Button Click RRS feed

  • Question

  • User-1314346660 posted

    Hello Experts,

    I have a radio button list. Its a simple Yes/No.

    When the user clicks "No" then a hidden panel appears with a textbox to add additional information. When they click "No" then nothing additional happens. The problem I have is that when the click "Yes" then the AutoPostback occurs and sets the focus to the top of my web page. I want the focus to stay on the radio button.

    I work out how to do this and have googled for a few hours without success. I would really appreciate any guidance on this please! Thanks very much in advance.

    Billson 3000

    My code is below:

                            <asp:RadioButtonList ID="rdbList1" runat="server" AutoPostBack="True" RepeatDirection="Horizontal" Width="30%" OnTextChanged="rdbList1_TextChanged">
                                <asp:ListItem>No</asp:ListItem>
                                <asp:ListItem>Yes</asp:ListItem>
                            </asp:RadioButtonList>
    

    And code behind:

                    string focus = Convert.ToString(rdbList1.Text);
                    if (focus == "Yes")
                    {
                        Panel5.Focus();
                    }
                    else
                    {
                        rdbList1.Focus();
                    }

    Wednesday, November 14, 2018 3:31 PM

Answers

  • User-1314346660 posted

    Sorry experts. After 3 hours of fiddling I cracked it and set the focus elsewhere.embarassed

    string str = Convert.ToString(rdbList1.Text);
    if (str == "Yes")
    {
    Textbox1.Focus();
    Panel5.Visible = true;
    }
    else
    {
    Button2.Focus();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2018 3:42 PM
  • User475983607 posted

    What does focus on the radio button mean?  Do you actually want the page to scroll to the radio button?

    If so, try adding MaintainScrollPositionOnPostBack to the page directive.

    MaintainScrollPositionOnPostBack = "true"

    https://stackoverflow.com/questions/5147942/how-to-maintain-scroll-position-on-autopostback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2018 3:43 PM

All replies

  • User-1314346660 posted

    Sorry experts. After 3 hours of fiddling I cracked it and set the focus elsewhere.embarassed

    string str = Convert.ToString(rdbList1.Text);
    if (str == "Yes")
    {
    Textbox1.Focus();
    Panel5.Visible = true;
    }
    else
    {
    Button2.Focus();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2018 3:42 PM
  • User475983607 posted

    What does focus on the radio button mean?  Do you actually want the page to scroll to the radio button?

    If so, try adding MaintainScrollPositionOnPostBack to the page directive.

    MaintainScrollPositionOnPostBack = "true"

    https://stackoverflow.com/questions/5147942/how-to-maintain-scroll-position-on-autopostback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 14, 2018 3:43 PM