我创建了一个简单的程序,它将获取数据并在vb.net的默认验证器中验证它的服务器端。
第一次它的工作在某种程度上。但是在第二次做同样的事情时,txtDOB
的模糊事件没有被调用。
我的代码如下:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test_NET_27.aspx.vb" Inherits="Test_NET_27" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>.NET Test</title>
<script src="Script/Jquery.js" type="text/javascript"></script>
<script src="Script/jquery.min.js" type="text/javascript"></script>
<script src="Script/jquery-1.9.1.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="background-color: Silver; height: 600px; margin-left: 10%; margin-right: 10%;
margin-top: 2%">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<fieldset style="margin-left: 10%; margin-right: 10%">
<legend style="text-align: Left; font-size: medium;"><strong>User Details </strong>
</legend>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="Inline"
ChildrenAsTriggers="true">
<ContentTemplate>
<div align="center">
<table style="width: auto; max-width: 100%">
<tr>
<td colspan="100%" style="text-align: right">
<asp:Label runat="server" ID="lbl" Text="* Fields are Mandatory." ForeColor="red"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: right">
<strong style="font-size: small">Name<label style="color: Red">*</label>
:</strong>
</td>
<td style="text-align: left">
<asp:TextBox runat="server" ID="txtName" placeholder="Enter Name" TabIndex="1"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter Name!"
ControlToValidate="txtName"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" ErrorMessage="Please Enter Text Only!"
ControlToValidate="txtName" SetFocusOnError="True" ValidationExpression="^[a-zA-Z]*$">
</asp:RegularExpressionValidator>
</div>
</td>
</tr>
<tr>
<td style="text-align: right">
<strong style="font-size: small">DOB<label style="color: Red">*</label>
:</strong>
</td>
<td style="text-align: left">
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional" RenderMode="Block">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtDOB" placeholder="e.g.: 28-Oct-1989" TabIndex="2"></asp:TextBox>
<cc1:CalendarExtender ID="calDOB" runat="server" TargetControlID="txtDOB" Format="dd-MMM-yyyy">
</cc1:CalendarExtender>
<div>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDOB" runat="server" ErrorMessage="Please Enter DOB!"
ControlToValidate="txtDOB"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rgeDob" runat="server" ControlToValidate="txtDOB"
ErrorMessage="Please enter DOB in dd-mmm-yyyy format" SetFocusOnError="True"
ValidationExpression="^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$"
Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>
</div>
</td>
<td style="text-align: right">
<strong style="font-size: small">Age :</strong>
</td>
<td style="text-align: left">
<asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
<%--<asp:Label runat="server" ID="lblAge"></asp:Label>--%>
</td>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
</Triggers>
</asp:UpdatePanel>
</tr>
<tr>
<td style="text-align: right">
<strong style="font-size: small">Group<label style="color: Red">*</label>
:</strong>
</td>
<td style="text-align: left">
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddlGroup" TabIndex="3" AutoPostBack="True">
<asp:ListItem Value="0">Select Value</asp:ListItem>
<asp:ListItem Value="1">Gold</asp:ListItem>
<asp:ListItem Value="2">Silver</asp:ListItem>
<asp:ListItem Value="3">Bronze</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
</Triggers>
</asp:UpdatePanel>
<div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator_ddlGroup" runat="server" ErrorMessage="Please Select Group!"
ControlToValidate="ddlGroup" Display="Dynamic" SetFocusOnError="True" InitialValue="0">
</asp:RequiredFieldValidator>
</div>
</td>
<td style="text-align: Left">
<asp:CheckBox runat="server" ID="chkActive" Text="Active" TabIndex="4" />
</td>
</tr>
<tr>
<td>
<%--<asp:ValidationSummary ID="ValidationSummary1" runat="server" />--%>
</td>
</tr>
<tr>
<td colspan="100%" align="center">
<asp:Button runat="server" ID="btnAdd" Text="ADD" TabIndex="5" OnClientClick="return fnValidate();" />
</td>
</tr>
</table>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:GridView runat="server" ID="gvDetails" Style="width: 100%;" align="center" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" Wrap="true" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" Wrap="true" />
</asp:BoundField>
<asp:BoundField DataField="Group" HeaderText="Group">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" Wrap="true" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" Wrap="true" />
</asp:BoundField>
<asp:BoundField DataField="Active" HeaderText="Active">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" Wrap="true" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" Wrap="true" />
</asp:BoundField>
<asp:BoundField DataField="Age" HeaderText="Age">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" Wrap="true" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" Wrap="true" />
</asp:BoundField>
<asp:TemplateField HeaderText="Operation">
<ItemTemplate>
<asp:ImageButton ID="lnkDelete" runat="server" ImageUrl="~/images/remove.png" Height="15px"
Width="15px" ToolTip="Delete" TabIndex="6" OnClientClick="return confirm('Are You Sure You Want To Delete This Row?');" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="7%" Wrap="true" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="7%" Wrap="true" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
</Triggers>
</asp:UpdatePanel>
</fieldset>
<%--<div style="width: 60%; margin-left: 20%; margin-right: 10%">
<fieldset>
<legend style="text-align: Left; font-size: medium;"><strong>Saved Details </strong>
</legend>
</fieldset>
</div>--%>
</div>
</form>
<script type="text/javascript">
// var username = new Array();
// var len = username.length;
$("#txtDOB").blur(function() {
debugger;
var content = {};
content.dob = $("#txtDOB").val().toString();
var jsonText = JSON.stringify(content);
$.ajax({
type: "POST",
url: "Test_NET_27.aspx/GetAge",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonText,
success: function(data) {
debugger;
var dataget = $.parseJSON(data.d);
$("#txtAge").val(dataget[0].age);
},
error: function(ex) {
alert("Error Occured While Calculating Age! : " + ex);
}
});
});
function fnValidate() {
if ($("#txtName").val() == "") {
alert("Please Enter UserName");
$("#txtName").focus();
return false;
}
else {
var alphaValidate = /^[a-zA-Z ]*$/;
if (!alphaValidate.test($("#txtName").val())) {
alert("Please Enter Only Text in User Name");
$("#txtName").val() = "";
$("#txtName").focus();
return false;
}
}
if (jQuery.inArray($("#txtName").val().toUpperCase(), username) == 0) {
alert("Please Enter Unique UserName.");
return false;
}
if ($("#txtDOB").val() == "") {
alert("Please Enter Birth Date.");
return false;
}
if ($("#ddlGroup option:selected").val() == 0) {
alert("Please Select Group!");
$("#ddlGroup").focus();
return false;
}
//username[len] = $("#txtName").val().toUpperCase();
return false;
}
</script>
</body>
</html>
Imports Newtonsoft.Json
Imports System.Data
Partial Class Test_NET_27
Inherits System.Web.UI.Page
#Region "Variables"
Private VS_Grid As String = "Null"
#End Region
#Region "PageEvents"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.txtAge.Attributes.Add("readonly", "readonly")
End Sub
#End Region
#Region "Button Event"
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim dt_grid As New DataTable
Dim dr As DataRow
Try
If Not Page.IsValid Then
Me.Validate()
Exit Sub
End If
If (Me.ViewState(VS_Grid) Is Nothing) Then
dt_grid.Columns.Add("Name")
dt_grid.Columns.Add("Group")
dt_grid.Columns.Add("Active")
dt_grid.Columns.Add("Age")
'dt_grid.Columns.Add("Operation")
Else
dt_grid = Me.ViewState(VS_Grid)
End If
dr = dt_grid.NewRow
dr("Name") = Me.txtName.Text.ToString.Trim
dr("Group") = Me.ddlGroup.SelectedItem.Text.ToString.Trim
dr("Active") = "InActive"
If Me.chkActive.Checked = True Then
dr("Active") = "Active"
End If
dr("Age") = Me.txtAge.Text.ToString.Trim
dt_grid.Rows.Add(dr)
Me.ViewState(VS_Grid) = dt_grid
If dt_grid.Rows.Count > 0 Then
Me.gvDetails.DataSource = dt_grid
Me.gvDetails.DataBind()
Else
Me.gvDetails.EmptyDataText = "No Data Available."
Me.gvDetails.DataSource = Nothing
Me.gvDetails.DataBind()
End If
fnResetValues()
Catch ex As Exception
End Try
End Sub
#End Region
#Region "GridEvents"
Protected Sub gvDetails_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvDetails.RowCommand
Dim index As Integer = e.CommandArgument
Dim dt_grid As New DataTable
Try
dt_grid = Me.ViewState(VS_Grid)
If dt_grid.Rows.Count > 0 Then
If e.CommandName.ToUpper = "DELETE" Then
dt_grid.Rows(index).Delete()
End If
dt_grid.AcceptChanges()
Me.ViewState(VS_Grid) = dt_grid
Me.gvDetails.DataSource = dt_grid
Me.gvDetails.DataBind()
Me.UpdatePanel2.Update()
End If
Catch ex As Exception
End Try
End Sub
Protected Sub gvDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDetails.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
CType(e.Row.FindControl("lnkDelete"), ImageButton).CommandArgument = e.Row.RowIndex
CType(e.Row.FindControl("lnkDelete"), ImageButton).CommandName = "DELETE"
End If
End Sub
Protected Sub gvDetails_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvDetails.RowDeleting
End Sub
#End Region
#Region "WebMethod"
<Web.Services.WebMethod()> _
Public Shared Function GetAge(ByVal dob As String) As String
Dim dt_jsonData As New DataTable
Dim dr As DataRow = dt_jsonData.NewRow
Dim age As Integer
Dim jsonString As String = String.Empty
Try
age = Math.Floor(DateDiff(DateInterval.Month, DateValue(dob), Now()) / 12)
dt_jsonData.Columns.Add("age")
dr("age") = age.ToString.Trim()
dt_jsonData.Rows.Add(dr)
If dt_jsonData.Rows.Count <> 0 Then
jsonString = JsonConvert.SerializeObject(dt_jsonData)
End If
Return jsonString
Catch ex As Exception
Return ""
Exit Function
End Try
End Function
#End Region
#Region "Reset Values"
Public Function fnResetValues() As Boolean
Me.txtName.Text = ""
Me.txtDOB.Text = ""
Me.txtAge.Text = ""
Me.ddlGroup.SelectedValue = 0
Me.chkActive.Checked = False
End Function
#End Region
End Class
当我移除
时,javascript
的{{1}}也是如此
fnValidate()
if (jQuery.inArray($("#txtName").val().toUpperCase(), username) == 0) {
alert("Please Enter Unique UserName.");
return false;
}
VB端的点击事件未执行。
答案 0 :(得分:1)
删除除一个jQuery库之外的所有文件
添加行时没有唯一ID。将ID =“txtDOB”更改为class =“txtDOB”,将其他字段更改为ID
将您的代码更改为
$("#UpdatePanel3").on("blur",".txtDOB",function() {
debugger;
var content = {"dob": $(this).val()};
var jsonText = JSON.stringify(content);
答案 1 :(得分:0)
已经完成!!!
我需要做的就是在onblur
中添加textbox
并修改功能中的模糊事件。即。
<asp:TextBox runat="server" ID="txtDOB" placeholder="e.g.: 28-Oct-1989" TabIndex="2"
onblur="blurFunction();"></asp:TextBox>
function blurFunction() {
debugger;
var content = {};
content.dob = $("#txtDOB").val().toString();
var jsonText = JSON.stringify(content);
$.ajax({
type: "POST",
url: "Test_NET_27.aspx/GetAge",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonText,
success: function(data) {
debugger;
var dataget = $.parseJSON(data.d);
$("#txtAge").val(dataget[0].age);
},
error: function(ex) {
alert("Error Occured While Calculating Age! : " + ex);
}
});
}