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!

DropDownList is driving me Nuts!!!

Status
Not open for further replies.

Sidrp

IS-IT--Management
Jun 4, 2003
44
US
Hi All..
I just moved over to VS.net and trying to design an application using ASP.net. I am having some trouble with the
DropDownList.
1. The event SelectedItemChanged doesnot fire.
2. I used a button and this also reloads the page. I am sure there are some tags I missed.
Any help is appriciated.
thanks
Sid
heres my code:
--------------------------------------------------------------

Imports System.Data.SqlClient

Public Class updateBuild
Inherits System.Web.UI.Page
' Dim DdlSerials As New System.Web.UI.WebControls.DropDownList
'Public Event ListChanged(ByVal sender As Object, ByVal e As EventArgs)

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlConn = New System.Data.SqlClient.SqlConnection
Me.SqlComm = New System.Data.SqlClient.SqlCommand
'
'SqlConn
'
Me.SqlConn.ConnectionString = "workstation id=ASPSERVER;packet size=4096;integrated security=SSPI;data source=AS" & _
"PSERVER;persist security info=False;initial catalog=PCTracker"
'
'SqlComm
'
Me.SqlComm.CommandText = "SELECT PCData.* FROM PCData WHERE (SerialNO = @SerialNo) OR (SONo = @SONo)"
Me.SqlComm.Connection = Me.SqlConn
Me.SqlComm.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SerialNo", System.Data.SqlDbType.NVarChar, 50, "SerialNO"))
Me.SqlComm.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SONo", System.Data.SqlDbType.NVarChar, 50, "SONo"))

End Sub
Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents tbxSerialNumber As System.Web.UI.WebControls.TextBox
Protected WithEvents tbxSalesOrderNo As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
Protected WithEvents SqlComm As System.Data.SqlClient.SqlCommand
Protected WithEvents lblErrorMsg As System.Web.UI.WebControls.Label
Protected WithEvents plhSelect As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents lblText As System.Web.UI.WebControls.Label
Protected WithEvents DgrRecord As System.Web.UI.WebControls.DataGrid
Protected WithEvents DdlSerials As System.Web.UI.WebControls.DropDownList
Protected WithEvents Button2 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
AddHandler DdlSerials.SelectedIndexChanged, AddressOf DdlSerials_SelectedIndexChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SqlDS As New DataSet
Dim SqlDV As New DataView


' Dim DdlSerials As New DropDownList

If tbxSerialNumber.Text = "" And tbxSalesOrderNo.Text = "" Then
lblErrorMsg.Text = " Enter Atleast one value"

Else
' Response.Write("IN ELSE")
Try
SqlConn.Open()
SqlComm.Parameters("@SerialNo").Value = CStr(tbxSerialNumber.Text)
SqlComm.Parameters("@SONo").Value = CStr(tbxSalesOrderNo.Text)

Dim SqlAdapter As New SqlDataAdapter
SqlAdapter.SelectCommand = SqlComm

SqlAdapter.Fill(SqlDS)
'SqlDV.RowFilter = "SerialNO = '" & CStr(tbxSalesOrderNo.Text) & "'"
'Response.Write(SqlDV.Count)
SqlConn.Close()

If SqlDS.Tables(0).Rows.Count = 0 Then
lblText.Text = "No Records found"
Else
If SqlDS.Tables(0).Rows.Count = 1 Then
lblText.Text = "1 match"
Else
If SqlDS.Tables(0).Rows.Count > 1 Then

lblText.Text = SqlDS.Tables(0).Rows.Count & " records found"
DdlSerials.Items.Clear()
DdlSerials.Visible = True
' DdlSerials.AutoPostBack = False
DdlSerials.DataTextField = "SerialNo"
DdlSerials.DataValueField = "SerialNo"
DdlSerials.DataSource = SqlDS.Tables(0)
DdlSerials.DataBind()
DdlSerials.Items.Insert(0, "Select Serial No")
plhSelect.Controls.Add(DdlSerials)

End If
End If
End If
Catch ex As Exception
lblText.Text = ex.ToString

Catch ex As SqlException
lblText.Text = ex.ToString

End Try

SqlConn.Close()
End If
End Sub


Private Sub DdlSerials_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'This never fires
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' lblErrorMsg.Text = DdlSerials.SelectedItem.Text
'This gives an error and reloads the page
End Sub
End Class
-----------------------------------------------------------
 
Drop lists have their AutoPostback property set to False by default...set it to true and your event will fire.

Wrap whatever is in your Page Load with an

If Not Page.IsPostBack Then
'Do stuff on first load only
End If

This will ensure that you only load things on the first load, and not every time the page is requested.

hth

D'Arcy
 
Thanks for the prompt reply..
Heres what I am trying to do..
I have a form on the page where a user enters a OrderNo, on clicking a button I am loading a DropDownBox with the list of items in that order.
I am using a Dataset to populate the drop down box. Now once the user sees the List, he can pick one of the items to view the details in a DataGrid.
My problem is, I am not loading anything when page loads, everything is event driven, so my Page_Load is empty. Do you have a better way to approach this?
I did wht you said before, but all it does is instaed of firing the event(method), it just refreshes the page and gives me an empty DropDownBox..

Let me know whts the best way to go about this.
thanks
Sid
 
Here's how I see the form flowing, and hte events used:

1. User enters an Order Number.
2. User clicks Button1
3. Button1_Click() event fires, which fills the drop list
4. User selects an item from the drop list
5. DropList_SelectedIndexChanged() is fired, which gets the
details (from a database I'm assuming) and binds it to
the datagrid.

You should be able to make this page work with only two methods: one for the button click and one for the drop list selected index changed.

hth

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top