|
|
#1 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
Hi All,
I currently have two dropdown lists which are bound to a SQL DataSource. The two controls have the following properties set:
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:
VB Code:
|
|
|
|
|
|
#2 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#3 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
Re: Binding Drop Down List
|
|
|
|
|
|
#4 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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>
Does it work? Gary
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#5 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
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. |
|
|
|
|
|
#6 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#7 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
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.
|
|
|
|
|
|
#8 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#9 | |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
Re: Binding Drop Down List
Quote:
That is correct... What do you mean by "Can you show the result of your query that you are executing?" |
|
|
|
|
|
|
#10 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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 Gary
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#11 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
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 |
|
|
|
|
|
#12 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#13 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
If there anyway of overcoming this as some items will have the same value?
|
|
|
|
|
|
#14 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#15 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
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. |
|
|
|
|
|
#16 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Binding Drop Down List
Hey,
What does the value represent though? Gary
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
|
|
#17 |
|
ASP.NET Moderator
Join Date: Feb 02
Location: Ulaan Baator GooGoo: Frog
Posts: 37,382
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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.
|
|
|
|
|
|
#18 |
|
Lively Member
Join Date: Jan 06
Posts: 111
![]() |
Re: Binding Drop Down List
|
|
|
|
|
|
#19 |
|
ASP.Nut
Join Date: Nov 04
Location: The Granite City
Posts: 9,868
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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
__________________
Remember to mark your thread as resolved. Check out the new Forum features, including tagging here. Remember to rate posts that help. Check out my site - ASP.Net Website running on CentOS 5.3 with Mono Free Stuff: Microsoft WebsiteSpark|Microsoft DreamSpark Learning Resources: MSDN|LearnVisualStudio|TrainingSpot|ScottGu's Blog|ASP.Net Starter Kits|Regex|RegExLib Useful Tools: XPath Builder|UltraMon|RegExBuddy|CopySourceAsHtml|TracExplorer|SQLyog|Chart Controls for .Net|SharePoint Designer|CodeRush Express Coding Links: XPath|ConnectionStrings|VB and MySQL|MySQL Connector.Net|My.Settings ADO.Net: MSDN Reference|Introduction|Using Access|Always use Parameters|Save and Retrieve Data - jm|An Explanation - jm Code Bank Submissions: Code Snippets|Profile Provider|Serialization: C# VB|Restricted Menu|Compressed HttpWebRequest |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|