I have a gridview on my aspx page:
<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>
I want to validate Email field when updating (It should not be empty).
Here is what I have tried:-
function validate()
{
if(document.getElementById("<%=txtEmail.ClientID%>").value=="")
{
alert("Email Field can not be blank");
document.getElementById("<%=txtEmail.ClientID%>").focus();
return false;
}
return true;
}
But I get error as txtEmail does not exist in the current context.
I also used this method:-
<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");
}
}
I am calling this method on update button but this also not working. How do I do that using javascript? where am i doing wrong?
You can add a class to your button
<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>
And then listen to the event click of the class in javascript
var buttonSave = document.getElementsByClassName("button_save");
for (var i = 0; i < buttonSave.length; i++) {
buttonSave[i].addEventListener('click', Validate(buttonSave[i]), false);
}
I think you should look into aspnet Validation Controls.
<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>
You have to add the ValidationGroup
to the save button also
<asp:ImageButton runat="server" ValidationGroup="inGridView" />
There are several different types and can be manipulated from code behind. See the documentation: https://msdn.microsoft.com/nl-nl/library/bwd43d0x.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.