我在更新面板中有一个 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 。使用本地存储
保留焦点文本input
的id
:
$(':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/