Showing posts with label second. Show all posts
Showing posts with label second. Show all posts

Saturday, March 24, 2012

Populate Dropdownlist with arraylist

I thought that by using the follwing code that the dropdown value wouldbe the second item mentioned in my list item but it seems to be thefirst that get in the selectedTex as well as the selectedValuefield. Is there something that I am missing so that theselectedValue would be the second item of my listeItem?
Thank you
<code>
'********************************************************************************
'********************************************************************************
Private Sub insertTables()
'********************************************************************************
Dim lstColums As New ArrayList
lstColums.Add(New ListItem("Software", "Cl_Systems.Software"))
lstColums.Add(New ListItem("SystemType", "Cl_Systems.Software"))
lstColums.Add(New ListItem("SystemPurchaseDate", "Cl_Systems.SystemPurchaseDate"))
lstColums.Add(New ListItem("ServicePlan", "Cl_Systems.ServicePlan"))
lstColums.Add(New ListItem("ServicePlan_Purchase", "Cl_Systems.ServicePlan"))
lstColums.Add(New ListItem("ServicePlan_WarrentyEnd", "Cl_Systems.WarrentyEnd"))
lstColums.Add(New ListItem("ServicePlan_RenewalDate", "Cl_Systems.RenewalDate"))
lstColums.Add(New ListItem("BuiltID", "Cl_Systems.BuiltID"))
lstColums.Add(New ListItem("Software Version", "Cl_Systems.Version"))
lstColums.Add(New ListItem("Modules", "Cl_Systems.Modules"))
lstColums.Add(New ListItem("Packages", "Cl_Systems.Packages"))
lstColums.Insert(0, "--Select Field--")
ddFields.DataSource = lstColums
ddFields.DataBind()
End Sub
</code>
You added this "lstColums.Insert(0, "--Select Field--")" at the first posion which is "0" and you didn't set your selectedvalue when the DDL loaded, so the default selectedvalue is the first one.

Event if I take that line away it give me the same result.


Are you really creating the list like this? If so, I suggest either (a) creating a ListItemCollection instead, or (b) skipping the intermediate object altogether and just adding the items to the dropdownlist's Items collection.

Wednesday, March 21, 2012

Populating a checkbox list

Hi - I can get this to work with the first pass, but when it reads the second, it errors with a System.NullReferenceException at the mylistitem.selected=true line.

My CheckBoxList is named cbAvail - and I have a comma delimmited list in a sesion variable "activitywhentext" which holds the text of the checkbox. I am trying to iterate through the comma list, find the checkbox item which relates to it, and select it.

Thanks for any help.

Mark


..
.
Dim i as integer
For i = 0 to mtArrNumItems(Session("activitywhennum"))
Dim mylistitem as new listitem
mylistitem = cbAvail.Items.FindByValue(mtArrRetItem(Session("activitywhennum"),i))
mylistitem.selected = true
Next
..
.
.
Function mtArrNumItems(ByVal mtArr as String) as Integer
Dim arr as Array
arr=Split(mtArr,",")
Return Ubound(arr)
End Function

Function mtArrRetItem(ByVal mtArr As String, ByVal index as integer) as String
Dim arr as Array
arr=Split(mtArr,",")
Return arr(index)
End Function

Have you debugged this and made certain that

cbAvail.Items.FindByValue(mtArrRetItem(Session("activitywhennum"),i))

actually returns an item? If this returns null then your mylistitem object will be null and you'd get an error. In order to figure out why it doesn't find anything you'd have to tell us where this code executes and what your page load event looks like.

Friday, March 16, 2012

Populating a listbox from DB w/o postback

Hi,

I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However, currently the code is such that with each selection there is a postback. We want to avoid it using filter and javascript. I am not using ADO.NET but adodbc and no datagrids or datasets (please don't tell me that i should as my boss clearly doesn't want to get into it at this stage).

How can i do it?

Thanks

Currently the code with the postback is as follows:
page_load

if not page.ispostback then
Do While Not rs.EOF
li = New ListItem(rs("cat_name").Value, rs("cat_id").Value)
List1.Items.Add(li)
rs.MoveNext()
Loop

End If

'List1.SelectedIndex = 0
rs.Close()
rs = Nothing

List1.SelectedValue = cat_id

'here there is some other code not relevant to the listboxes

'select items for second listbox
Dim rsSub As New ADODB.Recordset
rsSub.Open("SELECT sub_name, sub_id FROM subs WHERE cat_id=" & cat_id.ToString & " ORDER BY sub_name ASC", cn)
Dim l_item As ListItem
Do While Not rsSub.EOF
l_item = New ListItem(rsSub("sub_name").Value, rsSub("sub_id").Value)
List2.Items.Add(l_item)
rsSub.MoveNext()
Loop

List2.SelectedValue = sub_id

rsSub.Close()
rsSub = Nothing

cn.Close()
End If
' End If

End If

End Sub

Protected Sub Select1Change(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim cn As New ADODB.Connection
cn.Open(YBayTools.Constants.ConnectionString)

Dim rs As New ADODB.Recordset
Dim li As ListItem

rs.Open("Select * from subs where cat_ID =" & List1.SelectedItem.Value.ToString, cn)
'rs.Open("Select * from subs where cat_ID ='" & List1.SelectedItem.Text.ToString & "' & List1.SelectedItem.value.ToString", cn)

Dim strCat As String

If List2.Items.Count > 0 Then
List2.Items.Clear()
End If

If rs.BOF And rs.EOF Then
Response.Write("no records found")
Else

Do While Not rs.EOF
li = New ListItem(rs("sub_name").Value, rs("sub_id").Value)
List2.Items.Add(li)
rs.MoveNext()

Loop

End If

rs.Close()
rs = Nothing
cn.Close()You code is done to work with postback. So write client-site code with javascript on the first listBox and in this code, read the DB and populate you other listBox.
That's the thing I am not sure how to write it in jscript.
I need to get the data for both listboxes from a db.
You can use an XmlHttp object from client-side script, but this will probably only work on IE and Mozilla. Other solutions could use a lot of script and hidden frames (beuark!)