Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Last active August 29, 2015 13:57

Revisions

  1. Chris Weathers revised this gist Mar 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ajax.vb
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ ddlInstitution.Attributes.Add("onchange", "return abc()")
    '<WebMethod()> set call a web service in the file code behind, note a webservice page does not really work
    'its best to call the web method in the same page in which the AJAX resides
    ' <ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _ dictates if the web method accepts
    a HTTP 'GET' or 'POST' request
    'a HTTP 'GET' or 'POST' request
    ' it also determines the return type (JSON or XML) which is declared in the AJAX

    <WebMethod()>
  2. Chris Weathers revised this gist Mar 28, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ajax.vb
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@ ddlInstitution.Attributes.Add("onchange", "return abc()")
    ' This ajax call gets a drop down list and appends it to a dropdown on the mark up side
    '<WebMethod()> set call a web service in the file code behind, note a webservice page does not really work
    'its best to call the web method in the same page in which the AJAX resides
    ' <ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _ dictates if the web method accepts a HTTP 'GET' or 'POST' request
    ' <ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _ dictates if the web method accepts
    a HTTP 'GET' or 'POST' request
    ' it also determines the return type (JSON or XML) which is declared in the AJAX

    <WebMethod()>
  3. Chris Weathers created this gist Mar 28, 2014.
    85 changes: 85 additions & 0 deletions ajax.vb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    'first bind the ajax function to the dom object
    ddlInstitution.Attributes.Add("onchange", "return abc()")

    ' This ajax call gets a drop down list and appends it to a dropdown on the mark up side
    '<WebMethod()> set call a web service in the file code behind, note a webservice page does not really work
    'its best to call the web method in the same page in which the AJAX resides
    ' <ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _ dictates if the web method accepts a HTTP 'GET' or 'POST' request
    ' it also determines the return type (JSON or XML) which is declared in the AJAX

    <WebMethod()>
    <ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _
    Public Shared Function BindDatatoDropdown() As ddDetails()
    Dim dt As New DataTable()
    Dim details As New List(Of ddDetails)()

    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("CRTCConnectionString").ConnectionString)
    Using cmd As New SqlCommand("SELECT dataText FROM dSchool", con)
    con.Open()
    Dim da As New SqlDataAdapter(cmd)
    da.Fill(dt)
    For Each dtrow As DataRow In dt.Rows
    Dim value As New ddDetails()
    value.ddValue = dtrow("dataText").ToString()
    value.ddName = dtrow("dataText").ToString()
    details.Add(value)
    Next
    End Using
    End Using
    Return details.ToArray()
    End Function
    Public Class ddDetails
    Public Property ddValue() As String
    Get
    Return dd_value
    End Get
    Set(ByVal value As String)
    dd_value = value
    End Set
    End Property
    Private dd_value As String
    Public Property ddName() As String
    Get
    Return dd_Name
    End Get
    Set(ByVal value As String)
    dd_Name = value
    End Set
    End Property
    Private dd_Name As String
    End Class
    End Class

    '---------------------------------------------------------------------------------------------------------------------

    'The AJAX on the markup side

    <script type="text/javascript">
    function abc() {

    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "applicantInfo.aspx/BindDatatoDropdown",
    data: "{}",
    dataType: "json",
    success: function (response) {
    var Dropdown = $('#<%=ddSchool.ClientID%>');
    $.each(response.d, function (index, value) {
    Dropdown.append($("<option></option>").val(value.ddValue).html(value.ddName));
    });
    },
    error: function (xhr, ajaxOptions, thrownError) {
    'these xhr object gives us verbose metadata about an ajax error
    alert("xhr.responseText= " + xhr.responseText);
    alert("xhr.responseStatus= " + xhr.responseStatus);
    alert("xhr.readyState" + xhr.readyState);
    alert("thrownError= " + thrownError);

    }
    });
    }

    'the drop down that calls the 'abc()' Ajax function
    <asp:DropDownList ID="ddlInstitution" CssClass="ddlNormal" DataTextField ="dataText" DataValueField ="dataValue" AutoPostBack ="true" runat="server" />
    </script>