Trouble reading Attributes from control adapter - asp.net

I am trying to integrate an adapter to get support for the OptGroup tag in .net. i took the same adapter as i usually did (been a while, but no build errors or warnings). I set attributes when i add items into the DropDownList, and attempt to read them in the adapter and they seem to be set to Nothing. I entered breakpoints at points of interest and after being set, i can see the Attributes having a value. And in the adapter, the item remains but without said attribute.
To get every possible glitch out of the way i went into full hardcode for adding items, and here we have the result. The display does not group up, as the !=nothing never is met on the adapter.
declaration of list:
Dim item1 As New ListItem("potatoes", "1")
item1.Attributes("OptionGroup") = "optionGroup1"
Dim item2 As New ListItem("Onions", "2")
item2.Attributes("OptionGroup") = "optionGroup2" 'here the optiongroup is set, and debugger sees value to it.
Me.cboMaterial.Items.Add(item1)
Me.cboMaterial.Items.Add(item2)
where it fails in the adapter:
Dim list As DropDownList = Me.Control
Dim currentOptionGroup As String = ""
Dim renderedOptionGroups As New Collection
For Each item As ListItem In list.Items
If item.Attributes("OptionGroup") = "" Then 'here, the item is worth something (potatoes) but the OptionGroup is nothing.
RenderListItem(item, writer)
Else
If currentOptionGroup <> item.Attributes("OptionGroup") Then
If currentOptionGroup <> "" Then
RenderOptionGroupEndTag(writer)
End If
currentOptionGroup = item.Attributes("OptionGroup")
RenderOptionGroupBeginTag(currentOptionGroup, writer)
End If
RenderListItem(item, writer)
End If
Next
i must be missing something, perhaps trivial, but I've been looking at the same thing for too long. any help would be welcome.
edit: i have tried adding the attribute with secondary syntax... by desperation i guess.
item2.Attributes.Add("OptionGroup","optionGroup2")
it changes nothing :P

Related

Verify existence of an item in a dropdownlist

How do I check for the existence of an item in a dropdownlist in vb.net?
Here's my not working code:
dim ddlTestDropdown as dropdownlist
ddlTestDropdown = New DropDownList()
If(ddlTestDropdown.Items.FindByValue("42") Is Not nothing)
Console.WriteLine("It's there")
End If
it won't let me compare the returned ListItem to nothing
Update: The error is from saying Is Not the fix is to say:
If(Not ddlTestDropdown.Items.FindByValue("42") Is Nothing)
Alternate answer:
Here's what I found to do this. Like #praythyus tried you need to test for contains, but vb.net only lets you do contains on a listitem. So I combined what I did with what he did and this worked:
Dim SetThisIfExists = ddlTestDropdown.Items.FindByValue("42")
If(ddlTestDropdown.Items.Contains(SetThisIfExists))
ddlTestDropdown.SelectedIndex = ddlTestDropdown.Items.IndexOf(SetThisIfExists)
End If
Sorry for giving C# syntax. Can you try with
as #RS said, you need to fill the ddl after initializing.
if(ddlTestDropdown.Items.Contains("42"))
{
}
Or instead of FindByValue can you use FindByText

Simple bind value to textbox in code behind using Telerik OpenAccess

I cannot find a complete example. Found tons on grid and combobox, but not textbox. This test is to lookup a “PhoneTypeName” from a UserPhoneType table with TypeCode = “0” and assign that first value to a asp.net textbox.
Currently, I am getting “Object reference not set to an instance of an object” when setting the text box to "phonetype.FirstOrDefault.PhoneTypeName.ToString"
Using dbContext As New EntitiesModel()
Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = "O")
mytextbox.Text = phonetype.FirstOrDefault.PhoneTypeName.ToString
End Using
----EDIT----
I changed as suggested. I ALSO successfully bound the entire list of PhoneTypes to a droplist control...to confirm the data is accessible. It must be the way I am going about querying the table for a single record here.
I get the same error, but at "Dim type = phonetype.First..."
The record is in the table, but it does not appear to be extracted with my code.
Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext1.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = "M")
Dim type = phonetype.FirstOrDefault
If Object.ReferenceEquals(type, Nothing) = False And Object.ReferenceEquals(type.PhoneTypeName, Nothing) = False Then
mytextbox.Text = type.PhoneTypeName.ToString
End If
In general there are the following two possible reasons for getting this exception:
1) The phonetype list is empty and the FirstOrDefault method is returning a Nothing value.
2) The PhoneTypeName property of the first element of the phonetype list has a Nothing value.
In order to make sure that you will not get the Object reference not set to an instance of an object exception I suggest you add a check for Nothing before setting the TextBox value. It could be similar to the one below:
Dim type = phonetype.FirstOrDefault
If Object.ReferenceEquals(type, Nothing) = False And Object.ReferenceEquals(type.PhoneTypeName, Nothing) = False Then
mytextbox.Text = type.PhoneTypeName.ToString
End If
Fixed it.
I was able to view the SQL string being generated by using this:
mytextbox.text = phonetype.tostring
I saw that the SQL contained "NULL= 'O'"
I did it like the example?!? However, when I added .ToString to the field being queried, it worked.
So the final looks like this:
Using dbContext As New EntitiesModel()
Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode.**ToString** = "O")
mytextbox.Text = phonetype.FirstOrDefault.PhoneTypeName.ToString
End Using
BTW, Dimitar point to check for null first is good advice (+1). The value was nothing as he said.

for each MenuItem RemoveAt

I developed a basic system in that loops through the menuitems in a menu collection on page load (items are hardcoded, so can't use rowdatabound event) and disables those that don't meet a certain user level criteria:
For Each item As MenuItem In NavigationMenu.Items
Dim value As Int32 = Convert.ToInt32(item.Value)
Dim level As Int32 = Convert.ToInt32(Session.Item("uxID"))
If value > level Then item.Enabled = False
Next
It works great and disables all of the menuitems that it should and ignores the rest. The catch is that as time has gone by the amount of menuitems has increased, and its difficult for some users to know what they do and don't have access to.
My understanding is that menuitems don't have a visible property, but can be removed (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitemcollection.removeat.aspx) but I'm stumped on how to get the index of the menuitem in order to do so.
Just do the same:
Dim i As Integer = 0
While i < NavigationMenu.Items.Count
Dim value As Int32 = Convert.ToInt32(NavigationMenu.Items(i).Value)
Dim level As Int32 = Convert.ToInt32(Session.Item("uxID"))
If value > level Then : NavigationMenu.Items.RemoveAt(i)
Else : i += 1
End If
End While
Keep in mind that you can not do a foreach and remove items inside, because you will get an Exception about modifying the array which is looping.

ASP.Net treeview truncates node text

I have a treeview on my ASP.Net page and for some reason the text on some nodes gets cut off, I am programatically adding all the nodes and am aware of the existing issue listed here: http://support.microsoft.com/?scid=kb%3Ben-us%3B937215&x=8&y=13 however I am not changing the font and as you see in the code below this fix does not work for me.
Private Sub populateTreeView()
'Code that gets the data is here
Dim ParentIds As List(Of Integer) = New List(Of Integer)
For Each row As DataRow In ds.Rows
If ParentIds.Contains(row("ParentID")) Then
'' Do Nothing
Else
ParentIds.Add(row("ParentID"))
End If
Next
For Each Parent As Integer In ParentIds
Dim parentNode As New System.Web.UI.WebControls.TreeNode
For Each child In ds.Rows
If (child("ParentID") = Parent) Then
Dim childNode As New System.Web.UI.WebControls.TreeNode
parentNode.Text = child("ParentDescription")
parentNode.Value = child("ParentID")
parentNode.Expanded = False
childNode.Text = child("ChildDescription")
childNode.Value = child("ChildID")
parentNode.SelectAction = TreeNodeSelectAction.None
parentNode.ChildNodes.Add(childNode)
End If
Next
trvItem.Nodes.Add(parentNode)
Next
'This is just added to test the MS fix
trvItem.Nodes(0).Text += String.Empty
End Sub
The strange thing is that this issue only appears in IE, I have tested it in chrome and Firefox and both browsers display the text perfectly.
When I select a node this fixes the problem and all text displays as normal.
Any ideas as to what is going wrong here would be great as I'm clueless right now.
Thanks
tv1.LabelEdit = True
tv1.Nodes(0).Nodes(0).BeginEdit()
tv1.Nodes(0).Nodes(0).NodeFont = oNewFont
tv1.Nodes(0).Nodes(0).EndEdit(False)
tv1.LabelEdit = False
Marking this as closed since I never received an answer which solved the problem.
I managed to work around it by using the javascript postback to select one of the items on load, thus forcing the text to display correctly. I think this is an extension of the bug I linked in my original question.

ASP.NET TreeView control - CheckedNodes is always empty

I have an ASP.NET TreeView control with the "ShowCheckboxes" state set to "All".
If I check the boxes on the tree, then the tree.CheckedNodes property is always "False".
I've also tried looking at the individual Nodes(i).Checked property, but those are also all false.
If I manually set the Checked property to True from code, then it does get reflected in the .CheckedNodes property.
I feel like I must be missing something obvious- why would this simple boolean property fail to reflect what I've done in the UI?
Protected Function GetChosenIDs() As List(Of Guid)
Try
Dim result As List(Of Guid) = New List(Of Guid)
'This loop never executes, because nothing is marked "Checked".
For Each node As TreeNode In tree.CheckedNodes
result.Add(New Guid(node.Value))
Next
Return result
Catch ex As Exception
Throw ex
End Try
End Function

Resources