Make current selection not selectable again with treenodes - asp.net

I have a menu as a treeview and what I'm trying to do is make whatever treenode the user selects not selectable again unless he goes to a different page. How would I do this in asp.net and vb?
update:
ok so far this is what I have going on in the treenode selection changed sub
ViewState(10) = TreeView1.SelectedNode
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.None
If ViewState(10).valuepath <> TreeView1.SelectedNode.ValuePath Then
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.Select
End If
im sure the experts out there know exactly why this wouldn't work though I was surprised it complied. Anyways This gives me an error though I can't think of another way to keep that selected node in
UPDATE:
ViewState(10) = TreeView1.SelectedNode.DataItem
If ViewState(10) <> TreeView1.SelectedNode.DataItem Then
TreeView1.SelectedNode.Selected = ViewState(10)
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.Select
Else
TreeView1.SelectedNode.SelectAction = TreeNodeSelectAction.None
End If
This works for the selectaction.none which is exactly what I wanted but I'm having troubles enabling it again when the user selects another treenode.

This is the answer for anyone who needs this in the future
For Each n As TreeNode In TreeView1.Nodes
For Each a As TreeNode In n.ChildNodes
If a.Selected = True Then
a.SelectAction = TreeNodeSelectAction.None
Else
a.SelectAction = TreeNodeSelectAction.Select
End If
Next
Next

Related

Trouble reading Attributes from control adapter

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

VB / ASP page does not add first item to list in click method

I have written a basic shopping page. When a user clicks an item and valid quantity, they add the item to the cart with the add item button. This should grab the session variable which holds the list, add the new item, and then put the updated list back into the session.
EDIT:
It seems the items ARE being added but not shown until the next page reload. Why?
For the most part it works, except the initial click, for which the count of items remains 0 unless I click add again. From there everything works fine.
I am sure I am overlooking something simple.
In the pages Load Method, I create the new list if no session exists., otherwise using the existing list:
If IsNothing(Session("CustomerItems")) Then
CustomerItemsList = New List(Of CustomerProduct)
'Session("CustomerItems") = CustomerItemsList
Response.Write("Session did not exist yet.")
Else
CustomerItemsList = Session("CustomerItems")
Response.Write("Session Loaded.")
End If
Next the values are added to a list box control from an array to show the items.
Finally I placed these lines. I will use the .IsPostBack page to list all items in the list later on:
If Page.IsPostBack Then
Response.Write("<br>Page has been posted back. There Are Currently " & CustomerItemsList.Count & " items.")
Else
Response.Write("<br>Page has NOT been posted back. There Are Currently " & CustomerItemsList.Count & " items.")
End If
And finally, my click event looks like this:
Protected Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
CustomerItemsList = Session("CustomerItems")
Dim CustomerItem As CustomerProduct
CustomerItem.Description = ProductArray(lstItemDescriptions.SelectedIndex, 0)
CustomerItem.ItemQty = txtItemQty.Text
CustomerItem.BulkFee = ProductArray(lstItemDescriptions.SelectedIndex, 1)
CustomerItem.UnitWeight = ProductArray(lstItemDescriptions.SelectedIndex, 2)
CustomerItem.TotalWeight = Double.Parse(ProductArray(lstItemDescriptions.SelectedIndex, 2)) * Double.Parse(txtItemQty.Text)
CustomerItem.UnitCost = ProductArray(lstItemDescriptions.SelectedIndex, 3)
CustomerItem.TotalCost = CustomerItem.UnitCost * CustomerItem.ItemQty
CustomerItemsList.Add(CustomerItem)
Session("CustomerItems") = CustomerItemsList
End Sub
Now, I've gone over this thing a few times, and its probably simple, but it just doesn't make any sense.

Checkbox list keep disabled

Trying to use checkbox lists in (calendar booking system). The checkbox should be disabled and red if there are any data in the database against the date and hour. This all work perfectly here is the code. Using vb.net
OK i found a way how to clear the checkboxes
Dim i As Integer
Dim chboxItem As ListItem
For Each chboxItem In CheckBoxListMon.Items
i += 1
If (i Mod 1 = 0) Then
chboxItem.Enabled = True
End If
Next
Protected Sub Page_LoadComplete(sender As Object, e As EventArgs) Handles Me.LoadComplete
Try
strQuery = "SELECT BookingDate, checkBoxItem, BookRegUserID,Booked FROM bookings INNER JOIN checkboxitems where checkBoxItem = BookingTime"
MySQLCmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open()
DR = MySQLCmd.ExecuteReader
While DR.Read
bookDate = DR.Item("BookingDate")
bookTime = DR.Item("checkBoxItem")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")
Dim test As String = bookTime.ToString()
Select Case True
Case bookDate = lblMonday.Text And CheckBoxListMon.Items.FindByValue(test) IsNot Nothing
CheckBoxListMon.Items.FindByValue(bookTime).Enabled = False
CheckBoxListMon.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
End Select
End While
DR.Close()
dbCon.Close()
Catch ex As Exception
End Try
End Sub
When the page load it would not change the ones from the database. But when i reload the page it will actually work perfect.
Where can i put the check just to be sure that they are already in the memory.
Any help will be much appreciated. Thanks all.
Petr
You need to refresh the data when another week is selected because you are using the same controls for each week. You should be able to do this in whatever control you are using to toggle through the weeks.
Page_LoadComplete can only be expedted to fire each time a page has completed loading, that is why your controls work when going to another page and back.

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.

How to set the color of the inside of checkbox - asp.net

I am trying to set the inside color of a checkbox (the part that's usually white, waiting to be ticked \ checked). How can I set that to a different color, not the default?
Thank You!
You can't do this with css. Forms are notoriously hard to style (technical explanation from Eric Meyer).
You can, however, use javascript to completely replace the checkbox with an image yet still keep the form encoding of a normal checkbox element. Here's an example using jquery
Public Sub SetColor()
Try
For i As Integer = 0 To gvMaster.Rows.Count - 1
FN = gvMaster.Rows(i).Cells(3).Text
If FN = "Present" Then
gvMaster.Rows(i).Cells(3).ForeColor = Color.Green
End If
If FN = "Absent" Then
gvMaster.Rows(i).Cells(3).ForeColor = Color.Red
End If
AN = gvMaster.Rows(i).Cells(4).Text
If AN = "Present" Then
gvMaster.Rows(i).Cells(4).ForeColor = Color.Green
End If
If AN = "Absent" Then
gvMaster.Rows(i).Cells(4).ForeColor = Color.Red
End If
Next
Catch generatedExceptionName As Exception
End Try
End Sub

Resources