丢失gridview中文本框的焦点

时间:2014-11-12 10:39:44

标签: javascript c# jquery asp.net ajax

我在updatepanel中有一个网格视图,如下所示:

<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
        }

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

3 个答案:

答案 0 :(得分:5)

我建议的一个解决方案是使用javascript DOM Storage。保持焦点文字id的{​​{1}}与input

local storage

$(':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(){});

Here also an live example。在重新加载页面后重点放在文本中。

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

更具体的信息here。还full screen live example

答案 1 :(得分:0)

您可以使用JavaScript轻松关注某个元素:

window.onload = function() {
    document.getElementById("txt_1").focus();
};

或者,如果您更喜欢jQuery:

$(function() {
    $("#txt_1").focus();
});

此代码将在页面完全加载后专注于ID为“txt_1”的元素。

答案 2 :(得分:0)

这听起来像你失去焦点的文本框正在触发帖子回来?如果是这样,看看这个,它不是一个完整的解决方案,但它是一个开始。这或多或少是我的头脑,对不起,如果它不是100%正确。 http://msdn.microsoft.com/en-us/library/vstudio/bb383810(v=vs.100).aspx

Sys.WebForms.PageRequestManager可以获得文本框ID,或者回发中的项ID。从那里你可以使用document.getElementById("itemID").focus();

var requestMgr = Sys.WebForms.PageRequestManager.getInstance();
requestMgr.add_endRequest(endRequestHandler);

function endRequestHandler() {
  var itemID = requestMgr.args.get_postBackElement().id
  document.getElementById("itemID").focus()
}
相关问题