TreeView ondemand无法正常工作 [英] TreeView ondemand is not working
本文介绍了TreeView ondemand无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false"
CodeFile="KB.aspx.vb" Inherits="KB" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<style type="text/css">
.style1
{
height: 230px;
}
.style2
{
width: 157px;
}
.myclass { font-size: 1.17em;text-decoration: none;color: #333399; background-color:Blue;}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:ScriptManager ID="scriptAP" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upDemo" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<table>
<tr>
<td valign="top">
<asp:TreeView ID="LinksTreeView" Font-Names="Arial" ForeColor="Blue" EnableClientScript="false"
OnTreeNodePopulate="PopulateNode" runat="server">
<Nodes>
<asp:TreeNode Text="Form" SelectAction="Expand" PopulateOnDemand="true" />
</Nodes>
</asp:TreeView>
</td>
<td valign="top">
<asp:GridView ID="gvDemo" DataKeyNames="ID" HorizontalAlign="Center" runat="server"
AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" Font-Names="Verdana"
Font-Size="8pt" BorderWidth="0px" Width="700px" Visible="false">
<Columns>
<asp:BoundField HeaderText="" DataField="Desc" ControlStyle-Width="700" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CssClass="myclass" CommandName="KBEDIT" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"id")%>'
CausesValidation="false" />
<asp:LinkButton ID="btnAddPost" runat="server" Text="AddPost" CommandName="KBPOST" CssClass="myclass"
CommandArgument='<%# DataBinder.Eval(Container.DataItem,"id")%>' CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<%-- <HeaderStyle BackColor="#0681DF" ForeColor="White" />--%>
<%-- <AlternatingRowStyle BackColor="#F5F4F0" />--%>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<asp:Panel ID="plnAddPost" runat="server">
<table>
<tr>
<td>
<asp:Label ID="lblSelectedRow" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlcategory" runat="server" AutoPostBack="true">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlsubcategory" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtAddPost" runat="server" TextMode="MultiLine" Width="500" Height="200"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtAddPost"
runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="btnAddPost" runat="server" Text="AddPost" CssClass="myclass" />
</td>
<td>
<asp:LinkButton ID="btnCancle" runat="server" CssClass="myclass" CausesValidation="false" >Cancle</asp:LinkButton>
</td>
</tr>
</table>
</asp:Panel>
</td>
</tr>
</table>
<asp:HiddenField ID="hidnodeid" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvDemo" EventName="PageIndexChanging" />
<asp:AsyncPostBackTrigger ControlID="gvDemo" EventName="RowCommand" />
<asp:AsyncPostBackTrigger ControlID="gvDemo" EventName="RowDataBound" />
<asp:AsyncPostBackTrigger ControlID="btnAddPost" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnCancle" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ddlcategory" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlsubcategory" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
================================================== ======================
=========================================================================
Imports System
Imports System.Data
Imports System.Data.OleDb
Partial Class KB
Inherits System.Web.UI.Page
Dim objKBlist As New List(Of Knowledge)
Dim cnn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb")
Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
' Call the appropriate method to populate a node at a particular level.
Select Case e.Node.Depth
Case 0
' Populate the first-level nodes.
PopulateCategories(e.Node)
Case 1
' Populate the second-level nodes.
PopulateProducts(e.Node)
Case Else
' Do nothing.
End Select
End Sub
Sub PopulateCategories(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("select ID,name from MasterKB")
' Create the second-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node. Notice that the CategoryId is stored in the Value property
' of the node. This will make querying for items in a specific category easier when
' the third-level nodes are created.
Dim NewNode As TreeNode = New TreeNode(row("name").ToString(), row("ID").ToString())
' Set the PopulateOnDemand property to true so that the child nodes can be
' dynamically populated.
NewNode.PopulateOnDemand = True
' Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
Next
End If
End Sub
Sub PopulateProducts(ByVal node As TreeNode)
' Query for the products of the current category. These are the values
' for the third-level nodes.
Dim ResultSet As DataSet = RunQuery("select ID,name from MasterCT where KBID=" & node.Value)
' Create the third-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node.
Dim NewNode As TreeNode = New TreeNode(row("name").ToString())
' Set the PopulateOnDemand property to false because these are leaf nodes and
' do not need to be populated.
NewNode.PopulateOnDemand = False
' Set additional properties for the node.
' NewNode.SelectAction = TreeNodeSelectAction.None
NewNode.NavigateUrl = "~/KB.aspx?node=" & row("ID").ToString()
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
Next
End If
End Sub
Function RunQuery(ByVal QueryString As String) As DataSet
' Declare the connection string. This example uses Microsoft SQL Server and connects to the
' Northwind sample database.
'Dim ConnectionString As String = "server=localhost;database=NorthWind;Integrated Security=SSPI"
'Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
'Dim DBAdapter As SqlDataAdapter
Dim ResultsDataSet As DataSet = New DataSet
' Dim cmd As New OleDbCommand
Dim DBAdapter As New OleDbDataAdapter
'Dim dtTable As New DataTable
'Dim query As String
'query = "select ID,name from MasterCT where KBID=" & ID
'cmd = New OleDb.OleDbCommand(query, cnn)
cnn.Open()
'Dim myReader As OleDbDataReader = cmd.ExecuteReader()
'While myReader.Read()
' If Not IsDBNull(myReader(0)) Then
' ddlsubcategory.Items.Add(New ListItem With {.Value = myReader(0), .Text = myReader(1)})
' End If
'End While
'cnn.Close()
Try
' Run the query and create a DataSet.
DBAdapter = New OleDbDataAdapter(QueryString, cnn)
DBAdapter.Fill(ResultsDataSet)
' Close the database connection.
cnn.Close()
Catch ex As Exception
' Close the database connection if it is still open.
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
' Message.Text = "Unable to connect to the database."
End Try
Return ResultsDataSet
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("node") IsNot Nothing Then
hidnodeid.Value = Request.QueryString("node")
End If
If Not Page.IsPostBack Then
fillGrid()
fillcategory()
If Request.QueryString("id") IsNot Nothing Then
LinksTreeView.Visible = False
gvDemo.Visible = False
plnAddPost.Visible = True
SelectedRowFrom(Request.QueryString("id"))
Else
LinksTreeView.Visible = True
gvDemo.Visible = True
plnAddPost.Visible = False
End If
End If
End Sub
Sub fillcategory()
ddlcategory.Items.Clear()
Dim cmd As New OleDbCommand
Dim dtTable As New DataTable
Dim query As String
query = "select ID,name from MasterKB"
cmd = New OleDb.OleDbCommand(query, cnn)
cnn.Open()
Dim myReader As OleDbDataReader = cmd.ExecuteReader()
While myReader.Read()
If Not IsDBNull(myReader(0)) Then
ddlcategory.Items.Add(New ListItem With {.Value = myReader(0), .Text = myReader(1)})
End If
End While
cnn.Close()
End Sub
Sub fillsubcategory(ByVal id As Integer)
ddlsubcategory.Items.Clear()
Dim cmd As New OleDbCommand
Dim dtTable As New DataTable
Dim query As String
query = "select ID,name from MasterCT where KBID=" & id
cmd = New OleDb.OleDbCommand(query, cnn)
cnn.Open()
Dim myReader As OleDbDataReader = cmd.ExecuteReader()
While myReader.Read()
If Not IsDBNull(myReader(0)) Then
ddlsubcategory.Items.Add(New ListItem With {.Value = myReader(0), .Text = myReader(1)})
End If
End While
cnn.Close()
End Sub
Sub SelectedRowFrom(ByVal id As Integer)
Dim cmd As New OleDbCommand
Dim dtTable As New DataTable
Dim query As String
query = "select KB.ID,MasterCT.KBID,KB.CTID,KB.Desc from KB inner join MasterCT on MasterCT.id =KB.CTID where KB.id=" & id
cmd = New OleDb.OleDbCommand(query, cnn)
cnn.Open()
Dim myReader As OleDbDataReader = cmd.ExecuteReader()
Dim objknowledge As New Knowledge
While myReader.Read()
If Not IsDBNull(myReader(0)) Then
objknowledge.id = myReader(0)
End If
If Not IsDBNull(myReader(1)) Then
objknowledge.kb_id = myReader(1)
End If
If Not IsDBNull(myReader(2)) Then
objknowledge.cb_id = myReader(2)
End If
If Not IsDBNull(myReader(3)) Then
objknowledge.Desc = myReader(3)
End If
End While
cnn.Close()
With objknowledge
ddlcategory.SelectedItem.Value = .kb_id
fillsubcategory(ddlcategory.SelectedItem.Value)
ddlsubcategory.SelectedItem.Value = .cb_id
lblSelectedRow.Text = .Desc
End With
End Sub
Sub fillGrid()
Dim cmd As New OleDbCommand
Dim dtTable As New DataTable
Dim query As String
If hidnodeid.Value <> "" Then
query = "select ID,CTID,Desc from KB where CTID=" & hidnodeid.Value
Else
query = "select ID,CTID,Desc from KB"
End If
cmd = New OleDb.OleDbCommand(query, cnn)
cnn.Open()
Dim myReader As OleDbDataReader = cmd.ExecuteReader()
While myReader.Read()
Dim objKB As New Knowledge
Dim objKBforPostInformation As New Knowledge
If Not IsDBNull(myReader(0)) Then
objKB.id = myReader(0)
objKBforPostInformation.id = myReader(0)
End If
If Not IsDBNull(myReader(1)) Then
objKB.cb_id = myReader(1)
objKBforPostInformation.cb_id = myReader(1)
End If
If Not IsDBNull(myReader(2)) Then
objKB.Desc = myReader(2)
objKBforPostInformation.Desc = ""
End If
objKBlist.Add(objKB)
objKBlist.Add(objKBforPostInformation)
End While
cnn.Close()
' ViewState.Add("ViewKBlist", objKBlist)
gvDemo.DataSource = objKBlist
gvDemo.DataBind()
End Sub
Protected Sub gvDemo_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvDemo.PageIndexChanging
gvDemo.PageIndex = e.NewPageIndex
fillGrid()
End Sub
Protected Sub gvDemo_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvDemo.RowCommand
Select Case e.CommandName
Case "KBEDIT"
Response.Write("select")
Case "KBPOST"
Response.Redirect("~/KB.aspx?id=" & e.CommandArgument & "&op=AddPost")
End Select
End Sub
Protected Sub gvDemo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDemo.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btnEditButton As LinkButton = CType(e.Row.FindControl("btnEdit"), LinkButton)
Dim btnAddPostButton As LinkButton = CType(e.Row.FindControl("btnAddPost"), LinkButton)
If e.Row.Cells(0).Text = "" Or e.Row.Cells(0).Text = " " Then
btnEditButton.Visible = False
btnAddPostButton.Visible = True
Else
' e.Row.Cells(0).Text = HttpUtility.HtmlDecode(e.Row.Cells(0).Text.Substring(0, 1000)) & "............"
e.Row.Cells(0).Text = HttpUtility.HtmlDecode(e.Row.Cells(0).Text)
If Session("userDetails") IsNot Nothing Then
btnEditButton.Visible = True
Else
btnEditButton.Visible = False
End If
btnAddPostButton.Visible = False
End If
End If
End Sub
Protected Sub btnCancle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancle.Click
Response.Redirect("~/KB.aspx")
End Sub
Protected Sub ddlcategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlcategory.SelectedIndexChanged
fillsubcategory(ddlcategory.SelectedItem.Value)
End Sub
Protected Sub btnAddPost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddPost.Click
If txtAddPost.Text = "" Then
RequiredFieldValidator1.IsValid = False
Else
Dim cmd As New OleDbCommand
Dim dtTable As New DataTable
Dim query As String
query = "INSERT into KB (CTID,[Desc]) Values(" & ddlsubcategory.SelectedItem.Value & ",'" & txtAddPost.Text & "');"
cmd = New OleDb.OleDbCommand(query, cnn)
cmd.CommandType = CommandType.Text
cmd.CommandText = query
cnn.Open()
Using cnn
cmd.ExecuteNonQuery()
End Using
cnn.Close()
Response.Redirect("~/KB.aspx")
End If
End Sub
End Class
推荐答案
In spite of reading all this coding carefully, I would suggest you to go through the following links below and try finding your answer:
Building a TreeView on Demand Using AJAX[^]
MSDN : TreeNode.PopulateOnDemand Property[^]
And read the similar threads below:
TreeView.TreeNodePopulate Event [^]
ASP.NET TreeView and loading data on demand[^]
--Amit
In spite of reading all this coding carefully, I would suggest you to go through the following links below and try finding your answer:
Building a TreeView on Demand Using AJAX[^]
MSDN : TreeNode.PopulateOnDemand Property[^]
And read the similar threads below:
TreeView.TreeNodePopulate Event [^]
ASP.NET TreeView and loading data on demand[^]
--Amit
这篇关于TreeView ondemand无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文