问题
I have a gridview which contains a text box as a template field.the grid view is in an updatepanel.
I use the text changed event to to calculate the percentage of the first four textboxes and put the result in the fifth text box , My problem is: i always lose the focus as soon as the text changed ,and every time i was supposed to move the mouse cursor again to the target text box.How to solve this problem?I wanna to keep the focus on my text box after text changed.
My Code:
private void calc()
{
float sum = 0;
for (int i = 0; i < 7; i++)
{
RadTextBox txt1 = (RadTextBox)gv_Evaluation.Rows[i].Cells[3].FindControl("txt_evaluateWeights");
int weight;
bool result = Int32.TryParse(txt1.Text, out weight);
if (result)
{
sum += weight;
}
}
double percentage;
percentage = Math.Round((sum / 100) * 100, 2);
RadTextBox txt3 = (RadTextBox)gv_Evaluation.Rows[7].Cells[3].FindControl("txt_evaluateWeights");
txt3.Text = percentage.ToString();//string.Format("{0:0.0%}", percentage.ToString());
}
protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
calc();
}
My aspx:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="pnl_research" runat="server" CssClass="pnl">
<div id="detailsDiv" align="center" style="width: 800px;">
<table border="0" width="98%">
<tr>
<td align="center">
<asp:Panel ID="panel_rmv" runat="server" Visible="true" Direction="RightToLeft">
<div class="grid" dir="rtl">
<div class="grid" dir="rtl">
<div class="rounded">
<div class="top-outer">
<div class="top-inner">
<div class="top">
<h2>
<asp:Label ID="Label35" runat="server" Text="##"></asp:Label></h2>
</div>
</div>
</div>
<div class="mid-outer">
<div class="mid-inner">
<div class="mid">
<asp:GridView Width="100%" ID="gv_Evaluation" CssClass="datatable" AllowSorting="True"
runat="server" TabIndex="2" AutoGenerateColumns="False" AllowPaging="True" GridLines="None"
OnRowDataBound="gv_Evaluation_RowDataBound">
<EmptyDataTemplate>
<table style="width: 100%;">
<tr>
<td>
</tr>
<tr>
<td align="center">
<asp:Label ID="Label4" runat="server" Font-Size="16pt" Text="لا يوجد بيانات"></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="م">
<ItemTemplate>
<asp:Label ID="lblSerial" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="" DataField="activityType" />
<asp:BoundField HeaderText="" DataField="activityWeight" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<telerik:RadTextBox ID="txt_evaluateWeights" runat="server" AutoPostBack="True" OnTextChanged="txt_evaluateWeights_TextChanged">
</telerik:RadTextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="" DataField="activitySelf" />
<asp:BoundField HeaderText="" DataField="activityBoss" />
<asp:BoundField HeaderText="" DataField="activityDean" />
</Columns>
<RowStyle VerticalAlign="Top" CssClass="row" />
</asp:GridView>
</div>
</div>
</div>
<div class="bottom-outer">
<div class="bottom-inner">
<div class="bottom">
</div>
</div>
</div>
</div>
</div>
</asp:Panel>
</td>
</tr>
</table>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
回答1:
Is the textbox in an UpdatePanel? Is the entire page getting posted?
You could set the focus in the code-behind...
protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
calc();
((TextBox)sender).Focus();
}
回答2:
You can use jquery to do it
$('#txt_evaluateWeights').focus();
or normal javascript
document.getElementById("Box1").focus();
回答3:
Thanks a lot , I fix my problem:
Firstly:
because no command argument property for the textbox to store the gridview index , i store it in the tab index .
TabIndex='<%#((GridViewRow)Container).RowIndex%>'
protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
calc();
int index = ((RadTextBox)sender).TabIndex;
((RadTextBox)gv_Evaluation.Rows[index + 1].Cells[3].FindControl("txt_evaluateWeights")).Focus();
}
回答4:
protected void TxtPaidAmtTextChanged(object sender, EventArgs e)
{
int index = ((TextBox)sender).TabIndex;
TextBox txtindex = (TextBox)gridCurrentFeeHead.Rows[index + 1].FindControl("TxtPaidAmt");
txtindex.Focus();
}
来源:https://stackoverflow.com/questions/6748625/how-to-keep-focus-on-the-text-box-after-text-changed-event