在回发后将焦点放回gridview选定的行

时间:2010-07-05 18:37:41

标签: asp.net gridview

是否可以将焦点放回到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();
    }

2 个答案:

答案 0 :(得分:0)

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

<input type="text" id="gridviewcell_id" onkeydown="lastcell.value = this.id" />
<input type="hidden" id="lastcell" runat="server" />

上面的例子是普通的html,你必须在gridview中添加正确的onkeydown代码。

在回发(例如onclick)事件处理程序代码中,您可以从隐藏字段值属性中检索id,并在刷新页面后注册javascript以执行。如果你有一个点击按钮来执行回发,你可以这样做:

protected void MyButton_Click(object sender, EventArgs e)
{
   string id = lastcell.Value;
   string script = "var ctrl = document.getElementById('" + lastcell.Value + "');";
   script += "ctrl.focus();";
   ClientScript.RegisterClientScriptBlock(this.GetType(), 
       "focusScript", script, true);

}

这会使你的页面在加载后执行以下脚本,并且控件应该重新关注焦点:

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

答案 1 :(得分:0)

在.aspx文件的git指令中添加import plotly.figure_factory as ff import plotly.graph_objects as go import numpy as np import plotly.io as pio pio.renderers.default = "browser" ## Create first figure import plotly.io as pio pio.renderers.default = "browser" x1,y1 = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2)) u1 = np.cos(x1)*y1 v1 = np.sin(x1)*y1 fig1 = ff.create_quiver(x1, y1, u1, v1, name='Quiver') fig1.show() ## Create second figure import plotly.io as pio pio.renderers.default = "browser" x = np.linspace(-3, 3, 100) y = np.linspace(-3, 3, 100) Y, X = np.meshgrid(x, y) u = -1 - X**2 + Y v = 1 + X - Y**2 fig2 = ff.create_streamline(x, y, u, v, arrow_scale=.1, name='Steamline') fig2.show() 或将其添加到MaintainScrollPositionOnPostBack="true"的{​​{1}}

@Page

当导航到带有大表的上一页时,此方法也适用。

  

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

Documentation