简体   繁体   English

如何在回发后维护标签顺序

[英]How to maintain tab order after postback

The requirement is for some calculation to happen on entering a value in the textbox and since calculation is same ontextchanged is linked to the same event. 要求是在输入文本框中的值时进行某些计算,并且由于计算相同,因此textchanged链接到同一事件。 When I tab out it neatly goes to next control and does a postback to Calculate. 当我选中它时,它会整齐地转到下一个控件并进行回发计算。

Now after the postback and the server side is called and executed, the tab order is messed up and on tab it does not bring focus to the correct control. 现在,在回发和服务器端被调用并执行之后,选项卡顺序混乱并且在选项卡上它不会将焦点带到正确的控件上。 It always points to the URL in the browser window. 它始终指向浏览器窗口中的URL。

Please let me know how do i retrieve the control which should be next in focus after the postback using the tabIndex. 请让我知道如何使用tabIndex检索回发后应该是下一个焦点的控件。

  <asp:TextBox ID="txtDiscount" runat="server" CssClass="NormalTextBox" TabIndex="45"
                                    MaxLength="3" OnTextChanged="btnCalculatePrice_Click" AutoPostBack="True"></asp:TextBox>


  protected void btnCalculatePrice_Click(object sender, EventArgs e)
    {....

} }

I tried the below code but didnt know how to fetch the exact control 我尝试了下面的代码,但不知道如何获取确切的控件

   if(sender!=null)
        {
            WebControl reqCtrl = (WebControl)sender;
            int taborder = reqCtrl.TabIndex;
            int nexttabOrder = taborder + 1;

        }

Use below code in order to set focus to next control after post back. 使用以下代码,以便在回发后将焦点设置为下一个控件。

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as Page);
int indx = wcICausedPostBack.TabIndex;
var ctrl = from control in wcICausedPostBack.Parent.Controls.OfType<WebControl>()
where control.TabIndex > indx
select control;
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus();
}
}
protected Control GetControlThatCausedPostBack(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
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;
}

Here is a similar (app code) solution that works for controls that may not necessarily share the same parent. 这是一个类似的(应用程序代码)解决方案,适用于可能不一定共享同一父级的控件。 It assumes the tab indexes are exactly 1 number apart. 它假设标签索引正好相差1个数字。

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        SetFocusAfterPostBack();
    }
}

public static void SetFocusAfterPostBack()
{
    var page = HttpContext.Current.Handler as Page;
    if (page == null)
    {
        return;
    }
    var postBackCtl = page.FindControl(HttpContext.Current.Request.Form["__EVENTTARGET"]) as WebControl;
    if (postBackCtl == null || postBackCtl.TabIndex == 0)
    {
        return;
    }
    var ctl = GetCtlByTabIndex(page, postBackCtl.TabIndex + 1);
    if (ctl != null)
    {
        ctl.Focus();
    }
}
private static WebControl GetCtlByTabIndex(Control ParentCtl, int TabIndex)
{
    foreach (Control ctl in ParentCtl.Controls)
    {
        var webCtl = ctl as WebControl;
        if (webCtl != null)
        {
            if (webCtl.TabIndex == TabIndex)
            {
                return webCtl;
            }
        }
        var retCtl = GetCtlByTabIndex(ctl, TabIndex);
        if (retCtl != null)
        {
            return retCtl;
        }
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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