简体   繁体   中英

focus on the user control in C#

I have a user control(error control) in a master page. by default the user control is hidden(visible false)

on click of a submit button i am showing the error control.Now on post back I want to bring focus to this control.I am writing the java script in register Startup script as following:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Scroll", "window.scrollTo(0, 0)", true);

on postback page goes up shows this control and then comes back to its previous scroll position.

Have you set MaintainScrollPositionOnPostback to true in the page directive at the top of your page/master page or in the <pages> element of your web.config?

You could also look at using the Focus method of the control in your postback handler

Edit to clarify:

If any of these are set to true then it's very likely that they will override your startup script to send the page back to the top of the page - have you tried setting it to false in either the Page directive, or on the Page object in the same code that displays the error control?

Finally I have solved this issue. following is the way i am doing it...

i have added a text box of width Zero% in the same where my user control(error control is placed)

now whenever an error occurs I call following method.

public void SetFocus()
        {
            ScriptManager3.SetFocus(txtFocus.ClientID);
        }

although error control is not focused in reality this clever work around helps me achieve my goal.

hope it is helpful for others

 private void Control_GotFocus(object sender, EventArgs e)
    {
        Console.WriteLine("Control GotFocus : " + ((sender as Control).Name));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM