gpt4 book ai didi

javascript - 更新时如何使用javascript验证asp.net gridview文本框?

转载 作者:行者123 更新时间:2023-11-30 14:49:54 27 4
gpt4 key购买 nike

我的 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 做到这一点?我哪里做错了?

最佳答案

你可以给你的按钮添加一个类

<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中监听类的事件click

var buttonSave = document.getElementsByClassName("button_save");

for (var i = 0; i < buttonSave.length; i++) {
buttonSave[i].addEventListener('click', Validate(buttonSave[i]), false);
}

关于javascript - 更新时如何使用javascript验证asp.net gridview文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48307066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com