0
以下是方案,即使在回发后,我仍需要在页面中维护标签顺序。即使在回发之后仍然维护标签顺序
我有一个带有3个文本框的用户控件,这个控件放在一个带有其他控件的aspx页面中。在测试框的onBlur中,我正在做一个__doPostBack将一些数据传递给服务器,并在父页面上处理这个数据,因此Tab键顺序丢失了。是否有可能在回发中维护标签顺序,其中我从父页面到控件中的文本框进行选项卡标签,然后再次标签到下一个父页面控件。
我尝试使用下面的代码将焦点设置到usercontrol中的下一个文本框中,但是这会导致代码被调用,即使用户只是通过单击页面而将焦点置于控件之外。
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;
}
您提供的代码,这将被称为总是,但我只需要从文本框中切换时设置焦点。 – Arvind
不,它并不总是被调用。它将在您列出(重点放在下一个)输入和准备好(在回发之后)时被调用, – nunespascal