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

Drop Down List - SelectedIndexChange How to Code

Status
Not open for further replies.

tmunson99

MIS
Jun 18, 2004
62
US
I have two drop down lists. One for property name and one for department name. The drop down lists are binding fine. When I selected department from the list and save the record, the first department is being saved (the department at index(0) and not the department I selected. I'm sure I need code for a selectedindexchange, but I don't know how to do that. This is what I have so far:

Dim dsProperty As DataSet
Dim dsDepartment As DataSet

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.txtDate.Text = Now().ToShortDateString
dsProperty = WorkOrdersDB.GetProperty 'Fill the dataset
dsDepartment = WorkOrdersDB.GetDepartment
Me.BindPropertyDropDownList() ' Fill the drop down list
Me.BindDepartmentDropDownList()

End Sub

Private Sub BindPropertyDropDownList()
ddlProperty.DataSource = dsProperty
ddlProperty.DataMember = "tblProperty"
ddlProperty.DataTextField = "PropertyName"
ddlProperty.DataValueField = "PropertyID"
ddlProperty.DataBind()

End Sub

Private Sub BindDepartmentDropDownList()
ddlDepartment.DataSource = dsDepartment
ddlDepartment.DataMember = "tblDepartment"
ddlDepartment.DataTextField = "Department"
ddlDepartment.DataValueField = "DepartmentId"
ddlDepartment.DataBind()

End Sub

Private Sub ddlDepartment_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ddlProperty.SelectedIndexChanged

End Sub
 
Where's the routine that saves the record? That's where the problem probably is. You need to get the current value of the DropDownList.

Dim myDept as string = ddlDepartment.SelectedItem.Value
Dim myProperty as string = ddlProperty.SelectedItem.Value
 
Hi,

you will a postback check in the load event. the first department is being saved each time since the droplists are being loaded every time the page loads (performance ramifications here as well). so,

in the load event, wrap this if statement around your code -
if not page.IsPostBack then


end if

You will need to set enabelviewstate=true for the drop lists controls. in the index changed event,
var_ddl_selected_index = ddl.selectedindex
var_ddl_selected_value = ddl.selectedvalue
var_ddl_selected_item = ddl.selecteditem

HTH

amit
 
Thanks, Amit, that did the trick. The selected department was saved to the table. Now I have:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Me.txtDate.Text = Now().ToShortDateString
dsProperty = WorkOrdersDB.GetProperty 'Fill the dataset
dsDepartment = WorkOrdersDB.GetDepartment 'Fill the dataset
Me.BindPropertyDropDownList() ' Fill the drop down list
Me.BindDepartmentDropDownList() 'Fill the drop down list

End If

End Sub

How do I 'clear' the page and its text boxes, etc. I have autopostback set to false on the drop down list and enableviewstate set to true. I'm having a hard time understanding these two properties. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top