【问题标题】:enable or disable button based on comparing textboxes values of gridview in c#基于比较c#中gridview的文本框值启用或禁用按钮
【发布时间】:2015-03-03 13:46:06
【问题描述】:

您好,我是 javascript 和 c# 的新手。 如果我错了,请纠正我。

这是我的gridview 代码。

     <asp:GridView ID="GridView1" CssClass="table table-hover table-bordered" runat="server" AutoGenerateColumns="False" ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
          <asp:BoundField DataField="AvlQty" HeaderText="Available" ItemStyle-Width="35" >
<ItemStyle Width="35px"></ItemStyle>
            </asp:BoundField>


<asp:TemplateField HeaderText="Qty" ItemStyle-Width="70">
                <ItemTemplate>                
                    <asp:TextBox ID="TextBoxQty" onkeyup="Calculation(this)" CssClass="txtQty" runat="server" Text='<%# Eval("SelQty") %>' MaxLength="5" Width="45"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxQty" Display="Dynamic" ForeColor="Red" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
                 <asp:regularexpressionvalidator ID="revAvailablePeriod" runat="server" ErrorMessage="Must > 0" ForeColor="Red" controltovalidate="TextBoxQty" validationexpression="^[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$" setfocusonerror="true" validationgroup="AddAssests" xmlns:asp="#unknown"></asp:regularexpressionvalidator>
              <asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare='<%# Eval("AvlQty") %>' ControlToValidate="TextBoxQty" ForeColor="Red"
ErrorMessage="Must < Available" Operator="LessThan" Type="Integer"></asp:CompareValidator>

                </ItemTemplate>

<ItemStyle Width="70px"></ItemStyle>
            </asp:TemplateField>
</Columns> 
</asp:GridView>

<asp:Button ID="Button2" runat="server" Enabled="false" Text="Place Order >>" CssClass="btn btn-info" OnClick="Button2_Click" />

现在,如果 gridview 的每一行的数量大于可用数量,则应禁用按钮,否则应启用按钮。

我想使用 javascript 函数来实现它。 任何人都可以帮助我吗?

我写了这个函数但是不知道什么时候调用以及如何调用.. 这就是我到目前为止所做的..

 function Calculation() 
 {
   var grid = document.getElementById("<%= GridView1.ClientID%>");
         var counter = 0;
         for (var i = 1; i < grid.rows.length ; i++) {
             var txtAvl = grid.rows[i].cells[3];

             var qty = grid.rows[i].cells[5];
             if (txtAvl.value >= qty.value && qty.value > 0)
             { counter++; }
             else
             { document.getElementById('Button2').disabled = true; }

         }
         if (counter == grid.rows.length)
             document.getElementById('Button2').disabled = false;

 } 

【问题讨论】:

  • 将您的代码作为更新发布在问题中。
  • 我按照你的要求添加了javascript函数代码@DylanCorriveau..

标签: javascript c# asp.net gridview


【解决方案1】:

onkeyup 函数足以调用此javascript 验证。我稍微更改了您的脚本及其工作方式。

<script type="text/javascript">
        function Calculation() {
            var grid = document.getElementById('GridView1');
            var counter = 0;

            for (var i = 1; i < grid.rows.length; i++) {

                var txtQty = document.getElementById('GridView1_ctl0' + (i + 1) + '_TextBoxQty'); //TextBox control

                if (txtQty != undefined) {

                    var qty = txtQty.value;
                    var txtAvl = grid.rows[i].cells[0].innerHTML;

                    if (txtAvl >= qty && qty > 0) {
                        counter++;
                    }
                    else {
                        document.getElementById('Button2').disabled = true;
                        break;
                    }
                }
            }
            if (counter == grid.rows.length - 2)
                document.getElementById('Button2').disabled = false;
        } 
    </script>

您必须相应地使用 GridviewTextBox 的正确 ID 进行更新。希望这会有所帮助。

【讨论】:

  • ..我试过了,你说的但脚本中的函数不起作用,我在文本框中调用了这样的函数
  • @RajParekh 您是否更改了 html 中的 GridView ID 和 TextBox ID?
  • ya @Selva TS,我把它改成了 GridView1 和 TextBoxQty。
  • 列顺序呢?您是否尝试过在 Chrome/Firebox 控制台中进行调试?
  • 是的,我也更改了列顺序。我没有得到一件事,你添加的文本框控件中的 (i+1) 做了什么。
猜你喜欢
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
相关资源
最近更新 更多