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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList Problem 1

Status
Not open for further replies.

royyan

Programmer
Jul 18, 2001
148
US
I have a couple of DropDownList on my form. Once I am trying to submit them, they always send the first item to the database. Following is part of code related to the DropDown List.

Dim dsAccountManager As New DataSet()
Dim daAccountManager As New SqlDataAdapter()
daAccountManager.SelectCommand = cmdAccountManager
daAccountManager.Fill(dsAccountManager, "AcctMgrs")
ddlAcctMgr.DataSource = dsAccountManager
ddlAcctMgr.DataMember = "AcctMgrs"
ddlAcctMgr.DataTextField = "AccountManager"
ddlAcctMgr.DataValueField = "AcctMgrID"
ddlAcctMgr.DataBind()
Dim li As New ListItem("", "0")
ddlAccountManager.Items.Insert(0, li)
ddlAcctMgr.Items.Insert(0, li)

Try
ddlAcctMgr.Items.FindByText(Session("AccountManager")).Selected = True
Catch
ddlAcctMgr.SelectedIndex = 0
End Try

Any feedback regarding this post is highly appreciated!
 
Your going to have to specify what you want here royyan. I have no idea what your problem is from what you described. If you can provide a better description of what you are trying to do and what is actually happening I'll try and help. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I think royyan is having a problem wherein the dropdownlist shows the field values to which it is bound correctly. The user can select a new item from the list (say they select the 10th item) and it appears to be the new SelectedItem.

However, if you set a breakpoint on the ddlAcctMgr_SelectedIndexChanged event you see that when a user selects a new item the event is not fired; the actual selected index is still the default (0) position.

Then lets say there is a "Submit" button that is supposed to pass the selecteditem to a query as a variable. As soon as you click on that button you see that the SelectedIndexChanged is THEN fired; the index position is still zero (0).

So I think the question deals with the getting the DropDownList to react the way you expect it to (i.e. a user selects an item from the bound control and it is reflected in the SelectedIndex property and does not get reset to 0 when a button is clicked).

In case it's not obvious, I'm having the same problem.

Thanks,

O.
 
This is a result of the value property of the dropdownlist not being unique
penny1.gif
penny1.gif
 
I found out it was not keeping the selectedindex because I wasn't checking IsPostBack in the Page_load event; the values kept getting reset.

 
Thank you guys for all your replies. I think Olichap already gave us a correct answer.
 
Found your post and I think I'm having a similar problem...

I'd like to change the value in a text box based on the value selected from a drop down box (the drop down contains the Department Code and the text box will contain the Department Name), but I've set breakpoints and yet never see it enter the ddDeptCode_SelectedIndexChanged() event.

Here's what I've got that isn't working. Can you clarify what you mean about testing the IsPostBack method?

Thanks in advance,
~Lindsay
================================================
Imports System
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class DeptInfo
Inherits System.Web.UI.Page
Protected WithEvents ddDeptCode As System.Web.UI.WebControls.DropDownList
Protected WithEvents txtDeptName As System.Web.UI.WebControls.TextBox

#Region " Web Form Designer Generated Code "

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

End Sub

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 results As DataTable

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
'Load the values from the database into the Department Code drop down.
If Not IsPostBack Then
'Create a connection string
Dim connString As String
connString = &quot;PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\test2.mdb&quot;

Try
'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()

'Specify the SQL string
Dim strSQL As String = &quot;SELECT * from Department ORDER BY DeptCode&quot;

'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a DataReader
Dim objDataReader As OleDbDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

'Do the DataBinding to the drop down
ddDeptCode.DataSource = objDataReader
ddDeptCode.DataBind()

'Load a shared DataTable for later use
Dim objDataSet As New DataSet()
Dim objAdapter As New OleDbDataAdapter(strSQL, objConnection)
Dim row As DataRow
Dim counter As Integer = 0

'Create a temporary dataset to hold the values from the database
objAdapter.Fill(objDataSet, &quot;Department&quot;)
results = objDataSet.Tables(&quot;Department&quot;)

'Close the datareader/db connection
objDataReader.Close()

Catch x As Exception
txtClose.Text = &quot;Connection failed to open: &quot; + x.ToString()
End Try



Else
'Find the appropriate value for Department Name and change it based on drop down list value
txtDeptName.Text = &quot;testing&quot; 'this doesn't work either

End If

End Sub


Private Sub ddDeptCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddDeptCode.SelectedIndexChanged
txtDeptName.Text = &quot;index changed test&quot;
End Sub


End Class
 
What OliChap means is the part in your page load event where you check to see if this is a post back or the first time the page has been loaded. ie. If Not IsPostBack Then...

Check to see if your postback property is set to true on the dropdown list in design view. That may fix your problem. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top