单击“保存”按钮时如何验证 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;
}
对我做错了什么有什么想法吗?
谢谢!
I have a gridview in a form that contains a Save ImageButton. I would like to create a Client-side CustomValidator that checks whether the grid is empty or not. If it is empty then I would like to throw an error message to the user.
This is my code. In the "Save_btn_Click" event, I check if the page is Valid:
<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;
}
Any ideas on what I'm doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用下面的代码行获取 gridview 的行数:
参考:
使用 Javascript 的 ASP.NET GridView 行计数
如何计算 gridview 中的行数使用 jQuery 的 asp.net
Use below line of code to get row count of your gridview:
references:
ASP.NET GridView row count using Javascript
How to count the rows in a gridview in asp.net using jQuery