2

我已在此 GridView 中添加了 TextBox 的验证,但此验证仅适用于 GridView 的当前页面(不适用于 GridView 的上一页和下一页)。

<asp:GridView ID="Grd1" runat ="server" Width ="100%" AllowPaging ="true" pagesize="5">
  <Columns>
    <asp:BoundField HeaderText="Name" DataField="NM" ItemStyle-Width="300px" HeaderStyle-Width="300px" HeaderStyle-Wrap="false" ItemStyle-Wrap="false" />
    <asp:TemplateField HeaderText="Size (GB)">
      <ItemTemplate>
        <asp:TextBox ID="txtSize" runat="server" Width="100px"></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

<asp:Button ID="btnAdd" runat="server" Text="Save" OnClick="btnAdd_Click" />

<script language="javascript" type="text/javascript">
    function IsValidateAdd() {
        if (validateGridTextBox() == false)
            { return false; }
    }

    function validateGridTextBox() {
        var flag = false;
        var dropdowns = new Array(); //Create array to hold all the dropdown lists.
        var gridview = document.getElementById('<%=Grd1.ClientID %>'); //grvDMODetails is the id of ur gridview.

        dropdowns = gridview.getElementsByTagName('input'); //Get all dropdown lists contained in Grd1.            
        for (var i = 0; i < dropdowns.length; i++) {
            if (dropdowns.item(i).value != "") //If dropdown has no selected value
            {
                flag = true;
            }
            else 
            {
                flag = false;
                break;
            }
        }

        if (flag == false) 
        {
            alert('Please enter Table Size.');
            return flag;
        }

</script>

后面的代码:

btnAddDM.Attributes.Add("onclick", "return IsValidateAdd();");
4

1 回答 1

0

when you navigation thru prev page or next page,the grid view part has changed,but the javascript function was binded to the original grid view.this is why it does not work.

If you want it work,there are 2 solutions

1,when you navigation to other pages,bind the javascript validate function again manually.

2,add onclientclick attribute to the textbox control,such as

<asp:TextBox ID="txtSize" runat="server" Width="100px" onClientClick="validateGridTextBox(this)"></asp:TextBox>

and change your validateGridTextBox javascript function accordingly.

于 2013-09-29T09:35:50.893 回答