我正在使用带分页的gridview来显示数据库表。当用户插入UserID时,我想在客户端检查自定义验证功能中的重复项。我必须循环遍历网格中的所有值,而不仅仅是显示的值。我无法获取UserID列中的所有值以检查是否存在插入的UserID。 这是标记:
<asp:TemplateField HeaderText="UserID">
<HeaderTemplate> UserID
<asp:ImageButton ID="senigvUserIDFilter" runat="server" ImageUrl="Images/filter.png" OnClientClick="return ShowHideFilterTxtBox('senigvTxtUserIDFilter')" />
<asp:TextBox ID="senigvTxtUserIDFilter" runat="server" AutoPostBack="true" style="display:none;" ClientIDMode="Static" OnTextChanged="senigvGridFilter_TextChanged">
</asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="senigvLblUserID" runat="server" Text='<%# Bind("UserID") %>' ClientIDMode="Static"
></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="senigvLblEditUserID" runat="server" Text='<%# Bind("UserID") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="senigvTxtBxInsertUserID" runat="server" Text='<%# Bind("UserID") %>' ClientIDMode="Predictable"></asp:TextBox>
<asp:RequiredFieldValidator ID="senigvRequiredFieldInsertUserID" ControlToValidate="senigvTxtBxInsertUserID" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSenderValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="senigvMaxValInsertUserID" ControlToValidate="senigvTxtBxInsertUserID" runat="server"
ErrorMessage="Maximumn length is 40." ValidationGroup="InsertSenderValidation" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,40}$" >
</asp:RegularExpressionValidator>
<asp:CustomValidator ID="senigCustomDupValInsertUserID" runat="server" ControlToValidate="senigvTxtBxInsertUserID" CssClass="message-error"
ErrorMessage="*" ClientValidationFunction="ValidateUserID" EnableClientScript="true"
ValidationGroup="InsertSenderValidation">
</asp:CustomValidator>
</FooterTemplate>
</asp:TemplateField>
这是jquery函数,'ValidateUserID'
function ValidateUserID(sender, args) {
args.IsValid = true;
sender.innerHTML = '';
var table = document.getElementById("#UserInfoGridVew");
var tbody = table.tBodies[0];
//loop thru all UserIDs
for (i = 0, rowLen = tbody.rows.length; i < rowLen; i++) {
row = tbody.rows[i];
var currentUserID = row.cells[1].innerHTML;
alert('currentUserID: ' + currentUserID);
if (args.Value == currentUserID) {
//if values are equal; input invalid; set error message
args.IsValid = false;
sender.innerHTML = "UserID exists.";
break;
}
}
};
当我使用此功能时,我在Object required
行
var tbody = table.tBodies[0];
我需要访问gridview中的所有数据行,然后检查UserID列是否有重复项。我怎么能这样做?
答案 0 :(得分:0)
请试试下面的代码
function ValidateUserID(sender, args) {
args.IsValid = true;
sender.innerHTML = '';
var gvDrv = document.getElementById("#UserInfoGridVew");
for (i = 1; i < gvDrv.rows.length; i++) {
var cell = gvDrv.rows[i].cells;
var currentUserID = cell[1].innerHTML;
alert('currentUserID: ' + currentUserID);
if (args.Value == currentUserID) {
//if values are equal; input invalid; set error message
args.IsValid = false;
sender.innerHTML = "UserID exists.";
break;
}
}
}
打开页面的视图源并确保网格ID为&#34; UserInfoGridVew&#34;。
希望这会对你有所帮助。
答案 1 :(得分:0)
获取所有数据以确定是否重复的唯一方法是将JSON调用回数据库并比较值。
网格使用分页,以便获取我必须回调数据库的所有值。