gridview中的模板字段问题

时间:2011-07-21 08:33:09

标签: .net asp.net ajax gridview templatefield

问:

我有以下问题,我不知道如何解决它。

我有grid view其中一列是template field为(文本框)。网格视图由8行组成。我所做的是每次用户在文本框中输入数据时,我将总数放在最后一个文本框中(设置enabled = false)。我通过某种方法对文本框中的数据条目求和并在事件中调用它text changed。但每次我在文本框中输入一个数字,然后单击Tab in the keyboard或使用鼠标光标移动到下一个框我失去焦点,我必须将鼠标光标再次放在预期的文本框中。 / p>

我尝试以下方法来解决我的问题,但徒劳无功。

 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());

        }

1 个答案:

答案 0 :(得分:1)

使用服务器端PostBack执行此操作是一种可怕的方式。

使用JavaScript代替。这是jQuery中的一个小例子

GridView

<asp:GridView ID="DemoGrid" runat="server"
            AutoGenerateColumns="false"
            ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="index">
            <ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item">
            <ItemTemplate>
                <asp:Label ID="DemoLabel" runat="server" Text='<%# Container.DataItem %>' />
            </ItemTemplate>
            <FooterTemplate>Total</FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:TextBox ID="DemoText" runat="server" CssClass="quantity">
                </asp:TextBox>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="TotalLabel" runat="server" CssClass="result"/>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

背后的守则

protected void Page_Load(object sender, EventArgs e){
    if (!IsPostBack)
    {
        string[] array = new string[] { "demo1", "demo2", "demo3", "demo4", "demo5" };
        DemoGrid.DataSource = array;
        DemoGrid.DataBind();
    }
}

JavaScript(jQuery)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".quantity").bind("blur", function () {
            var $quantity = $(this);
            var quantity = +$quantity.val(); //cast to number
            if (!isNaN(quantity)) {
                var $sum = $quantity.closest("table").find("tr:last .result");
                var sum = +$sum.html();
                $sum.html(sum + quantity);
            }
        });
    });
</script>

希望这有帮助