我在包含Save ImageButton的表单中有一个gridview。我想创建一个客户端CustomValidator来检查网格是否为空。如果它是空的,那么我想向用户抛出一条错误消息。
这是我的代码。在“Save_btn_Click”事件中,我检查页面是否有效:
<asp:GridView ID="MyGridView" runat="server"
AutoGenerateColumns="False"
OnRowCancelingEdit="gridView_RowCancelingEdit"
OnRowCommand="gridView_RowCommand"
OnRowDataBound="gridView_RowDataBound"
OnRowEditing="gridView_RowEditing"
OnRowUpdating="gridView_RowUpdating"
>....</GridView>
<asp:CustomValidator id="cvFabricCollection" runat="server"
ErrorMessage="Please enter at least one row"
ControlToValidate="gridView"
ValidationGroup="MyGroup"
ClientValidationFunction ="ValidateGrid">
</asp:CustomValidator>
<asp:ImageButton ID="Save_btn"
ImageUrl="images/save.gif"
runat="server"
CausesValidation="True"
ValidationGroup="MyGroup"
OnClick="Save_btn_Click"/>
使用Javascript:
function ValidateGrid(sender, args)
{
var rowscount = document.getElementByID(<%=MyGridView.ClientID%>).rows.length;
alert(rowscount);
if(rowscount <= 1)
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
关于我做错的任何想法?
谢谢!
答案 0 :(得分:0)
使用以下代码行获取gridview的行数:
var rowscount = document.getElementByID(<%=Gridview1.ClientID%>).rows.length;
if(rowcount >0)
{
alert("your message");
}
答案 1 :(得分:0)
function PassengerGrid(source, args) {
var Grid1 = document.getElementById("<%=GridviewPassenger.ClientID%>");
if (Grid1 == null) {
args.IsValid = false;
}
else if (Grid1.rows.length <= 0)
{
args.IsValid = false;
}
else {
args.IsValid = true;
}
}