更新表单后,继续关注ajax更新面板

时间:2022-08-25 17:18:02

I've have a formview that is designed to update it's datasource each time a text_changed event or dropdown list has been changed. On every text_changed event and equivalent for other controls I call the formView.UpdateItem method and this causes the form to lose focus from the control I have tabbed down to.

我有一个formview,旨在每次更改text_changed事件或下拉列表时更新它的数据源。在每个text_changed事件和其他控件的等效事件上,我调用formView.UpdateItem方法,这会导致表单从我已选中的控件中失去焦点。

I've tried to implement this method but I get unknown runtime errors after I have tried to select a control for the second time.

我试图实现此方法,但在尝试第二次选择控件后,我得到了未知的运行时错误。

I can't help but think there is another way of dealing with this.

我不禁想到还有另一种方法可以解决这个问题。

How would you go about solving this issue?

你会如何解决这个问题?

2 个解决方案

#1


For things like this, I often stash the value in an asp:Hidden control (input type="hidden") using the javascript and then add a pageLoad function (in the javascript) to parse that field and then set the focus. This way the id of the focused control is persisted through the postback.

对于这样的事情,我经常使用javascript将值隐藏在asp:隐藏控件(输入类型=“隐藏”)中,然后添加一个pageLoad函数(在javascript中)来解析该字段然后设置焦点。这样,聚焦控件的id通过回发持久化。

Something like this (pseudocode):

像这样的东西(伪代码):

<input type="hidden" id="focusHolder" />

function onSubmit (registered via ClientScript.RegisterOnSubmitStatemnet) {
grab the target and stash id in #focusHolder
}

function pageLoad() {
$get($get('focusHolder').value).focus();
}

#2


You can do it on server side too. Here is an example of how to put focus on the control that caused the asynchronous postback:

你也可以在服务器端做到这一点。下面是一个如何将焦点放在导致异步回发的控件上的示例:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
            {
                string IDPostbackCtrl = GetAsyncPostBackControlID(Page, Page.Request);
                ScriptManager.GetCurrent(Page).SetFocus(IDPostbackCtrl);
            }
        }
    }

    public string GetAsyncPostBackControlID(Page page, HttpRequest request)
    {
        string smUniqueId = ScriptManager.GetCurrent(page).UniqueID;
        string smFieldValue = request.Form[smUniqueId];

        if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains('|'))
            return smFieldValue.Split('|')[1];

        return String.Empty;
    }

#1


For things like this, I often stash the value in an asp:Hidden control (input type="hidden") using the javascript and then add a pageLoad function (in the javascript) to parse that field and then set the focus. This way the id of the focused control is persisted through the postback.

对于这样的事情,我经常使用javascript将值隐藏在asp:隐藏控件(输入类型=“隐藏”)中,然后添加一个pageLoad函数(在javascript中)来解析该字段然后设置焦点。这样,聚焦控件的id通过回发持久化。

Something like this (pseudocode):

像这样的东西(伪代码):

<input type="hidden" id="focusHolder" />

function onSubmit (registered via ClientScript.RegisterOnSubmitStatemnet) {
grab the target and stash id in #focusHolder
}

function pageLoad() {
$get($get('focusHolder').value).focus();
}

#2


You can do it on server side too. Here is an example of how to put focus on the control that caused the asynchronous postback:

你也可以在服务器端做到这一点。下面是一个如何将焦点放在导致异步回发的控件上的示例:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
            {
                string IDPostbackCtrl = GetAsyncPostBackControlID(Page, Page.Request);
                ScriptManager.GetCurrent(Page).SetFocus(IDPostbackCtrl);
            }
        }
    }

    public string GetAsyncPostBackControlID(Page page, HttpRequest request)
    {
        string smUniqueId = ScriptManager.GetCurrent(page).UniqueID;
        string smFieldValue = request.Form[smUniqueId];

        if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains('|'))
            return smFieldValue.Split('|')[1];

        return String.Empty;
    }