I have 3 dropdownlist controls on one web page and these controls get populated from database one after user selects one of the values of previous dropdownlist control. For example, values in 3rd dropdownlist gets populated when user selects value from second dropdownlist. Values in second dropdownlist gets populated when user selects value from first dropdownlist control. First dropdownlist control gets populated during page load. AutoPostBack property is true for first control as well as second control. when user selects a value from first control, it calls a function which shall populate the second control and i am checking IsPostBack property before second control gets populated. some how it does not pass through IsPostBack property.
below is my vb.net code
can someone help me out?
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Partial Public Class new_log
Inherits System.Web.UI.Page
Dim strVariable As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
If Not Page.IsPostBack Then
ViewState("PanelSeed") = 1
Panel1.Visible = True
Panel2.Visible = False
cmbUnit.Items.Clear()
loadunits()
cmbRequesttype.Items.Clear()
loadRequesttypes()
End If
End Sub
Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox2.TextChanged
End Sub
Sub loadunits()
Dim conn As New SqlConnection(AppSettings("connString"))
Dim cmdSelect As New SqlCommand("Select unit,unit_id from unit", conn)
Dim dtrReader As SqlDataReader
Try
conn.Open()
dtrReader = cmdSelect.ExecuteReader()
Dim sel As New ListItem
sel.Text = "Please Select"
sel.Value = ""
cmbUnit.Items.Add(sel)
While dtrReader.Read
Dim li As New ListItem()
li.Value = dtrReader("unit_id")
li.Text = dtrReader("unit")
cmbUnit.Items.Add(li)
End While
dtrReader.Close()
conn.Close()
Catch ex As Exception
End Try
End Sub
Protected Sub cmbUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbUnit.SelectedIndexChanged
'Dim conn As New SqlConnection(AppSettings("connString"))
'conn.Open()
'Dim com As New SqlCommand("select unit_id from unit where unit=" & Val(cmbUnit), conn)
'conn.Close()
End Sub
Protected Sub cmbRequest_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbRequesttype.SelectedIndexChanged
cmbcategory.Items.Clear()
cmbTitle.Items.Clear()
cmbcategory.Items.Insert(0, "--Select--")
cmbTitle.Items.Insert(0, "--Select--")
loadcategory()
cmbcategory.Focus()
'Dim conn As New SqlConnection(AppSettings("connString"))
'conn.Open()
'Dim com As New SqlCommand("select request_id from request_type where request_type=" & Val(cmbRequest), conn)
'conn.Close()
End Sub
Sub loadcategory()
Try
If cmbRequesttype.SelectedIndex = 0 Then
Exit Sub
End If
Dim con As New SqlConnection(AppSettings("connString"))
strVariable = Nothing
Dim cmdSelect As New SqlCommand("Select category,category_id from category where request= " & cmbRequesttype.SelectedValue, con)
Dim Reader As SqlDataReader
con.Open()
Reader = cmdSelect.ExecuteReader()
If Reader.HasRows Then
With cmbcategory
.DataSource = Reader
.DataValueField = "category_id"
.DataTextField = "category"
.DataBind()
End With
End If
Reader.Close()
con.Close()
Catch ex As Exception
End Try
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
Dim CurrentPanel As String = "Panel" + ViewState("PanelSeed").ToString()
ViewState("PanelSeed") = CInt(ViewState("PanelSeed")) + 1
Dim NextPanel As String = "Panel" + ViewState("PanelSeed").ToString()
Dim p As Panel = CType(FindControl(CurrentPanel), Panel)
p.Visible = False
p = CType(FindControl(NextPanel), Panel)
p.Visible = True
If CInt(ViewState("PanelSeed")) = 2 Then
End If
End Sub
Sub loadRequesttypes()
Dim cob As New SqlConnection(AppSettings("connString"))
Dim cmdSelect As New SqlCommand("Select request,request_id from request_type", cob)
Dim dr As SqlDataReader
Try
cob.Open()
dr = cmdSelect.ExecuteReader
Dim sel As New ListItem
sel.Text = "Please Select"
sel.Value = ""
cmbRequesttype.Items.Add(sel)
While dr.Read
Dim li As New ListItem()
li.Value = dr("request_id")
li.Text = dr("request")
cmbRequesttype.Items.Add(li)
End While
dr.Close()
cob.Close()
Catch ex As Exception
End Try
End Sub
Protected Sub cmbcategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbcategory.SelectedIndexChanged
cmbTitle.Items.Clear()
cmbTitle.Items.Insert(0, "--Select--")
loadTitle()
cmbTitle.Focus()
'Dim conn As New SqlConnection(AppSettings("connString"))
'conn.Open()
'Dim com As New SqlCommand("Select category,category_id from category where request=cmbRequesttype.SelectedItem.Text", conn)
'conn.Close()
End Sub
Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrevious.Click
Dim CurrentPanel As String = "Panel" + ViewState("PanelSeed").ToString()
ViewState("PanelSeed") = CInt(ViewState("PanelSeed")) - 1
Dim PrevPanel As String = "Panel" + ViewState("PanelSeed").ToString()
Dim p As Panel = CType(FindControl(CurrentPanel), Panel)
p.Visible = False
p = CType(FindControl(PrevPanel), Panel)
p.Visible = True
End Sub
Sub loadTitle()
Try
If cmbcategory.SelectedIndex = 0 Then
Exit Sub
End If
Dim con As New SqlConnection(AppSettings("connString"))
strVariable = Nothing
Dim cmdSelect As New SqlCommand("Select title,title_id from title where request= " & cmbcategory.SelectedValue, con)
Dim Reader As SqlDataReader
con.Open()
Reader = cmdSelect.ExecuteReader()
Dim sel As New ListItem
sel.Text = "Please Select"
sel.Value = ""
cmbTitle.Items.Add(sel)
While Reader.Read
Dim li As New ListItem()
li.Value = Reader("title_id")
li.Text = Reader("title")
cmbTitle.Items.Add(li)
End While
Reader.Close()
con.Close()
Catch ex As Exception
End Try
End Sub
End Class