Reputation: 4864
Using the following web.sitemap as an example, we would like to create an event handler to catch when the user clicks on the "Teachers" link of an ASP.Net TreeView. This happens to be a parent node.
<siteMapNode title="Teachers" url="~/DefaultTeachers.aspx" >
<siteMapNode url="~/Teachers.aspx" title="Teachers" description="Maintain details of each Teacher." />
<siteMapNode url="~/TeacherSchedules.aspx" title="Teacher Schedules" description="Maintain teacher schedules." />
<siteMapNode url="~/TeacherEmailNotices.aspx" title="Email Notices To Teachers" description="Email notices to teachers." />
</siteMapNode>
Can you tell me what event to place in the markup of the ASP.Net TreeView maybe something like this?
<asp:TreeView
id="TreeViewMain"
runat="server"
ExpandDepth="0"
OnUserClickedTheLink="TreeViewMain_UserClickedTheLink"
DataSourceID="KnowledgeAcademySiteMap">
<RootNodeStyle ImageUrl="/Images/book.png" />
<ParentNodeStyle ImageUrl="/Images/book.png" />
<LeafNodeStyle ImageUrl="/Images/book.png" />
</asp:TreeView>
Also in the code-behind file we would like to place e.Node or something like that in an "If" statement to test what node was actually clicked:
If e.Node.Text = "Teachers" Then
' We will close all other nodes and open up the 3 leaf nodes under "Teachers" here.
'----------------------------------------------------------------------------------
End If
Upvotes: 0
Views: 1836
Reputation: 17039
The problem is that the TreeView can perform only one of two functions, which are:
Since you're setting the tree view data source to a site map it will perform the Navigate function and will not fire any post back event. It will simply say - oh you cliked on the Teachers node I'm just going to take you to the Teachers page and nothing more.
There's a number of things you can try:
Sample xml file:
<menu name="menu">
<teachers name="teachers">
<teacher name="teacher1" />
<teacher name="teacher2" />
<teacher name="teacher3" />
</teachers>
</menu>
Code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim ds As XmlDataSource = New XmlDataSource
ds.DataFile = Server.MapPath("~/App_Data/menu.xml")
TreeViewMain.DataSource = ds
TreeViewMain.DataBind()
End If
End Sub
Protected Sub SelectionChanged(sender As Object, e As EventArgs) Handles TreeViewMain.SelectedNodeChanged
Dim selected As String = TreeViewMain.SelectedValue
If selected.Equals("teachers") Then
'Do any required processing and then manually redirect to the Teachers Page
End If
End Sub
The problem with this approach of course is that you'll need to check every clicked item and manually redirect the user to a particular page which won't be acceptable if you have many tree view items to evaluate
Upvotes: 1