我的aspx页面上有一个gridview:
<asp:GridView ID="gvPhoneBook" runat="server" AutoGenerateColumns="false"
ShowFooter="true" DataKeyNames="PhoneBookID"
ShowHeaderWhenEmpty="true"
OnRowCommand="gvPhoneBook_RowCommand"
OnRowEditing="gvPhoneBook_RowEditing" OnRowCancelingEdit="gvPhoneBook_RowCancelingEdit"
OnRowUpdating="gvPhoneBook_RowUpdating" OnRowDeleting="gvPhoneBook_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" Text='<%# Eval("FirstName") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstNameFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("LastName") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" Text='<%# Eval("LastName") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastNameFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact">
<ItemTemplate>
<asp:Label Text='<%# Eval("Contact") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContact" Text='<%# Eval("Contact") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtContactFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label Text='<%# Eval("Email") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" Text='<%# Eval("Email") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmailFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" OnClientClick="return Validate(this);" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我想在更新时验证电子邮件字段(它不应为空)。
以下是我的尝试: -
function validate()
{
if(document.getElementById("<%=txtEmail.ClientID%>").value=="")
{
alert("Email Field can not be blank");
document.getElementById("<%=txtEmail.ClientID%>").focus();
return false;
}
return true;
}
但由于txtEmail在当前上下文中不存在,我收到错误。
我也使用过这种方法: -
<script type="text/javascript">
function Validate(lnkUpdate) {
var txtEmail;
var row = lnkUpdate.parentNode.parentNode;
txtEmail = row.getElementsByID("txtEmail");
if (txtEmail.value == null) {
alert("Email Field can not be blank");
}
}
我在更新按钮上调用此方法 但这也行不通。我怎么用javascript做到这一点?我在哪里做错了?
答案 0 :(得分:2)
您可以在按钮
中添加课程<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" class="button_save" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
然后在javascript中听取该课程的点击事件
var buttonSave = document.getElementsByClassName("button_save");
for (var i = 0; i < buttonSave.length; i++) {
buttonSave[i].addEventListener('click', Validate(buttonSave[i]), false);
}
答案 1 :(得分:0)
我认为你应该研究一下aspnet验证控件。
<asp:TextBox ID="txtEmail" Text='<%# Eval("itemid") %>' runat="server" />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Email Field can not be blank" ControlToValidate="txtEmail"
ValidationGroup="inGridView"></asp:RequiredFieldValidator>
您还必须将ValidationGroup
添加到保存按钮
<asp:ImageButton runat="server" ValidationGroup="inGridView" />
有几种不同的类型,可以从后面的代码中进行操作。请参阅文档:https://msdn.microsoft.com/nl-nl/library/bwd43d0x.aspx