Dropdownlist using Ajax problem

  • 10 years ago

    Hi
    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; 
     clearDll("ddlEquipmentID");
     clearDll("ddlVendor");
        if(iManageYear!=0)
        {   
      AjaxFile.getEquipmentid(vManageYear,machinetype, GetEquipmentRecords_CallBack);
     }
        else
        {
      document.getElementById("ddlEquipmentID").selectedIndex=0;
      document.getElementById("ddlVendor").selectedIndex=0;
      return
        }












    }

    function GetEquipmentRecords_CallBack(response)
    {
        var ds = response.value;
        var i = 0;
        var iLength = 0;
        if (response.error != null){
            return;
        }
        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)
    {
     iLength=document.getElementById(ddlname).options.length;
     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
            Ajax.Utility.RegisterTypeForAjax(GetType(AjaxFile))
            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.





    NC.

     

Post a reply

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

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

Contribute

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.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup