Биты и байты.

Биты и байты.

среда, 18 февраля 2015 г.

Частичное обновление страницы. AJAX+ ASP.NET

Описание вызова ниже, пример взят отсюда  и немного модифицирован.



Aspx страница

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type = "text/javascript">
        function ShowCurrentTime() {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetDocStatus",
                data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
       // alert(response.d);
        document.getElementById("demo").innerHTML = response.d;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 427px"/>
    Your Name :
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
    <input id="btnGetTime" type="button" value="Show Current Time"
        onclick = "ShowCurrentTime()" /><br />
        <asp:Label ID="LabelRefresh" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <p id="demo">Результат</p>
    </form>
</body>
</html>


Обработчик

Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            LabelRefresh.Text = Now.ToString
        End If
    End Sub
    <System.Web.Services.WebMethod()> _
    Public Shared Function GetCurrentTime(ByVal name As String) As String
        Return "Hello " & name & Environment.NewLine & "The Current Time is: " & _
                 DateTime.Now.ToString()
    End Function

    <System.Web.Services.WebMethod()> _
    Public Shared Function GetDocStatus(ByVal name As String) As String
        Dim result As String = ""
        Try
            result = RunSQL("SELECT TOP 1 s.Название  FROM TABLE WHERE c.[Регистрационный номер] = N'" + name + "'")
            Return result + " " + Now.ToString

        Catch ex As SqlException
            result = ex.Message
        End Try
        Return result
    End Function

    Public Shared Function RunSQL(SQLCommand As String) As String
        Dim result As String = ""
        Dim sqlConStr As String = "Data Source=test;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=test"
        Dim conn As New SqlConnection(sqlConStr)
        Try
            Dim a As New SqlDataAdapter(SQLCommand, conn)
            Dim dataset As New DataSet()
            a.Fill(dataset, "Table_Values")
            If dataset.Tables(0).Rows.Count > 0 Then
                result = dataset.Tables(0).Rows(0).Item(0).ToString
            Else
                result = "Не найден"
            End If
        Catch ex As SqlException
            result = ex.Message
        End Try
        conn.Close()
        Return result
    End Function


End Class


Результат работы

About