【问题标题】:Maintain tab order even after postback即使在回发之后也保持标签顺序
【发布时间】:2012-09-06 05:50:35
【问题描述】:

这是一个场景,即使在回发之后,我也需要保持页面中的 Tab 键顺序。

我有一个带有 3 个文本框的用户控件,它与其他控件一起放置在一个 aspx 页面中。在测试框的 onBlur 中,我正在执行 __doPostBack 以将一些数据传递给服务器并在父页面上进行处理,因此选项卡顺序丢失。是否可以在回发中保持标签顺序,我从父页面标签到控件中的文本框,然后再次标签到下一个父页面控件。

我尝试使用下面的代码将焦点设置到用户控件中的下一个文本框,但这会导致代码被调用,即使用户只是通过单击页面将焦点移出控件。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as UserControl);
            if (wcICausedPostBack != null)
            {
                int indx = wcICausedPostBack.TabIndex;
                Panel selectedPanel = null;
                if (Panel1.Visible)
                {
                    selectedPanel = Panel1;
                }
                else if (Panel2.Visible)
                {
                    selectedPanel = Panel2;
                }
                if (selectedPanel != null)
                {

                    var ctrl = from control in selectedPanel.Controls.OfType<WebControl>()
                               where control.TabIndex == (indx + 1)
                               select control;
                    if (ctrl.Count() > 0)
                    {
                        ctrl.First().Focus();
                    }
                }
            }
        }
    }

    protected Control GetControlThatCausedPostBack(UserControl page)
    {
        Control control = null;
        string ctrlname = string.Empty;
        UserAction updateAction = CurrentUserAction();
        if (updateAction != null)
        {
            if (!string.IsNullOrEmpty(updateAction.Data))
            {
                string[] splitSting = updateAction.Data.Split('$');
                ctrlname = splitSting[splitSting.Count() - 1];
            }
        }
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }

【问题讨论】:

    标签: c# asp.net user-controls


    【解决方案1】:

    您可以使用 jQuery 将当前聚焦的字段 id 存储在隐藏控件中。

    将其存储在隐藏控件中将在回发时保持它。
    您可以使用该输入 id 查找下一个输入并使用 jQuery 再次为其设置焦点。

    $("input").focus(function() {
        $("#currectFocusField").val($(this).attr("id"));
    });
    
    $(document).ready(function(){
        var lastFocusedInput = $("#currectFocusField").val();
    });
    

    【讨论】:

    • 您提供的代码,这将始终被调用,但我只需要在从文本框切换时设置焦点。
    • 不,它不会总是被调用。当你 tabout(专注于下一个)输入和准备好(回发后)时,它将被调用
    猜你喜欢
    • 2023-01-10
    • 2012-02-02
    • 2015-10-10
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2020-09-03
    相关资源
    最近更新 更多