gridview中的模板字段问题
问:
我遇到以下问题,但我不知道如何解决它。
我有一个网格视图,其中一列是一个模板字段(文本框)。网格视图由 8
行组成。我所做的是每次用户在文本框中输入数据时,我将总计放入最后一个文本框中(设置启用= false)。我通过某种方法对文本框中的数据输入求和并在事件中调用它文本已更改
。但是每次我在文本框中输入数字,然后单击键盘中的 Tab 或使用鼠标光标移动到下一个框时,我都会失去焦点,并且必须再次放置鼠标光标在预期的文本框中。
我尝试以下方法来解决我的问题但徒劳。
foreach (GridViewRow r in gv_Evaluation.Rows)
{
((RadTextBox)r.Cells[3].FindControl("txt_evaluateWeights")).Attributes.Add("blur", "calc()");
}
在我的页面加载中,这根本不起作用。
protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
calc();
((TextBox)sender).Focus();
}
这样,焦点将返回到上一个文本框(我的意思是我已经完成的文本框),而不是我想要焦点所在的文本框,以输入数据。
编辑:
我的计算方法:
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());
}
Q:
I have the following problem , and i don't know how to fix it really.
I have a grid view
one of the columns is a template field
as (text box). The grid view consists of 8
rows . What i do is every time the user enter data in the text box,I put the total in the last text box(which set enabled = false).I sum the data entry in the text boxes through some method and call it in the event text changed
. But every time i enter a number in the text box and then click Tab in the keyboard
or use the mouse cursor to move to the next box i lose the focus , and i have to put the mouse cursor again in the intended textbox.
I try the following methods to fix my problem but in vain .
foreach (GridViewRow r in gv_Evaluation.Rows)
{
((RadTextBox)r.Cells[3].FindControl("txt_evaluateWeights")).Attributes.Add("blur", "calc()");
}
in my page load , this doesn't work at all.
protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
calc();
((TextBox)sender).Focus();
}
This way return the focus to the previous textbox (i mean the one which i already have done) not the text box i wanna the focus in, to enter the data.
EDIT:
My calc method:
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用服务器端回发来执行此操作是一种可怕的方法。
请改用 JavaScript。这是 jQuery 中的一个小示例
GridView
背后的代码
JavaScript
(jQuery)希望这会有所帮助
Doing it using server side PostBack is a horrendous way of doing this.
Use JavaScript instead. Here is a small example in jQuery
The GridView
The Code Behind
The JavaScript (jQuery)
Hope this helps