Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 22nd, 2010, 05:02 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
Default

Simple solution using VB and SQL Server

in .aspx

Code:

<asp:SqlDataSourceID="SqlDataSource1"runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT [DriverID], DriverFirstname + ' ' + DriverLastName as DriverName FROM [Driver]">
</asp:SqlDataSource>
<asp:DropDownListID="DriverDropDownList"runat="server"AutoPostBack="True"DataSourceID="SqlDataSource1"DataTextField="DriverName"DataValueField="DriverID">
</asp:DropDownList>
<br/>
<asp:LabelID="DriverIDLabel"runat="server"Text="Label"></asp:Label>
<br/>

In Code behind .aspx.vb

Code:
ProtectedSub DriverDropDownList_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles DriverDropDownList.SelectedIndexChanged
DriverIDLabel.Text = DriverDropDownList.SelectedValue
EndSub

This displays DriverFirstName and DriverLastName
When changed then DriverIDLabel displays the DriverID

To convert to C# try http://www.developerfusion.com/tools/


I rarely use XML as a datasource so can't really help you with this.

If you programatically load the data then using for each line of date
Code:
DriverDropDownList.Items.Add(New ListItem(DriverFirstname & " " & DriverLastName,  DriverID))
You will have a new item added to the dropdown list.

Finally you may want to add
Code:
DriverDropDownList.Items.Add("Select Driver")
Before programmatically filling your drop down list.
 
Old January 22nd, 2010, 05:32 AM
Authorized User
 
Join Date: Jan 2010
Posts: 33
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Timeware,

Thanks for getting back this fast and go to this extent. Actually with your previous reply I was already getting as far as displaying the e on dropdown change event. How do I get the Label to display not the selectedValue but all the data with the index= dropdownlist1.SelectedValue? In my sample code I only threw in FirstName and LastName but there are more fields that I need to display in this Label or a different type of control. No worries on the XML data but can you show me how you do it should the data source be SQL Server? Thanks again.
 
Old January 22nd, 2010, 06:32 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
Default

.aspx

Code:
<asp:LabelID="DriverMobileLabel"runat="server"Text="Label"></asp:Label>

.aspx.vb

Code:
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

and


Code:
ProtectedSub DriverDropDownList_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles DriverDropDownList.SelectedIndexChanged

DriverIDLabel.Text = DriverDropDownList.SelectedValue

Dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim MyReader As SqlDataReader
Dim CT1 AsString
Dim CaseTrackConnectionString AsString
CaseTrackConnectionString = ConfigurationManager.ConnectionStrings("CollectionConnectionString").ConnectionString
MyConnection = New SqlConnection(CaseTrackConnectionString)
MyCommand = New SqlCommand
CT1 = "SELECT * FROM Driver WHERE DriverID='" + DriverDropDownList.SelectedValue.ToString + "'"
MyCommand.CommandText = CT1
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
MyCommand.Connection.Open()
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
While MyReader.Read()
If MyReader("DriverMobile") IsNot System.DBNull.Value Then
DriverMobileLabel.Text = MyReader("DriverMobile")
EndIf

EndWhile
MyCommand.Dispose()
MyConnection.Close()
MyConnection.Dispose()


EndSub

Any more and I'll be writing your complete system :-)
The Following User Says Thank You to timeware For This Useful Post:
jtnchang (January 22nd, 2010)
 
Old January 22nd, 2010, 07:11 AM
Authorized User
 
Join Date: Jan 2010
Posts: 33
Thanks: 13
Thanked 0 Times in 0 Posts
Smile

Timeware,

Thanks for the prompt reply again. Appreciate it for going the extent to show the solution. Now I know the benefit of using reader method. For I was baffled trying to set asp.net data control datavalue to dropdownlist1.SelectedValue. I know there is a xml reader method but I need to see if it works the similar way as the reader in SqlCommand class. Thanks so much for your relentless support.

Bless,
 
Old January 22nd, 2010, 07:18 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
Default

You're welcome.

I'm sorry but I don't use much XML and any help I could offer on this would probably below you current level. However you should be able use the sample and just replace sections with your alternative reading methods.

I always find when I have a problem like this that I need to break it down into really small sections.Get one bit working and then incorporate it into the final solution.

Don't forget that whenever you generate a solution that works, it may not be perfect or the best way to purists but if it works you can't knock it!

Good Luck.
The Following User Says Thank You to timeware For This Useful Post:
jtnchang (January 22nd, 2010)
 
Old January 22nd, 2010, 04:33 PM
Authorized User
 
Join Date: Jan 2010
Posts: 33
Thanks: 13
Thanked 0 Times in 0 Posts
Smile

Timeware,

I have been very helpful and got me out of my jam. Thanks for your time, efforts, your code and your word of wisdom. Later.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retain selected values from dropdown list?? skhan ASP.NET 3.5 Basics 5 September 1st, 2009 09:13 AM
How to highlight selected dropdown values. sheetm J2EE 0 June 5th, 2007 05:04 AM
selected item in dropdown not saved on postback rathbird ADO.NET 3 March 6th, 2006 08:44 AM
Getting selected item in dropdown to match XML juliaPGATOUR XSLT 1 December 11th, 2003 05:56 AM
Title: dropdown list as selected according to the san_only ASP.NET 1.x and 2.0 Application Design 1 June 4th, 2003 01:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.