Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将焦点放回gridview';回发后返回选定的行_Asp.net_Gridview - Fatal编程技术网

Asp.net 将焦点放回gridview';回发后返回选定的行

Asp.net 将焦点放回gridview';回发后返回选定的行,asp.net,gridview,Asp.net,Gridview,在选定一行生成回发后,是否可以将焦点放回gridview行 我试图在gridview行上添加一个onkeydown处理程序,以便使用键盘进行导航。我认为,我的问题是,在第一次回发后,选定的单元格会失去焦点,因此下一个关键点不会被单元格捕捉到 我有以下代码 网格视图 <asp:GridView runat="server" ID="gdvPersons" AutoGenerateColumns="false" onrowcreated="gdvPersons_Row

在选定一行生成回发后,是否可以将焦点放回gridview行

我试图在gridview行上添加一个onkeydown处理程序,以便使用键盘进行导航。我认为,我的问题是,在第一次回发后,选定的单元格会失去焦点,因此下一个关键点不会被单元格捕捉到

我有以下代码

网格视图

    <asp:GridView runat="server" ID="gdvPersons" AutoGenerateColumns="false" 
        onrowcreated="gdvPersons_RowCreated" onselectedindexchanged="gdvPersons_SelectedIndexChanged">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <%# ((GridviewFocus.Person) Container.DataItem).Name %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Age">
                <ItemTemplate>
                    <%# ((GridviewFocus.Person) Container.DataItem).Age %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

背后的代码

    protected void Page_Load(object sender, EventArgs e)
    {
        var persons = new List<Person> {new Person() {Name = "Fikre", Age = 24}, 
                                        new Person() {Name = "Mike", Age = 29},
                                        new Person() {Name = "Mark", Age = 35}};
        gdvPersons.DataSource = persons;
        gdvPersons.DataBind();
    }

    protected void gdvPersons_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Attributes.Add("onkeydown", ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.DataItemIndex));
    }

    protected void gdvPersons_SelectedIndexChanged(object sender, EventArgs e)
    {
        gdvPersons.SelectedRow.Focus();
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
var persons=new List{new Person(){Name=“Fikre”,Age=24},
新人(){Name=“Mike”,年龄=29},
新人(){Name=“Mark”,年龄=35};
gdvPersons.DataSource=个人;
gdvPersons.DataBind();
}
受保护的无效gdvPersons_RowCreated(对象发送者,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
e、 Add(“onkeydown”,ClientScript.GetPostBackEventReference((控制)发送方,“Select$”+e.Row.DataItemIndex));
}
受保护的无效gdvPersons\u SelectedIndexChanged(对象发送方,事件参数e)
{
gdvPersons.SelectedRow.Focus();
}

在onkeydown脚本代码中,将单元格的id复制到隐藏的输入字段中

<input type="text" id="gridviewcell_id" onkeydown="lastcell.value = this.id" />
<input type="hidden" id="lastcell" runat="server" />
这将使页面在加载后执行以下脚本,并且控件应检索焦点:

var ctrl = document.getElementById("yourid"); 
ctrl.focus();

在.aspx文件中的
@Page
指令中添加
maintaintScrollPositionOnPostback=“true”
,或将其添加到
web.config中的
system.web/pages部分

<system.web>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" maintainScrollPositionOnPostBack="true"/>
</system.web>

这也适用于导航到上一页的大表格,例如

当网页发布回服务器时,用户将返回到页面顶部。在长网页上,这意味着用户必须将页面滚动回页面上的最后一个位置


我尝试过您的解决方案,但我在gridview的行ID方面遇到了问题。我在问题中加入了我的原始代码。您发布的代码中没有编辑字段。那么,您打算如何进行导航?您能否提供一个更完整的示例,以及您使用的是.net3.5还是4.0?我正在将onkeydown事件添加到gdvPersons_RowCreated事件中的每一行。我的代码的问题是,表行需要一个选项卡索引才能聚焦。我还更改了gdvPersons.SelectedRow.Focus();用你的代码来聚焦这一行,所以我把它标记为答案。谢谢
<system.web>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" maintainScrollPositionOnPostBack="true"/>
</system.web>