【问题标题】:Focus on textbox inside gridview and updatepanel专注于gridview和updatepanel中的文本框
【发布时间】:2014-03-05 15:20:30
【问题描述】:

在为一件看似简单的事情而大发雷霆之后,我来了:

我有一个 ASP.NET GridView 包裹在 UpdatePanel 中。一旦用户在最后一行的最后一个文本框中输入内容并移出,我将在 gridview 中添加一个新行。这是通过触发文本框的OnTextChanged 事件并将其AutoPostBack 属性设置为true 在服务器端完成的。该位使用UpdatePanel 进行了Ajaxified。

我的简单要求是:部分刷新结束后,我需要将焦点设置在新添加行的第一个文本框上

我尝试了什么:

//Get the newly added row (basically the last row)
GridViewRow newRow = myGridView.Rows[myGridView.Rows.Count - 1];
//Get the TextBox control on which I want to set focus
TextBox textBox = newRow.FindControl("txtMyCode") as TextBox;
//Set the focus
ScriptManager.GetCurrent(this.Page).SetFocus(textBox);

在单步执行代码时,上面的每一行都会执行,但是,当部分回发完成时,文本框没有焦点。 ScriptManager.GetCurrent(this.Page) 返回 AjaxControlToolkitScriptManager 的实例,该实例位于母版页上。

有什么想法吗?

【问题讨论】:

  • 您是如何添加行以及何时/何地添加的?尝试改用RowDataBound

标签: asp.net gridview asp.net-ajax updatepanel scriptmanager


【解决方案1】:

如果您无法从服务器站点设置焦点,您可以编写 jquery 来实现这一点。

<script type="text/javascript">


Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(BindEvents);

function BindEvents() {

    //write your logic here lets say 
        //if your grid view empty row is visible then set focus


}
</script>

【讨论】:

    【解决方案2】:

    您可以使用javascript pageLoad 函数,该函数是每次刷新ajax 更新面板的内容时触发的特定于Ajax 的函数。在该函数内触发文本框上的“焦点”。

    function pageLoad() {
    document.getElementById('ClientIdOfYourTextBox').focus(); }
    

    您需要在javascript中获取新添加的文本框的ClientId。您可以通过在添加后将其保存在隐藏字段服务器端然后获取隐藏字段的“值”客户端或通过构建 jquery 表达式来实现它(例如,从所有 id 包含“txtMyCode”的“输入”控件中获取最后一个)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多