Community discussion forum

how to load the dropdownlist box without postback?

  • 1 year ago

     

    How to load the dropdownlist box without  postback?

     

    eg:

    if you choose country in one dropdown the corresponding states wil fetch in another dropdown without postback

    pls help.... 

     

  • 1 year ago

    Try this site:

    http://www.asp.net/ajax/

  • 1 year ago

     

    is it possible in javascript? 

     

  • 1 year ago

    Hi

     We can do it in javascript.

    I found out the following code to solving my problem.

     

    Javascript Code - ASPX page



    function Assign_State()
    {
    var id=  document.getElementById("DDLCountry").value;
    var select2 = document.Form1.DDLState;
    select2.options.length=0;
    select2.options[0] = new Option("--Select--","");
    if (id!="")
    {
    for(i=0;i
    {
    select2.options[i+1] = new Option(state_list[id][i],state_id_list[id][i]);
    }
                        }
    }
    function ShowID()
    {
    alert(document.getElementById("DDLState").value)
    return false;
    }

    ASPX - Body Content



    <table align="center">
    <tr>
    <td><asp:Label id="Label1" runat="server">Country List</asp:Label></td>
    <td><asp:DropDownList id="DDLCountry" runat="server"></asp:DropDownList></td>
    </tr>
    <tr>
    <td><asp:Label id="Label2" runat="server">State List</asp:Label></td>
    <td><asp:DropDownList id="DDLState" runat="server"></asp:DropDownList></td>
    </tr>
    <tr>
    <td></td>
    <td align="right"><asp:Button id="BtnShow" runat="server" Text="Show"></asp:Button></td>
    </tr>
    </table>


    ASPX - Code Behind


    Imports System.Data.OleDb
    Public Class FrmDynamicList
       Inherits System.Web.UI.Page
    #Region " Web Form Designer Generated Code "

       'This call is required by the Web Form Designer.
        Private Sub InitializeComponent()

       End Sub
       Protected WithEvents Label1 As System.Web.UI.WebControls.Label
       Protected WithEvents Label2 As System.Web.UI.WebControls.Label
       Protected WithEvents DDLCountry As System.Web.UI.WebControls.DropDownList
       Protected WithEvents DDLState As System.Web.UI.WebControls.DropDownList
       Protected WithEvents BtnShow As System.Web.UI.WebControls.Button

       'NOTE: The following placeholder declaration is required by the Web Form Designer.
       'Do not delete or move it.
       Private designerPlaceholderDeclaration As System.Object

       Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
           'CODEGEN: This method call is required by the Web Form Designer
           'Do not modify it using the code editor.
           InitializeComponent()
       End Sub

    #End Region
       Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           DDLCountry.Attributes.Add("onchange", "Assign_State()")
           BtnShow.Attributes.Add("OnClick", "return ShowID()")
           If Not Page.IsPostBack Then

               Dim state_list As Hashtable
               Dim state_id_list As Hashtable

               Dim count As Integer
               Dim constr As String, sql As String

               Dim conn As OleDbConnection
               Dim ada As OleDbDataAdapter
               Dim country_ds As DataSet

               constr = "Provider=MSDAORA.1;User ID=1;Password=;Data Source=;Persist Security Info=True"

               Try
                   conn = New OleDbConnection(constr)
                   conn.Open()

                   state_list = New Hashtable
                   state_id_list = New Hashtable

                   sql = "Select * from test_country"
                   ada = New OleDbDataAdapter(sql, conn)
                   country_ds = New DataSet
                   ada.Fill(country_ds)

                   For count = 0 To country_ds.Tables(0).Rows.Count - 1
                       state_list.Add(country_ds.Tables(0).Rows(count)("country_id"), GetStringFromDS(country_ds.Tables(0).Rows(count)("country_id"), "state_name"))
                       state_id_list.Add(country_ds.Tables(0).Rows(count)("country_id"), GetStringFromDS(country_ds.Tables(0).Rows(count)("country_id"), "state_id"))
                   Next

                   ConvertToJsArray(state_list, "state_list")
                   ConvertToJsArray(state_id_list, "state_id_list")

                   Dim dr As DataRow = country_ds.Tables(0).NewRow
                   dr(1) = "--Select--"
                   country_ds.Tables(0).Rows.InsertAt(dr, 0)
                   DDLCountry.DataSource = country_ds
                   DDLCountry.DataValueField = "country_id"
                   DDLCountry.DataTextField = "country_name"
                   DDLCountry.DataBind()
               Catch ex As Exception
                   Response.Write(ex.Message)
               Finally
                   conn.Close()
                   conn = Nothing
               End Try
           End If
       End Sub
       Private Function GetStringFromDS(ByVal id As String, ByVal field As String) As String
           Dim constr As String, sql As String, state_list As String
           constr = "Provider=MSDAORA.1;User ID=;Password=;Data Source=;Persist Security Info=True"
           Dim conn As OleDbConnection
           Dim ada As OleDbDataAdapter
           Dim state_ds As DataSet
           Dim count As Integer

           Try
               conn = New OleDbConnection(constr)
               conn.Open()
               sql = "Select " & field & " from test_state where country_id=" & id
               ada = New OleDbDataAdapter(sql, conn)
               state_ds = New DataSet
               ada.Fill(state_ds)
               For count = 0 To state_ds.Tables(0).Rows.Count - 1
                   state_list = state_list & "'" & state_ds.Tables(0).Rows(count)(field) & "'" & ","
               Next
               state_list = Mid(state_list, 1, Len(state_list) - 1)
               GetStringFromDS = state_list
           Catch ex As Exception
               Response.Write(ex.Message)
           Finally
               conn.Close()
               conn = Nothing
           End Try
       End Function
       Function ConvertToJsArray(ByVal State_list As Hashtable, ByVal array_name As String)
           Dim script_array As String
           script_array = Chr(13) & " " & Chr(13)
           script_array = script_array & array_name & "= new  Array(" & State_list.Count & ")" & Chr(13)
           Try
               Dim myEnumerator As IDictionaryEnumerator = State_list.GetEnumerator()
               While myEnumerator.MoveNext()
                   script_array = script_array & array_name & "['" & myEnumerator.Key & "'] = new  Array(" & myEnumerator.Value & ")" & Chr(13)
               End While
               script_array = script_array & " "
               Response.Write(script_array)
           Catch ex As Exception
               Response.Write(ex.Message)
           End Try
       End Function
    End Class


  • 1 year ago

    Hi Jeganeedi,

    I am also trying to acheive the samething.Ur code is so useful, I am beginner in .net and Javascripts. I tried to use ur code in my usercontrol. I am getting error in javascript at state_list[id][i] as undeclared. How u r passing those values in the javascript. Can u please help me in this.

     

    Thanks,

  • 1 year ago

    I got the same error 'state_list' is undefined. Any help?

    Jas

  • 1 year ago

    thnx

  • 1 year ago
    It's great code sample. Thank you, Kimsea

Post a reply

Enter your message below

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

Want to stay in touch with what's going on? Follow us on twitter!