gridview中的模板字段问题

发布于 2024-11-25 12:29:00 字数 1562 浏览 0 评论 0原文

问:

我遇到以下问题,但我不知道如何解决它。

我有一个网格视图,其中一列是一个模板字段(文本框)。网格视图由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-12-02 12:29:00

使用服务器端回发来执行此操作是一种可怕的方法。

请改用 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

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

(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

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

The Code Behind

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

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

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文