C# 如何在文本框';是否在GridView ASP.NET中更改了s值?

C# 如何在文本框';是否在GridView ASP.NET中更改了s值?,c#,asp.net,gridview,C#,Asp.net,Gridview,每当我更改文本框的值时,如何更改标签的值,作为文本框和gridview中的边界字段相乘的结果?(不使用Textbox\u TextChanged,因为它要求我在开火前先单击其他位置) lblTotal=txtSacks*价格 提前感谢您提供的任何帮助当文本框失去焦点时,您当前的事件处理程序将进行回发。您可能需要一些客户端javascript来完成它,而无需在每次按键时发回消息。将jQuery放入页面并尝试以下代码: <Columns> <asp:TemplateFi

每当我更改文本框的值时,如何更改
标签的值,作为
文本框
gridview
中的
边界字段
相乘的结果?(不使用Textbox\u TextChanged,因为它要求我在开火前先单击其他位置)


lblTotal=txtSacks*价格


提前感谢您提供的任何帮助

当文本框失去焦点时,您当前的事件处理程序将进行回发。您可能需要一些客户端javascript来完成它,而无需在每次按键时发回消息。将jQuery放入页面并尝试以下代码:

<Columns>
    <asp:TemplateField HeaderText="Sacks">
        <ItemTemplate>
            <asp:TextBox ID="txtSacks" runat="server"
                CssClass="form-control" Text="0" Width="100px" ToolTip="<%# Container.DataItemIndex + 1 %>">
            </asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Price">
        <ItemTemplate>
            <asp:Label runat="server" ID="lblPrice" CssClass="lbl-price" ToolTip="<%# Container.DataItemIndex + 1  %>" Text='<%# Bind("Price") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="price" HeaderText="Price" />
    <asp:TemplateField HeaderText="Subtotal">
        <ItemTemplate>
            <asp:Label ID="lblTotal" runat="server" CssClass="lbl-total" ToolTip='<%# Container.DataItemIndex + 1 %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>        
这不是实现这一点的最优雅的方式,但它确实有效。很长一段时间没有使用web窗体和GridView了

注意:代替
,您可以使用数据行中的某些属性来字符串控件,如
priceId


您不应该为在客户端可以执行的操作加载服务器。使用Javascript来实现它。@AnupSharma我也在考虑使用Javascript,但我无法识别文本框、边界字段值和标签的事实阻止了向它们提供id。服务器将使用唯一的客户端id呈现它们。在jquery/javascript中使用该客户端id。您还可以自己为它们设置唯一id,并将Clientidmode设置为static。好的,您可以使用文本框的
keyup
keypress
事件,同时使用
后跟指向
span
getElementsByTagName
捕获gridview的
ClientID
。查看本文中的示例:。您的答案帮助解决了我的问题。不过,如果你同意的话,你能解释一下脚本的工作原理吗?我想用这个来创建一个类似情况的解决方案。我从来没有真正深入研究过javascript,因此我对它的了解有限,我添加了一些评论,希望能有所帮助
<Columns>
    <asp:TemplateField HeaderText="Sacks">
        <ItemTemplate>
            <asp:TextBox ID="txtSacks" runat="server"
                CssClass="form-control" Text="0" Width="100px" ToolTip="<%# Container.DataItemIndex + 1 %>">
            </asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Price">
        <ItemTemplate>
            <asp:Label runat="server" ID="lblPrice" CssClass="lbl-price" ToolTip="<%# Container.DataItemIndex + 1  %>" Text='<%# Bind("Price") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="price" HeaderText="Price" />
    <asp:TemplateField HeaderText="Subtotal">
        <ItemTemplate>
            <asp:Label ID="lblTotal" runat="server" CssClass="lbl-total" ToolTip='<%# Container.DataItemIndex + 1 %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>        
<script>
  $("input[title]").on("keyup", function () {
       /*We had emitted ToolTip attribute on our controls in Grid. 
        Those are translated to `title` in the browser. Here we are 
        picking controls based on the values that would be set by 
        asp.net while rendering grid.*/
        var $t = $(this), 
         //pick value of title or tooptip attribute of $t control
         i = $t.attr("title") || $t.attr("tooltip");

        //we get value of $t and initialize a number from it as it is a string
        var s = Number($t.val());

        //if we are not able get a number from textbox value, return
        if (isNaN(s)) return;

        //we are looking for a span which has title attribute set to value
        //we picked from textbox above also having class lbl-price 
        //and getting its text
        var p = $("span[title='" + i + "'].lbl-price").text();

        //then we get next span with same title value but class lbl-total
        // and set its text to calculated value.
        $("span[title='" + i + "'].lbl-total").text(s * +p);
  });
</script>