I'm using Visual Web Developer 2008 Express Edition and I need your assistance since I'm new to it. I want to insert a record into my listview control of my asp.net webpage using c# or vb.net codes. Here's how it works, if I have four textboxes and I'm going to fill each textboxes so I click a command button I want to insert the value of each textboxes into the listview control. Please guide on how to do this. Thank you and I appreciate your help.
1 Answer
What you are looking for is Data Binding, there is a myriad of Exemple to start form, here some exemples
- http://www.codeproject.com/KB/webforms/CompleteListView.aspx
- http://geekswithblogs.net/rashid/archive/2007/09/09/Asp.net-ListView---DataBinding.aspx
Update :
Here some vb.net code to help you, I did not test it, it's just to show you the principle.
private Class person
public FirstName as string
public LastName as string
public Address as string
end class
So you could do something like this in your button Click event
Protected Sub Add_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Add.Click
'create the new object
dim newPerson as person = new person
newPerson.FirstName = FirstNameTextBox.text
newPerson.LastName = LastNameTextBox.text
newPerson.Address = AddressTextBox.text
'Get or create a list
dim personList As List(Of person) = Session("personList")
if personList is nothing then
personList = new List(Of person)
end if
'add it to a list and save
personList.Add(newPerson )
Session("personList") = personList
'Bind the list
personListView.DataSource = personList
personListView.Databind()
end sub
3 Comments
timmack
@DavRob--------not necessarily databinding, because when you talk about databinding the record comes from your database. All I want is just to add the records directly from your textboxes into the listview control just like we normally did in windows application. How possible is that?
DavRob60
IT' long to explain, but in short, one way of doing this is to write a class with the properties you need. then you will be able create a instance of this class, put it in a collection and bind the collection to the list. If you want to add more than one item, you will have to retrieve and save your collection in the session.
timmack
For now its very hard for me to figure it out but if you can provide me a sample code then would be so much better.