Imports System
Imports System.Net
Imports System.IO
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Web.UI.Control
Imports System.Configuration
Imports System.Collections
Imports System.Web.Security
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.VisualBasic
Partial Class Permission
Inherits System.Web.UI.Page
Dim sql As String
Dim cmd As SqlCommand
Dim execsql As New ClsExecDB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GentHTML()
End Sub
Public Sub GentHTML()
Dim html As New StringBuilder
html.AppendLine("<asp:TreeView ID=""TreeView1"" runat=""server"" ShowLines =""True"">")
html.AppendLine("<Nodes >")
html.AppendLine("<asp:TreeNode Text=""เว็บบอร์ด"" Value=""เว็บบอร์ด"">")
html.AppendLine("<asp:TreeNode NavigateUrl=""http://www.pantip.com"" ShowCheckBox=""True"" Text=""pantip"" Value=""1"" >")
html.AppendLine("</asp:TreeNode>")
html.AppendLine("</asp:TreeNode>")
html.AppendLine("</Nodes>")
html.AppendLine("</asp:TreeView><br />")
html.AppendLine("<asp:Button ID = ""cmdChrck"" runat =""server"" Text = ""Check"" />")
Response.Write(html.ToString)
End Sub
End Class
ใน GentHTML() ลองเปลี่ยนไปใช้ HTML Tag แทน ASP.NET Control แล้วใช้ Label รับ
Code (VB.NET)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = GentHTML()
End Sub
Public Function GentHTML()
Return "<ul><li>เว็บบอร์ด<ul><li><input type='checkbox'><a href='http://www.pantip.com'>Pantip</a></li></ul></li></ul>"
End Function
แต่ถ้ายืนยันจะใช้ ASP.NET Control มันต้องแอดลงไปประมานลิ้งก์ข้างล่าง
http://msdn.microsoft.com/en-us/library/kyt0fzt1(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
คิดว่าที่ทำแบบนี้เพราะจะเก็บแท็กไว้ในฐานข้อมูลแล้วคิวรี่เรียกออกใช่ป่าวครับ งั้นลองเปลี่ยนไปใช้ HTML แทนดูเน้อะ
Imports System
Imports System.Net
Imports System.IO
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Web.UI.Control
Imports System.Configuration
Imports System.Collections
Imports System.Web.Security
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.VisualBasic
Partial Class Permission
Inherits System.Web.UI.Page
Dim sql As String
Dim cmd As SqlCommand
Dim cmd2 As SqlCommand
Dim link As String = ""
Dim lic As String
Dim str As String
Dim execsql As New ClsExecDB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("license") = "all"
If IsPostBack = False Then
PopulateRootLevel()
Dim toolbarMasterPage As MasterPage = CType(Me.Master, MasterPage)
End If
End Sub
Private Sub License()
lic = Session("license")
Dim myArr As Array = Split(lic, ",")
Dim i As Integer
For i = 0 To UBound(myArr)
If str = "" Then
str = myArr(i)
ElseIf str <> "" Then
str = str & " OR " & " sc.id LIKE " & myArr(i)
End If
Next
End Sub
Private Sub PopulateRootLevel()
If Session("license").ToString = "all" Then
sql = "select id,parentid,title,(select count(*) FROM T_MENU " _
& "WHERE parentid=sc.id) childnodecount FROM T_MENU sc where parentID IS NULL"
ElseIf Session("license").ToString = "lock" Then
Response.Cookies("_USER_ID").Value = Nothing
Response.Cookies("_USER_NAME").Value = Nothing
Response.Cookies("_USER_FIRST_NAME").Value = Nothing
Response.Cookies("_DEPT_ID").Value = Nothing
Response.Cookies("_USER_TYPE").Value = Nothing
Response.Cookies("_COMPANY").Value = Nothing
Session("license") = Nothing
Session("user") = Nothing
Exit Sub
Else
License()
str = " WHERE (sc.id LIKE " & str & ")"
sql = "select id,parentid,title,(select count(*) FROM T_MENU "
sql = sql & "WHERE parentid=sc.id) childnodecount FROM T_MENU sc " & str
sql = sql & "AND parentID IS NULL"
End If
cmd = New SqlCommand(sql)
Dim ds1 As DataSet = New ClsGetDataInfo()._GetDataset4(cmd, "TbMenu")
PopulateNodes(ds1.Tables("TbMenu"), TreeView1.Nodes)
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)
If Session("license").ToString = "all" Then
sql = "select id,parentid,prefix,title,proname,(select count(*) FROM T_MENU " _
& "WHERE parentid=sc.id) childnodecount FROM T_MENU sc where parentID=@parentID order by seq"
Else
License()
str = " WHERE (sc.id LIKE " & str & ")"
sql = "select id,parentid,prefix,title,proname,(select count(*) FROM T_MENU "
sql = sql & "WHERE parentid=sc.id) childnodecount FROM T_MENU sc" & str
sql = sql & "AND parentID=@parentID order by seq "
End If
cmd = New SqlCommand(sql)
cmd.Parameters.Add("@parentID", SqlDbType.SmallInt).Value = parentid
Dim ds2 As DataSet = New ClsGetDataInfo()._GetDataset4(cmd, "TbMenuselect")
PopulateNodes(ds2.Tables("TbMenuselect"), parentNode.ChildNodes)
End Sub
Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)
For Each dr As DataRow In dt.Rows
Dim tn As New TreeNode()
tn.Text = dr("title").ToString()
tn.Value = dr("id").ToString()
If dr("parentid").ToString() <> "" Then
tn.ShowCheckBox = True
End If
nodes.Add(tn)
'If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
Next
End Sub
Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
PopulateSubLevel(CInt(e.Node.Value), e.Node)
End Sub
Protected Sub cmdChrck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdChrck.Click
CheckTree()
End Sub
Public Sub CheckTree()
For Each node As TreeNode In TreeView1.CheckedNodes
Response.Write("value :" + node.Value)
Next
End Sub
'Public Sub GentHTML()
' Dim html As New StringBuilder
' html.AppendLine("<asp:TreeView ID=""TreeView1"" runat=""server"" ShowLines =""True"">")
' html.AppendLine("<Nodes >")
' html.AppendLine("<asp:TreeNode Text=""เว็บบอร์ด"" Value=""เว็บบอร์ด"">")
' html.AppendLine("<asp:TreeNode NavigateUrl=""http://www.pantip.com"" ShowCheckBox=""True"" Text=""pantip"" Value=""1"" >")
' html.AppendLine("</asp:TreeNode>")
' html.AppendLine("</asp:TreeNode>")
' html.AppendLine("</Nodes>")
' html.AppendLine("</asp:TreeView><br />")
' html.AppendLine("<asp:Button ID = ""cmdChrck"" runat =""server"" Text = ""Check"" />")
' 'Response.Write(html.ToString)
'End Sub
End Class