telerik radtreeview显示连接表中的数据会给出错误这些列目前没有唯一值

时间:2016-03-21 13:12:39

标签: sql asp.net sql-server telerik radgridview

我想显示名为" OpenCourses"和" OpenCoursesChapters"。

Opencourses表:

enter image description here

OpenCoursesChapters表:

enter image description here

我想在radtreeview上将coursename显示为父节点,将chaptername显示为子节点:

 <div class="demo-container size-thin">

    <telerik:RadTreeView Height="300px" runat="server" ID="RadPanelBar1" DataTextField="name" DataFieldID="id"
     DataFieldParentID="parentid" DataSourceID="SqlDataSource1" >

    </telerik:RadTreeView>
</div>

   <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:ConStr %>"
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT courseid as id, NULL as parentid, coursename as name from opencourses union select chapterid as id, courseid as parentid, chaptername as name from opencourseschapters"></asp:SqlDataSource>

在运行项目时,我收到错误

  

这些列目前不具备唯一值。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先你的选择命令必须是:

    "SELECT chapterid AS ID, chaptername AS Name,
courseid + 1000 AS parentID FROM OpenCoursesChapters p
UNION ALL
SELECT courseid + 1000 AS ID, coursename AS Name, null AS parentID
FROM Opencourses"

除非您可以像这样使用asp TreeView:

            <asp:TreeView ID="RadPanelBar1"
                 runat="server"
                ShowCheckBoxes="All" 
                PopulateNodesFromClient="False"
                 onclick="client_OnTreeNodeChecked();" >
            </asp:TreeView>

并且在java脚本中(可能在页脚中)使用此函数来自动检查子节点:

 function client_OnTreeNodeChecked(evt)

 {

     var obj;

     if(window.event)    obj = window.event.srcElement;

     else                obj = (evt ? evt : (window.event ? window.event : null)).target;
     var treeNodeFound = false;
     var checkedState;
     if (obj.tagName == "INPUT" && obj.type == "checkbox" ) 
     {
         checkedState = obj.checked;
         do
         {
             obj = obj.parentNode;
         } 
         while (obj.tagName != "TABLE")

         var parentTreeLevel = obj.rows[0].cells.length;

         //get the current node's parent node.
         var tables = obj.parentNode.getElementsByTagName("TABLE");
         var numTables = tables.length;

         if (numTables >= 1)
         {
             for (i=0; i < numTables; i++)
             {
                 if (tables[i] == obj)
                 {
                     treeNodeFound = true;
                     i++;
                     if (i == numTables) return;
                 }

                 if (treeNodeFound == true){
                     var childTreeLevel = tables[i].rows[0].cells.length;
                     if (childTreeLevel > parentTreeLevel){
                         var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                         var inputs = cell.getElementsByTagName("INPUT");
                         inputs[0].checked = checkedState;
                     }
                     else  return;
                 }
             }
         }
     }

 } 

所以,在page_load()服务器端代码后面调用这个函数:

 public void tree_fill_general(TreeView tree_name, string parent_query, string child_query)
    {
        try
        {
            if (your connection == false)
            {
               your connection.Open();
            }
            tree_name.Nodes.Clear();
            string q = parent_query;
            SqlDataAdapter sda = new SqlDataAdapter(q, your connection);
            DataSet datasetParent = new DataSet();
            sda.Fill(datasetParent);
            for (int i = 0; i <= datasetParent.Tables[0].Rows.Count - 1; i++)
            {
                TreeNode main = new TreeNode();
                main.Text = datasetParent.Tables[0].Rows[i]["name"].ToString();
                main.Value = datasetParent.Tables[0].Rows[i]["code"].ToString();

                string q1 = child_query + main.Value;
                SqlDataAdapter sda1 = new SqlDataAdapter(q1, your connection);
                DataSet datasetChild = new DataSet();
                sda1.Fill(datasetChild);
                for (int j = 0; j <= datasetChild.Tables[0].Rows.Count - 1; j++)
                {
                    TreeNode mainchild = new TreeNode();
                    mainchild.Text = datasetChild.Tables[0].Rows[j]["name"].ToString();
                    mainchild.Value = datasetChild.Tables[0].Rows[j]["code"].ToString();
                    main.ChildNodes.Add(mainchild);
                }
                tree_name.Nodes.Add(main);
            }

        }
        catch (Exception eee)
        {
            Utility.SetMessage(Page, "error", eee.Message, enumIcon.Error);
        }
    }

和parent_query:

"select courseid as code,coursename as name from Opencourses  order by sec,courseid";

final,child_query:

"select chapterid as code,chaptername as name opencourseschapters where courseid  =";