Results 1 to 19 of 19

Thread: [RESOLVED] Binding Drop Down List

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Resolved [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:

    1. AppendDataBoundItems = True
    2. AutoPostBack = False
    3. 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:
    1. Protected Sub BindAuthorityDDL()
    2.  
    3.         Me.ddlAuthority.Items.Clear()
    4.  
    5.         Try
    6.  
    7.             Using da As New SqlDataAdapter("SELECT Activity, TimeUnit FROM tblATU WHERE FieldName = 'ddlAuthority' ORDER BY Activity ASC", ConfigurationManager.ConnectionStrings("DEEstimatorConnectionString").ToString())
    8.  
    9.                 Dim dt As New DataTable
    10.                 da.Fill(dt)
    11.                 da.Dispose()
    12.                 Me.ddlAuthority.DataSource = dt
    13.                 Me.ddlAuthority.DataTextField = "Activity"
    14.                 Me.ddlAuthority.DataValueField = "TimeUnit"
    15.                 Me.ddlAuthority.DataBind()
    16.  
    17.             End Using
    18.  
    19.         Catch ex As Exception
    20.  
    21.         End Try
    22.  
    23.     End Sub

    VB Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.         If Not Page.IsPostBack Then
    4.  
    5.             BindAuthorityDDL()
    6.  
    7.         End If
    8.  
    9.     End Sub

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Re: Binding Drop Down List

    Quote Originally Posted by gep13 View Post
    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.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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:
    <&#37;@ 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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Cool 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.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    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. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Re: Binding Drop Down List

    Quote Originally Posted by gep13 View Post
    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?"

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    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. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question Re: Binding Drop Down List

    If there anyway of overcoming this as some items will have the same value?

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    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. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Binding Drop Down List

    Hey,

    What does the value represent though?

    Gary

  17. #17
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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. #18

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Re: Binding Drop Down List

    Quote Originally Posted by mendhak View Post
    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...

  19. #19
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width