RadMenu dynamic databinding doesnt show

449 Views Asked by At

I'm using SQL server 2012, and VS2013. I want to design a dynamic Radmenu(Telerik) which databind with sql server. But it doesnt show at runtime, and no error occured.

Here is the code.

     <telerik:RadScriptManager runat="server" ></telerik:RadScriptManager>
        <telerik:RadMenu ID="RadMenu1" runat="server" style="top: 0px; left: 0px; width: 800px;height:auto"
             Skin="Metro">
        </telerik:RadMenu>

code-behind

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myCnn = new SqlConnection(myCnnstring);
            myCnn.Open();
            mysql = "select * from SYS_Menu";
            mycmd = new SqlCommand(mysql, myCnn);
            myda = new SqlDataAdapter(mycmd);
            myds = new DataSet();
            myda.Fill(myds);
            mydt = new DataTable();
            mydt = myds.Tables[0];

            RadMenu1.DataSource = mydt;
            RadMenu1.DataFieldID = "PK_ID";
            RadMenu1.DataFieldParentID = "ParentID";
            //Set Text, Value, and NavigateUrl:    
            RadMenu1.DataTextField = "TextField";
            RadMenu1.DataValueField = "PK_ID";
            RadMenu1.DataNavigateUrlField = "URL";
            RadMenu1.MaxDataBindDepth = -1;
            RadMenu1.DataBind();
            myCnn.Close();
        } 
    }

Table Sys_menu

PK_ID|ParentID|TextField|ValueField|URL
1|1|Home|Home|~/NewVersion/test2.aspx
10|9|SingleBuilding|SingleBuilding|~/NewVersion/test2.aspx
11|9|GroupBuilding|GroupBuilding|~/NewVersion/test2.aspx
2|1|Land|Land|~/NewVersion/test2.aspx
3|2|LandBlock|LandBlock|~/NewVersion/test2.aspx
4|2|LandCost|LandCost|~/NewVersion/test2.aspx
5|3|Voucher|Voucher|~/NewVersion/test2.aspx
6|3|Query|Query|~/NewVersion/test2.aspx
7|4|Voucher|Voucher|~/NewVersion/test2.aspx
8|4|Query|Query|~/NewVersion/test2.aspx
9|1|BuildingInfo|BuildingInfo|~/NewVersion/test2.aspx

another problem: I read the instruction of Telerik that the root of ParentID of hierarchical item should be null, but it will cause an error.

what should I do ? Thanks in advance.

2

There are 2 best solutions below

0
Veselin Vasilev On

It is probably due to the parent nodes not having NULL in their ParentId column.

Can you modify the data (setting the nulls) before you bind the menu to it?

0
Babai On

make ParentID column value of root element is null.

1|1|Home|Home|~/NewVersion/test2.aspx