开发者

Set Focus to an Element from code behind after submit in the same page

I want to set focus to an element (a button) after the user clicks on Submit, but the focus is not on a 开发者_StackOverflow社区new page it is in the same page. What is happening is that when they click submit i am evaluating a few conditions.. if one of them is met I send them back to the same page (or do not re-direct) in other terms but it still postsback so, when it postsback I again after the submit, i want to set the focus to this item.. how can i do this?


So you want to set the focus of the page to a WebControl on the page? i.e. a <asp:Button>?

You can use ControlId.Focus()

EDIT

If you are talking about the Scroll position after postback, then you could try this

<%@ Page MaintainScrollPositionOnPostback="true" %>

Now I have never used it before or tried it, so not sure if it works.


Despite your question doesn't seem too clear to me, I am assuming that you want to set focus to the asp.net control, so you can do

protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
           if(vartemp === vartemp2) //assuming that you want to set focus when specific condition meets
             myButton1.Focus();
        }
    }


Use a hiddenfield and set its value with the controlId of the control you are validating before making the postback. and in the pageload event you can check the value of the hidden field and focus that control.

Or you can use validationcontrols for validating.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜