![]() |
| ||
| Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) This a just a basic webform setup of a listbox being populated with data from a database, in this case the Northwind Access database, and then populating textboxes with data related to the item the user selects. 1. WebForm -Create an ASP.NET webform page -Add a listbox server control and set AutoPostBack property to True -Add 9 textbox server controls and give them appropriate ID's to match the FieldNames from the datasource. i.e txtProductID for ProductID, txtProductName for ProductName, etc -Add a checkbox server control for the discontinued option for a particular product. -For visual design purposes I would recommended associated labels to indicate what will appear in the textboxes. Sample Code: - Partial Code listing <form id="Form1" method="post" runat="server"> 2. Web.Config Settings - Connection String -Modify to meets your particular setup. -I prefere to place the connection string in the web.config file :cool: as it does add a performance boost, as well as simplify coding a web application that consistantly uses the same DataSource. -CASE SENSITIVE! :D Sample Code: - Partial Code listing <configuration> 3. Write the code behind for the WebForm a. Import Required Namespaces Imports System.Web.Security ' ||||| Required Class for Authentication b. Create Class Variable(s) Dim MyConn As OleDbConnection = New OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
c. Add the code in the Page_Load Event to populate the listbox Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListLoad() Subroutine ' ||||| FILL THE LIST BOX ||||| -Now that you have the listbox populated, what about the textboxes? -Well, what we want to happen is that when the user selects any item in the listbox we want to have the data on that item populate the textboxes. -The first two steps have been done to make this work. The first was setting the AutoPostBack property to True for the listbox, and the second was setting the DataValueField (primary key) and DataTextField for the listbox data. The third part is now obtain the data on the selection the user has made, which is done by the code in the SelectedIndexChanged event SelectIndexChanged code : rivate Sub lstProducts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstProducts.SelectedIndexChanged Now just "run" the webform and you are done. Of course this is a simple set of code and concept, but the principles that you can now build on from here will enable you do a variety to things, like dropdown menu lists, or listboxes of background color choices, just to name a few. I could have done a number of things different, or have add more funcitonality, which I do plan to do. As I modify and expand this concept further I will post the changes and the results. Happy Coding! :cool: |
| ||
| Re: Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) Additions and Changes to the Above - For Supplier & Category DropDownLists: 1. Change both txtSupplier and txtCategory to a DropDownList server control 2. Change the SQL string in ListLoad() subroutine to the following: ' ||||| Create SQL String 3. Add TWO new variables to the ListLoad() subroutine ' ||||| Variables to Store/Pass SupplierID & CategoryID 4. Modify the Try...Catch block in the ListLoad() subroutine to the following: Try 5. Create the LoadSuppliers & LoadCategories subroutines -Code provided is the for LoadSuppliers subroutine, which is a carbon copy of the LoadCategories subroutine Private Sub LoadSuppliers(ByVal intValue As Integer) These are just some minor changes to add further functionality to the initial code. Happy Coding! :cool: |
| ||
| Adding Next & Previous Record Controls And addition of sorts. This small addition will allow the user to click a Next and Previous button to move within the data in the listbox and dynamically update the textboxes/drop downlists displaying the item details. Add two Webform button controls, give them appropriate ID's and add the code for their OnClick events. Next Button Private Sub imgNext_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNext.Click Previous Button Private Sub imgPrev_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPrev.Click It should be noted that I used an ImageButton control, but you can use the standard webform button control or even a HyperLink button control if you want. Just be sure to modify the above code accordingly. Happy Coding :cool: |
| All times are GMT -4. The time now is 10:10 am. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC