Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnSelectedIndexChange.... 1

Status
Not open for further replies.

kizmar2

Programmer
May 25, 2004
164
US
.NET really makes me feel stupid. I'm trying to make a simple form with one dropdown box, populated but data from a SQL server database.

The drop down is populating fine, but the OnSelectedIndexChanged event isn't working the way I would think it should.

Here's my aspx.vb:
Code:
<form id="frmMealType" runat="server">				 <table border="0" cellpadding="0" cellspacing="0" height="350">
  <tr valign="top">
   <td width="100" class="title">Meal Type</td>
   <td>
     <asp:DropDownList id="ddlMealType" runat="server" CssClass="dropdownlist"  AutoPostBack="True" OnSelectedIndexChanged="ddlMealType_SelectedIndexChanged" />
   </td>
  </tr>
  <tr>
   <td colspan="2">
     <h1><asp:Label id="lblStatus" runat="server" /></h1>
   </td>
  </tr>
</table>

</form>

Here's the VB codebehind I'm trying to use:
Code:
Public Sub ddlMealType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMealType.SelectedIndexChanged
   ' When a meal is selected, set session
   Session("Serving") = Me.ddlMealType.SelectedItem.Value

   ' Testing
   Me.lblStatus.Text = "Redirecting..."

   ' Send user to serving.aspx to start scanning students
   Server.Transfer("serving.aspx")
End Sub

Let me know if this isn't enough to tell what's wrong. I have no clue why it's not doing anything when I select something.

KizMar
------------
 
Are you saying the SelectedIndexChanged event is not being called? Have you traced through the code?
 
Well it's trying to do something... I'm getting "Error: '__EVENTTARGET' is null or not an object". This only happens when I'm using the "AutoPostBack" option. If I'm not using autopostback, it doesn't appear to do anything.

Would IsPostBack effect this issue at all, or should I not even have to worry about that for this?

KizMar
------------
 
inside Page_Load()

are you doing this:

if not page.ispostback then
'bind your dropdown
end if


-DNG
 
Here's the full code behind:
Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient

Public Class serveMeal
	Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

	End Sub
	Protected WithEvents ddlMealType As System.Web.UI.WebControls.DropDownList
	Protected WithEvents lblStatus As System.Web.UI.WebControls.Label

	'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
	Dim myConnection As New SqlConnection(globalClasses.GetDSN())
	Dim objDataAdapter As New SqlDataAdapter
	Dim objDataSet As New DataSet


	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' Open connection
		myConnection.Open()

		'Put user code to initialize the page here
		If IsPostBack Then
			' Doing nothing here right now
		Else
			' Fill fields with data to be chosen
			FillMealTypesList()
		End If

		' Close connection
		myConnection.Close()
	End Sub

	Private Sub FillMealTypesList()
		Me.ddlMealType.Items.Clear()

		' Build SQL query
		Dim sqlQuery = "SELECT collectionID, collectionItem FROM collections WHERE groupID = 1 ORDER BY collectionItem"

		Dim myCommand As New SqlCommand(sqlQuery, myConnection)

		' Bind data to the list
		objDataAdapter.SelectCommand = myCommand
		objDataAdapter.Fill(objDataSet, "meal_types")
		Me.ddlMealType.DataSource = objDataSet.Tables("meal_types").DefaultView
		Me.ddlMealType.DataValueField = "collectionID"
		Me.ddlMealType.DataTextField = "collectionItem"
		Me.ddlMealType.DataBind()
		Me.ddlMealType.Items.Insert(0, "Select")
		Me.ddlMealType.AutoPostBack = True
	End Sub

	Public Sub ddlMealType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMealType.SelectedIndexChanged
		' When a meal is selected, set session
		'	1 = Breakfast
		'	2 = Brunch
		'	3 = Lunch
		'	4 = Dinner
		'	20 = Other
		Session("Serving") = Me.ddlMealType.SelectedItem.Value

		' Testing
		Me.lblStatus.Text = "Redirecting..."

		' Send user to serving.aspx to start scanning students
		Server.Transfer("serving.aspx")
	End Sub
End Class

KizMar
------------
 
Try using the IsPostBack as dvannoy suggested and see what happens
 
try these modifications:

Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient

Public Class serveMeal
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ddlMealType As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lblStatus As System.Web.UI.WebControls.Label

    '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
    Dim myConnection As New SqlConnection(globalClasses.GetDSN())
    Dim objDataAdapter As New SqlDataAdapter
    Dim objDataSet As New DataSet


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If NOT Page.IsPostBack Then
    FillMealTypesList()
    End If

End Sub

    Private Sub FillMealTypesList()
     Try   
     ' Open connection
        myConnection.Open()

        ' Build SQL query
        Dim sqlQuery = "SELECT collectionID, collectionItem FROM collections WHERE groupID = 1 ORDER BY collectionItem"

        Dim myCommand As New SqlCommand(sqlQuery, myConnection)

        ' Bind data to the list
        objDataAdapter.SelectCommand = myCommand
        objDataAdapter.Fill(objDataSet, "meal_types")
        Me.ddlMealType.DataSource = objDataSet.Tables("meal_types").DefaultView
        Me.ddlMealType.DataValueField = "collectionID"
        Me.ddlMealType.DataTextField = "collectionItem"
        Me.ddlMealType.DataBind()
        Me.ddlMealType.Items.Insert(0, "Select")
        Me.ddlMealType.AutoPostBack = True
   Catch(ex as exception)
   Finally
' Close connection
        myConnection.Close()
   End Try
    End Sub

    Public Sub ddlMealType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMealType.SelectedIndexChanged
        ' When a meal is selected, set session
        '    1 = Breakfast
        '    2 = Brunch
        '    3 = Lunch
        '    4 = Dinner
        '    20 = Other
        Session("Serving") = Me.ddlMealType.SelectedItem.Value

        ' Testing
        Me.lblStatus.Text = "Redirecting..."

        ' Send user to serving.aspx to start scanning students
        Server.Transfer("serving.aspx")
    End Sub
End Class

-DNG
 
I figured it out... there's two <form>'s in the aspx file. *DOH!!*

I will be modifying the VB based on some of the code you posted above though, thank you much!!

KizMar
------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top