Dropdownlist using Ajax problem

  • 13 years ago

    I have a problem on Dropdownlist that connect database
    and retrieve data by Ajax.I write javascript like this

    function ManageYear_onchange(machinetype)

     var iManageYear=document.forms[0].ddlManageYear.selectedIndex;
     var vManageYear=document.forms[0].ddlManageYear[iManageYear].value; 
      AjaxFile.getEquipmentid(vManageYear,machinetype, GetEquipmentRecords_CallBack);


    function GetEquipmentRecords_CallBack(response)
        var ds = response.value;
        var i = 0;
        var iLength = 0;
        if (response.error != null){
        if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0))
     //insert data to dropdown
            for(i=0; i<ds.Tables[0].Rows.length; i++)
                document.getElementById("ddlEquipmentID").options.add(new Option(ds.Tables[0].Rows[i]["EquipmentName"],ds.Tables[0].Rows[i]["EquipmentID"]))

    function clearDll(ddlname)
     for(i=0; i<iLength; i++)
                document.getElementById(ddlname).options[0] = null;
            document.getElementById(ddlname).options.add(new Option("--กรุณาเลือก--",0));

    and i insert ManageYear_onchange() at code behind like this

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            If Not Me.IsPostBack Then
                Me._MachineType = Request.QueryString("MachineType")
                ddlManageYear.SelectedValue = clsUtil.DateTimeNow.Year
                ddlManageYear.Attributes.Add("onchange", "return ManageYear_onchange('" & Me._MachineType & "');")
                Me.RegisterStartupScript("startup", "<script> ManageYear_onchange('" & Me._MachineType & "');</script>")
            End If
        End Sub

    on Ajax page i connect to server and send dataset like this

        <Ajax.AjaxMethod()> _
    Public Function getEquipmentid(ByVal ManageYear As Integer, ByVal Machinetype As String) As DataSet
            Dim ds As New DataSet
            Dim aclsQuery As New clsDBSQLBased
            Dim selectQuery As String

            selectQuery = "select  distinct cast(e.EquipmentId as varchar) + ' : ' + e.[description] [EquipmentName],e.EquipmentId [EquipmentID] from Eim_Equipment e,Eit_ManageEquipment m " + _
            " where e.equipmentid=m.equipmentid and " & _
            " m.ManageYear=" & ManageYear & _
            " and e.MachineType='" & Machinetype & "'"
            ds = aclsQuery.GetDataSet(selectQuery)
            Return ds

        End Function

    The error is when i click submit button
    after i was selected value from dropdown
    and postback to server .
    The selectedvalue is reset to the first data of dropdown(selectindex =0)
    I try to search and debug code (and debug javascript).
    Nothing wrong and now i don't know what to do if you can help me
    It's very helpful.Thanks.



Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie