Button to select data from a drop down list. ASP.net (vb)

Hi,

I have a drop down list that populates information about the product from an access database (vb) when the user makes a selection.

I would like to have minimal product info on the home page so when the user selects the button for example 'view details' they will be taken to the product screen with the item already selected in the drop down list and information showing.

Been going round in circles, hope someone can help. I can post my code for drop down list accessing the database if required.

Many Thanks
GuivarchAsked:
Who is Participating?
 
blandyukConnect With a Mentor Commented:
If you wanted it selected as part of the page load, please place it in the Page_Load() as below:

(Remember, you have to populate your object before setting the SelectedValue, hence I moved your object to be above "If Not IsPostBack Then").
    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Load
        DVDSelected = Me.GetDVDSelected()
        If Not IsPostBack Then
            ddlProducts.DataBind()
            dllProduct.SelectedValue = DVDSelected.DVDID.ToString
        End If
        lblDVDTitle.Text = DVDSelected.DVDTitle
        lblDirector.Text = DVDSelected.Director
        lblSummary.Text = DVDSelected.Summary
        lblDVDPrice.Text = FormatCurrency(DVDSelected.DVDPrice)
        imgProduct.ImageUrl = "Images/" _
            & DVDSelected.DVDImage
    End Sub

Open in new window

0
 
blandyukCommented:
When using the DropDownList, you can simply set the SelectedValue = value:

dllProduct.SelectedValue = prod.ProductID.ToString
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
blandyuk: SelectedValue is ReadOnly.

To set the item by values you must use:

Dim item as ListItem = ddl.Items.FindByValue("YouValue")
if (item <> null)
item.Selected = true


P.S.: this code was written by hand and I'm a C# dev... sorry for any syntax errors.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GuivarchAuthor Commented:
Ill try this as soon as I get home from work!
0
 
blandyukCommented:
AlexCode: Please read the property details for SelectedValue below:

Public Overridable Property SelectedValue() As String
Get the value of the selected item in the list control, or selects the item in the list control that contains the specific value.

It's not ReadOnly.
0
 
GuivarchAuthor Commented:
Couldnt get these solutions working. I have posted my code to see if im inserting it correctly.

Im very new to asp.net and have little experience of it, still trying to get my head round a lot of it.

Basically I have a home page with a list of products and I want the user to click the button and be taken to the order page where the drop down list is populated with data from an access database with the relevant button click.

I have the order page working so when you make a selection it populates information from the access database.

Any help/advice would be great!
*Code for Button on home page*

<asp:Button ID="dvd1Btn" runat="server" CausesValidation="False"  
             PostBackUrl="~/Order.aspx" Text="View Details" />  

*Code for Drop Down List on Order Page*

<asp:DropDownList ID="ddlProducts" runat="server" Width = "253px"
            DataSourceID="AccessDataSource1" DataTextField="DVDTitle"
            DataValueField="DVDID" AutoPostBack="True">
        </asp:DropDownList>

*VB for Order Page*

Imports System.Data

Partial Class Order
    Inherits System.Web.UI.Page

    Private DVDSelected As Product

    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ddlProducts.DataBind()
        End If
        DVDSelected = Me.GetDVDSelected()
        lblDVDTitle.Text = DVDSelected.DVDTitle
        lblDirector.Text = DVDSelected.Director
        lblSummary.Text = DVDSelected.Summary
        lblDVDPrice.Text = FormatCurrency(DVDSelected.DVDPrice)
        imgProduct.ImageUrl = "Images/" _
            & DVDSelected.DVDImage
    End Sub

    Private Function GetDVDSelected() As Product
        Dim dvProduct As DataView = CType( _
            AccessDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
        dvProduct.RowFilter = "DVDID = '" & ddlProducts.SelectedValue & "'"
        Dim Product As New Product
        Product.DVDID = dvProduct(0)("DVDID").ToString
        Product.DVDTitle = dvProduct(0)("DVDTitle").ToString
        Product.Director = dvProduct(0)("Director").ToString
        Product.Summary = dvProduct(0)("Summary").ToString
        Product.DVDPrice = CDec(dvProduct(0)("DVDPrice"))
        Product.DVDImage = dvProduct(0)("DVDImage").ToString
        Return Product
    End Function

Open in new window

0
 
blandyukCommented:
On your DropDownList, I can see the BindData() but where is the DataSource?

ddlCurrency.DataSource = ??
ddlCurrency.DataTextField = "????"
ddlCurrency.DataValueField = "????"
ddlProducts.DataBind()

When Using "ddlProducts.SelectedValue = ???" , you use the DataValueField.
0
 
GuivarchAuthor Commented:
Do you mean the AccessDataSource?
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
            DataFile="~/App_Data/dvd.mdb"
            SelectCommand="SELECT [DVDID], [DVDTitle], [Director], [Summary], [DVDImage], [DVDPrice] 
            FROM [DVD] ORDER BY [DVDTitle]">
        </asp:AccessDataSource>

<asp:DropDownList ID="ddlProducts" runat="server" Width = "253px"
            DataSourceID="AccessDataSource1" DataTextField="DVDTitle"
            DataValueField="DVDID" AutoPostBack="True">
        </asp:DropDownList>

Open in new window

0
 
blandyukCommented:
Ah, didn't see that :) I don't work with databases like that. As "DVDID" is your DataValueField, you should be able to set the selected value using what I mentioned above.
0
 
GuivarchAuthor Commented:
As you can probably tell im very new to this!

I understand the concept behind it but keep getting errors when I apply it.
My understanding is I should be looking to select a string from DVDID to match the data using;

dllProduct.SelectedValue = prod.ProductID.ToString

Would this be applied within the code for my button?

<asp:Button ID="Button2" runat="server" Text="Button"
  PostBackUrl="~/Order.aspx " />

Really appreciating all your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.