|
-
Nov 25th, 2009, 12:43 AM
#1
Thread Starter
Lively Member
[RESOLVED] Binding Drop Down List
Hi All,
I currently have two dropdown lists which are bound to a SQL DataSource. The two controls have the following properties set:
- AppendDataBoundItems = True
- AutoPostBack = False
- CausesValidation = True
When a Button Click Event fires, the dropdown lists default to the first value in the SQL DataSource.
I've removed the DataBinding and manually added items and upon clicking a button, the value originally selected in the dropdown list remains as is.
Below is the code I am using to bind one of the drop down lists.
VB Code:
Protected Sub BindAuthorityDDL()
Me.ddlAuthority.Items.Clear()
Try
Using da As New SqlDataAdapter("SELECT Activity, TimeUnit FROM tblATU WHERE FieldName = 'ddlAuthority' ORDER BY Activity ASC", ConfigurationManager.ConnectionStrings("DEEstimatorConnectionString").ToString())
Dim dt As New DataTable
da.Fill(dt)
da.Dispose()
Me.ddlAuthority.DataSource = dt
Me.ddlAuthority.DataTextField = "Activity"
Me.ddlAuthority.DataValueField = "TimeUnit"
Me.ddlAuthority.DataBind()
End Using
Catch ex As Exception
End Try
End Sub
VB Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindAuthorityDDL()
End If
End Sub
-
Nov 25th, 2009, 01:44 AM
#2
Re: Binding Drop Down List
Hey,
I take it the question that you have is that you want the currently selected item in the list to remain after a postback, when you bind it to the DataSource, is that correct?
You imply that, but it is not clearly stated.
Gary
-
Nov 25th, 2009, 01:54 AM
#3
Thread Starter
Lively Member
Re: Binding Drop Down List
 Originally Posted by gep13
Hey,
I take it the question that you have is that you want the currently selected item in the list to remain after a postback, when you bind it to the DataSource, is that correct?
You imply that, but it is not clearly stated.
Gary
Hi Gary,
That would be correct.
-
Nov 25th, 2009, 02:13 AM
#4
Re: Binding Drop Down List
Hey,
With the exception of the querying of the database, I have done exactly the same thing in the following:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDLL()
End If
End Sub
Private Sub BindDLL()
Dim list As New List(Of String)
list.Add("First")
list.Add("Second")
list.Add("Third")
list.Add("Fourth")
list.Add("Fifth")
DropDownList1.DataSource = list
DropDownList1.DataBind()
End Sub
Code:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm3.aspx.vb" Inherits="VBWebApplication.WebForm3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" CausesValidation="True">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
For sake of curiousity, can you create a new page, with just the above?
Does it work?
Gary
-
Nov 25th, 2009, 02:34 AM
#5
Thread Starter
Lively Member
Re: Binding Drop Down List
Hi Gary,
I created a new .ASPX page and copied your code which resulted in the effect that I'm after.
I then changed it to bind to a SQL DataSource and the same thing is happening.
-
Nov 25th, 2009, 02:54 AM
#6
Re: Binding Drop Down List
Hey,
I have just checked an application where I have done a similar thing, and it looks like I have to manually set the SelectedValue of the DropDownList to the currently selected value on subsequent postbacks to the server. This was achieved by putting the selected value of the Drop Down List into a session variable.
What I don't know off the top of my head though is whether this was a "workaround" for me not being able to find a better solution, or whether it was the only way of achieving it.
Gary
-
Nov 25th, 2009, 03:09 AM
#7
Thread Starter
Lively Member
Re: Binding Drop Down List
Hi Gary,
After further investigation, I worked out why the Post Back is defaulting to the first item in the drop down list.
Looking at the Selected Index for the 5 Data Bound Items, there index goes as follows:
0,1,2,0,0
Why this is happening when the Data Bind occurs I have no idea.
-
Nov 25th, 2009, 03:17 AM
#8
Re: Binding Drop Down List
Hey,
Does that mean that if you select the second or the third item in the DDL that it is retained after post back, but if you select the fourth and the fifth options, you get the first item. Is that right?
Can you show the result of your query that you are executing?
Gary
-
Nov 25th, 2009, 03:23 AM
#9
Thread Starter
Lively Member
Re: Binding Drop Down List
 Originally Posted by gep13
Hey,
Does that mean that if you select the second or the third item in the DDL that it is retained after post back, but if you select the fourth and the fifth options, you get the first item. Is that right?
Gary
Hi Gary,
That is correct...
What do you mean by "Can you show the result of your query that you are executing?"
-
Nov 25th, 2009, 03:25 AM
#10
Re: Binding Drop Down List
Hey,
Can you execute the following query against SQL Server:
Code:
SELECT Activity, TimeUnit FROM tblATU WHERE FieldName = 'ddlAuthority' ORDER BY Activity ASC
And show the results that you get?
Gary
-
Nov 25th, 2009, 03:36 AM
#11
Thread Starter
Lively Member
Re: Binding Drop Down List
ACTIVITY TIMEUNIT
Better to Administer 90
Election 25
Grant on Probate 35
Letter to Administer Will Annexed 90
No Grant AAPA 90
-
Nov 25th, 2009, 03:38 AM
#12
Re: Binding Drop Down List
Hey,
So there is your answer.
90 is the same value for 1, 4 and 5, which equates to the 0's that you are seeing.
Normally, you want to make the "value" of the DDL items unique, as this is what is used to distinguish it from another item in the DDL.
Gary
-
Nov 25th, 2009, 03:44 AM
#13
Thread Starter
Lively Member
Re: Binding Drop Down List
If there anyway of overcoming this as some items will have the same value?
-
Nov 25th, 2009, 03:48 AM
#14
Re: Binding Drop Down List
Hey,
Not that I am aware of. Why would the entries have the same value?!?
Surely they would have to have a different value, otherwise, why are they available for selection in multiple places?!?
Gary
-
Nov 25th, 2009, 04:06 AM
#15
Thread Starter
Lively Member
Re: Binding Drop Down List
Hi Gary,
There are different types of activities to select from but some of those activities have the same value. This could change in the future.
-
Nov 25th, 2009, 04:57 AM
#16
Re: Binding Drop Down List
Hey,
What does the value represent though?
Gary
-
Nov 25th, 2009, 02:27 PM
#17
Re: Binding Drop Down List
You need to introduce an ATUID in your tblATU table which is unique and use that as the value in your dropdownlist. When a user selects a value, look at it and use it to get the time value out from the database.
-
Nov 26th, 2009, 12:08 AM
#18
Thread Starter
Lively Member
Re: Binding Drop Down List
 Originally Posted by mendhak
You need to introduce an ATUID in your tblATU table which is unique and use that as the value in your dropdownlist. When a user selects a value, look at it and use it to get the time value out from the database.
Worked as suggested...
-
Nov 26th, 2009, 02:09 AM
#19
Re: Binding Drop Down List
Hey,
Glad to hear that you got it worked out.
Can you remember to go back and mark your thread as resolved (there are links in my signature if you are unsure), helps keep the place tidy.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|