我已在此 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();");