javascript - 失去 gridview 中文本框的焦点

标签 javascript c# jquery asp.net ajax

我在更新面板中有一个 GridView ,如下所示:

<asp:UpdatePanel ID="UpdatePanel3" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pnl_lect" runat="server">
                <asp:GridView ID="gv_ques" runat="server" CssClass="formTable cr_center" AutoGenerateColumns="False"
                    ShowFooter="True" OnRowDataBound="gv_ques_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="عناصر التقييم">
                            <ItemTemplate>
                                <asp:Label ID="lbl_ques" runat="server" Text='<%# Bind("que_desc") %>'></asp:Label>

                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="Label1" runat="server" Text="الاجمالي"></asp:Label>
                            </FooterTemplate>
                            <ItemStyle Width="45%" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ممتاز (4)">
                            <ItemTemplate>
                                <telerik:RadNumericTextBox ID="txt_1" runat="server" DbValue='<%# Bind("grade_id1") %>'
                                    AutoPostBack="true" Width="60px" MinValue="0" MaxValue="999999999" OnTextChanged="txt_1_TextChanged">
                                    <NumberFormat GroupSeparator="" DecimalDigits="0" />
                                </telerik:RadNumericTextBox>
                                <asp:HiddenField ID="hf_1" runat="server" Value="1" />
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_1" runat="server"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="جيد جدًا (3)">
                            <ItemTemplate>
                                <telerik:RadNumericTextBox ID="txt_2" runat="server" DbValue='<%# Bind("grade_id2") %>'
                                    AutoPostBack="true" Width="60px" MinValue="0" MaxValue="999999999" OnTextChanged="txt_1_TextChanged">
                                    <NumberFormat GroupSeparator="" DecimalDigits="0" />
                                </telerik:RadNumericTextBox>
                                <asp:HiddenField ID="hf_2" runat="server" Value="2" />
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_2" runat="server"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="جيد (2)">
                            <ItemTemplate>
                                <telerik:RadNumericTextBox ID="txt_3" runat="server" DbValue='<%# Bind("grade_id3") %>'
                                    AutoPostBack="true" Width="60px" MinValue="0" MaxValue="999999999" OnTextChanged="txt_1_TextChanged">
                                    <NumberFormat GroupSeparator="" DecimalDigits="0" />
                                </telerik:RadNumericTextBox>
                                <asp:HiddenField ID="hf_3" runat="server" Value="3" />
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_3" runat="server"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="مقبول (1)">
                            <ItemTemplate>
                                <telerik:RadNumericTextBox ID="txt_4" runat="server" DbValue='<%# Bind("grade_id4") %>'
                                    AutoPostBack="true" Width="60px" MinValue="0" MaxValue="999999999" OnTextChanged="txt_1_TextChanged">
                                    <NumberFormat GroupSeparator="" DecimalDigits="0" />
                                </telerik:RadNumericTextBox>
                                <asp:HiddenField ID="hf_4" runat="server" Value="4" />
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_4" runat="server"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ضعيف (0)">
                            <ItemTemplate>
                                <telerik:RadNumericTextBox ID="txt_5" runat="server" DbValue='<%# Bind("grade_id5") %>'
                                    AutoPostBack="true" Width="60px" MinValue="0" MaxValue="999999999" OnTextChanged="txt_1_TextChanged">
                                    <NumberFormat GroupSeparator="" DecimalDigits="0" />
                                </telerik:RadNumericTextBox>
                                <asp:HiddenField ID="hf_5" runat="server" Value="5" />
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label ID="lbl_5" runat="server"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

每次回发我都会失去焦点,所以我写了以下内容:

protected void txt_1_TextChanged(object sender, EventArgs e)
        {
            int progSer = int.Parse(Session["prog_serial"].ToString());
            int total = 0;
            RadNumericTextBox txt = (RadNumericTextBox)sender;

            GridViewRow r = (GridViewRow)txt.NamingContainer;

            TableCell cell = null;

            Control parent = txt;
            while ((parent = parent.Parent) != null && cell == null)
                cell = parent as TableCell;
            int indexOfTextBoxCell = -1;
            if (cell != null)
                indexOfTextBoxCell = r.Cells.GetCellIndex(cell);


            foreach (GridViewRow row in gv_ques.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    total = total + int.Parse(((RadNumericTextBox)row.Cells[indexOfTextBoxCell].Controls[1]).Value.ToString());
                }
            }
            ((Label)gv_ques.FooterRow.Cells[indexOfTextBoxCell].Controls[1]).Text = total.ToString();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + txt.ClientID + "').focus();$get('" + txt.ClientID + "').select();", true);//the focus
        }

现在我跳出文本框,焦点仍然没有完全正确设置。我必须在回发后单击一次才能使其正常工作。如何将焦点设置为选项卡式而不是当前文本框?

最佳答案

我建议的一个解决方案是使用 javascript DOM Storage 。使用本地存储保留焦点文本inputid:

$(':text').on("focus", function(){
  localStorage.setItem("focusItem", this.id);//here set in localStorage id of the textbox
  //console.log(localStorage.getItem("focusItem"));test the focus element id
});

$(document).ready(function(){}); 事件上,您可以将焦点设置在输入文本框上:

$('#' + localStorage.getItem("focusItem")).focus();

Here also an live example 。重新加载页面后,将焦点集中在文本上。

请记住,要删除 localStorage 中的内容,您必须通过 localStorage.removeItem(itemName) 显式清除它们。

更多具体信息here 。另外full screen live example .

关于javascript - 失去 gridview 中文本框的焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26884931/

相关文章:

javascript - 如何使用 introjs 突出显示多个元素?

javascript - 如何更改下拉子菜单二级背景?

c# - XXE:使用 XDocument 对 XML 外部实体引用的不当限制

c# - 将 List<T> 设置为 Null 的最佳实践

jquery - 在 jquery Accordion 菜单中设置打开

javascript - 如何重新定位下拉菜单?

javascript - div之间的空间很小

c# - 如何将两个 Entity Framework 调用合并为一个?

javascript - 为标签内容添加漂亮的滑动效果

jquery - 数据表为所有正文行添加类