Expanding an ASP.Net TreeView node from code-behind - asp.net

I'm learning how to access the controls of an ASP.Net master page and trying to expand a particular TreeView node. I'm doing this from another page that is not a master page.
objContentPlaceHolder, objLoginView and objTreeView all have a value as confirmed by using the debugger.
Can you look at this code and let us know why the code in the for loop is not executing? It reaches the for loop but just skips over the for loop at that point.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objContentPlaceHolder As ContentPlaceHolder
Dim objLoginView As LoginView
Dim objTreeView As TreeView
objContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolderBody"), ContentPlaceHolder)
If Not objContentPlaceHolder Is Nothing Then
objLoginView = CType(objContentPlaceHolder.FindControl("loginViewMain"), LoginView)
If Not objLoginView Is Nothing Then
objTreeView = CType(objLoginView.FindControl("TreeViewMain"), TreeView)
' Make sure all nodes for Maintenance are expanded.
'--------------------------------------------------
For Each treenode As TreeNode In objTreeView.Nodes
If treenode.Text = "Maintenance" Then
treenode.Expand()
End If
Next treenode
End If
End If
End Sub
* Update *
I changed the page load event handler to a PreRenderComplete event handler and would you believe it worked? Not sure why PreRender didn't but that was it. Thanks again everyone for all the help.

public Sub TreeView_TreeNodeDataBound(ByVal sender As Object, ByVal e As TreeNodeEventArgs )
dim mapNode as SiteMapNode = e.Node.DataItem as SiteMapNode
If mapNode.Title = "Maintenance" then
e.Node.Expand()
End if
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objContentPlaceHolder As ContentPlaceHolder
Dim objLoginView As LoginView
Dim objTreeView As TreeView
objContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolderBody"), ContentPlaceHolder)
If Not objContentPlaceHolder Is Nothing Then
objLoginView = CType(objContentPlaceHolder.FindControl("loginViewMain"), LoginView)
If Not objLoginView Is Nothing Then
objTreeView = CType(objLoginView.FindControl("TreeViewMain"), TreeView)
objTreeView.TreeNodeDataBound += TreeView_TreeNodeDataBound
End If
End If
End Sub
Hope this will help

From your example, it looks like your logic is only checking the root nodes. When dealing with hierarchical data, you need to employ recursive logic to ensure that the entire structure gets evaluated.
Something like this is what you need:
Protected Sub btnSearch_Click(sender As Object, e As EventArgs)
For Each node As TreeNode In TreeView1.Nodes
ExpandNodeByValue("Maintenance", node)
Next
End Sub
Private Sub ExpandNodeByValue(value As String, parentNode As TreeNode)
For Each childNode As TreeNode In parentNode.ChildNodes
If childNode.Value.ToLower() = value.ToLower() Then
childNode.Expand()
End If
If childNode.ChildNodes.Count > 0 Then
ExpandNodeByValue(value, childNode)
End If
Next
End Sub
I would also suggest using a DirectCast instead of CType, at least temporarily, to ensure that the control is being found. You would implement that like this:
Dim objTreeView as TreeView = DirectCast(objLoginView.FindControl("TreeViewMain"), TreeView)
If objTreeView IsNot Nothing Then
'The control was found
End If

Related

Jump to Page in C1PrintDocument

I have a form in my application that is used for previewing a report. It has a C1Ribbon at the top which contains navigation buttons, and a C1PrintPreview displaying in a PreviewPane.
I want the navigation buttons in the ribbon (first, previous, next, last), as well as a text box in which to enter a specific page number, to navigate through the preview of the report accordingly. So far, all the documentation and samples I've found have dealt only with adding hyperlinks directly to the report itself...so I'm having a hard time adapting it to my use.
Below is what I have so far...I don't get any build or run-time errors, it just doesn't do anything.
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Dim nextPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Next)
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
Dim lastPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Last)
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
Dim prevPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Previous)
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
Dim firstPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.First)
End Sub
Private Sub Navigation_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPageNum.KeyDown
If e.Modifiers = Keys.Enter Then
Dim setPage As C1LinkTargetPage = New C1LinkTargetPage(PageJumpTypeEnum.Absolute)
'setPage. = CInt(txtPageNum.Text)
If setPage.PageNo = 0 Then
'what to do if number entered is not a page in document
End If
End If
End Sub
I realize it's likely just because even though I make a C1LinkTargetPage, I don't tell the app what to do with it after that. But I'm not sure how to go about doing that - it's not like there's a "jumptopage" method for the C1PrintPreview (wish it were that easy). Buttons in the ribbon don't have a hyperlink property, so I can't set that when the form loads as in all the samples I found. Not sure where to go from here...
Also I don't even know how I'm supposed to be able to use the Absolute PageJumpTypeEnum...PageNo is read only.
Thank you!
UPDATE 2/25:
I learned that I should be dealing with properties of the Preview Pane...not the C1PrintDocument. With the code below, the navigation buttons and specifying a page number work. My only problem now then is displaying the current page in that page number box. With what I have, PreviewPane.CurrentHistoryEntry just goes to 1 (even if I was on a page other than 1 before-hand).
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
PreviewPane.DoGoNextPage()
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
Dim lastPage As DocumentLocation = New DocumentLocation(report.Pages(report.Pages.Count - 1))
PreviewPane.GotoDocumentLocation(lastPage)
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
PreviewPane.DoGoPreviousPage()
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
Dim firstPage As DocumentLocation = New DocumentLocation(report.Pages(0))
PreviewPane.GotoDocumentLocation(firstPage)
End Sub
Private Sub Navigation_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPageNum.KeyDown
If e.KeyValue = Keys.Enter Then
Dim pageNum As Integer = CInt(txtPageNum.Text)
If (pageNum > 0) And (pageNum <= report.Pages.Count) Then
Dim setPage As DocumentLocation = New DocumentLocation(report.Pages(pageNum - 1))
PreviewPane.GotoDocumentLocation(setPage)
Else
txtPageNum.Text = PreviewPane.CurrentHistoryEntry.ToString
End If
End If
End Sub
They key was "PreviewPane.StartPageIdx"
txtPageNum.Text = (PreviewPane.StartPageIdx + 1).ToString

Object reference not set to an instance of an object in ASP(vb)

I came across to this code that creates control dynamically. I tried it. However, whenever I run it, Object reference not set to an instance of an object error pops out and points out to a certain line in the code( i put ---> on the line). I'm a newbie in this programming language. I don't know what to do.
Here's the code I got:
Imports System.Collections.Generic
'Imports System.Data.Odbc
Partial Public Class main
Inherits System.Web.UI.Page
Private controlCounter As Integer = 0
Private myControlList As List(Of String)
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
myControlList = DirectCast(ViewState("myControlList"), List(Of String))
For Each ctlID As String In myControlList
controlCounter += 1
Dim hyper As New HyperLink()
hyper.ID = ctlID
Dim lineBreak As New LiteralControl("<br />")
PlaceHolder1.Controls.Add(hyper)
PlaceHolder1.Controls.Add(lineBreak)
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
myControlList = New List(Of String)()
ViewState("myControlList") = myControlList
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
controlCounter += 1
Dim hyper As New HyperLink()
hyper.Text = "a new text Box"
hyper.ID = "hyperlink" + controlCounter.ToString()
Dim lineBreak As New LiteralControl("<br />")
PlaceHolder1.Controls.Add(hyper)
PlaceHolder1.Controls.Add(lineBreak)
--> myControlList.Add(hyper.ID)
ViewState("myControlList") = myControlList
End Sub
End Class
please help me out. Thanks.
A button click initiates a postback, and you only set myControlList to a thing if the request is not a post back, so it's nothing.
If you're wanting to create the list on page load, and then only keep adding to it with subsequent clicks, say, then you'll need to shove myControlList into the Session, or something, after creating it, then on button click retrieve it again, add to it, and re-set it in the Session.
To do this add and getting from the Session stuff, do,
Session("someDistinctKey") = myControlList;
myControlList = CType(Session("someDisctintKey"), List(Of String))
respectively.

Failure to update textbox.text

I have a relatively simple ASP.NET problem (I should think) that regrettably I am unable to solve by myself. What I am trying to do is the following:
On a page I load a number of controls (Text Boxes) programmatically;
following this load the user should be able to select a value to load into the Textbox from a panel control that is added to the page following the click of a button
Once the panel is closed, the selected text from the panel should be loaded into the textbox
However, in the vb.net statements below when run the "test" string never makes it to the textbox - any help with resolving this would be greatly appreciated.
Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Controls_Load()
End Sub
Public Sub Controls_Load()
Dim ttf_tb As New TextBox With {.ID = "ttf_tb"}
Master_Panel.Controls.Add(ttf_tb)
Dim ttf_button As New Button
Master_Panel.Controls.Add(ttf_button)
AddHandler ttf_button.Click, AddressOf TTF_BUTTON_CLICK
End Sub
Public Sub TTF_BUTTON_CLICK(sender As Object, e As EventArgs)
Dim str As String = sender.id
Dim panel As New Panel
panel.ID = "TTF_Panel"
panel.Width = 300
panel.Height = 300
Master_Panel.Controls.Add(panel)
panel.BackColor = Drawing.Color.Black
panel.Style.Add(HtmlTextWriterStyle.Position, "absolute")
panel.Style.Add(HtmlTextWriterStyle.Left, "200px")
panel.Style.Add(HtmlTextWriterStyle.Top, "100px")
panel.Style.Add(HtmlTextWriterStyle.ZIndex, "100")
Dim CL_Button As New Button
CL_Button.ID = "TTF_Close_" & Replace(str, "TTF_Button_", "")
panel.Controls.Add(CL_Button)
AddHandler CL_Button.Click, AddressOf TTF_Close_Button_Click
End Sub
Public Sub TTF_Close_Button_Click(sender As Object, e As EventArgs)
Dim ttf_tb As TextBox = Master_Panel.FindControl("ttf_tb")
ttf_tb.Text = "Test"
Dim panel As Panel = FindControl("TTF_Panel")
Master_Panel.Controls.Remove(panel)
End Sub
End Class
I think you need to re-create your controls in the Page_Init method. It's been a while since I've done web forms but I think it's something like:
When a user clicks the button a post back is fired. This re-creates a new instance of your class, creates the controls on the page, assigns any form values then calls your Page_Load event.
The problem is you are creating your controls too late, so the forms values are never assigned correctly.
You should create / recreate your dynamic controls in the Init event:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Controls_Load()
End Sub
This should allow you to maintain their state across PostBacks.
For more information about this topic, see the MSDN article on the ASP.NET Page Life Cycle.

VB.net/Asp.net loading a control programatically with a public property to be set

I have a vb.net asp application where I'm loading a control (from another control on a page). I want to set a variable between the two controls when it loads.
This is where I load the control:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
phCompanies.Controls.Add(AuditControl)
End Sub
On the control that's being loaded i've exposed the item i want to change as a property
Public Property resultType() As String
Get
Return m_resultType
End Get
Set(ByVal value As String)
m_resultType = value
End Set
End Property
Basically all it is doing is setting a parameter for my table adaptor
Public Sub Load_Data()
dtblog = New dsDECorp.ClientInfoDataTable
dtblog = tablog.GetAudits(m_resultType)
For Each rClient As dsDECorp.ClientInfoRow In dtblog
CID = rClient.ClientID
ClientName = rClient.CompanyName
Next
dtblog.Dispose()
End Sub
How do I pass the parameter through the property from the first control to the second when it loads?
Thanks
Tom
I'm a C# guy so forgive me if I make any syntax errors but the following should work for you:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
Dim AuditControlType As Type = AuditControl.GetType()
Dim AuditField As FieldInfo = AuditControlType.GetField("resultType")
AuditField.SetValue(AuditControlType, "Your Value")
phCompanies.Controls.Add(AuditControl)
End Sub

VB.NET CheckboxList doubles when button is clicked. Why?

Every time I click the Delete Button, the check marks double in quantity:
Public Class About
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
For count As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(count).Selected Then
File.Delete("C:\Images\" & CheckBoxList1.Items(count).ToString)
End If
Next
End Sub
End Class
The checkboxlist isn't refreshed so that the checkbox that I deleted is removed from the checkboxlist. How do I accomplish that? Thanks!
On a button click, Page_Load is ran again, so the code that adds the checkboxes is ran a second time.
Add a check for Page.IsPostBack, and only add the checkboxes if it is not a postback.
If Not Page.IsPostBack Then
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If
(I hope syntax is right... Not used to VB)
the whole content of the Page_Load event handler has to be executed only once in your case, so rewrite it like this:
If Not IsPostBack Then
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If

Resources