日期:2014-05-18  浏览次数:20983 次

html控件select 如何在.cs文件中对其绑定数据呢?
<select id="P_Id" runat="server"></select>
这样的一个下拉框 我以前是用DropDownList的,所以在.cs文件中有一个方法来绑定的
C# code
    private void BindProductClassData()
    {
        BindProductJgData();BindProductHdData();BindProductBbData();
        DataTable dt = SQL.DT("select * from vProductClass order by RootID,Orders");
        P_Id.Items.Clear();
        DataRow[] drs = dt.Select("ParentId=0");

        foreach (DataRow dr in drs)
        {
            string classid = dr["classid"].ToString();
            string classname = dr["classname"].ToString();
            classname = "├─" + classname;

            P_Id.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank = "│ ";
            BindDrpNode(sonparentid, dt, blank);
        }
        P_Id.DataBind();
    }
    private void BindDrpNode(int parentid, DataTable dt, string blank)
    {
        DataRow[] drs = dt.Select("ParentID= " + parentid);
        foreach (DataRow dr in drs)
        {
            string classid = dr["classid"].ToString();
            string classname = dr["classname"].ToString();

            classname = blank + "├─" + classname;
            P_Id.Items.Add(new ListItem(classname, classid));

            int sonparentid = int.Parse(classid);
            string blank2 = blank + "├─";

            BindDrpNode(sonparentid, dt, blank2);
        }
    }


现如今,这个方法如何应用到select标签上啊?

------解决方案--------------------
C# code
protected string BindDrpNode()
    {
        。。。。
        if (reader != null)
        {
            while (reader.Read())
            {
                str += "<option value=" + reader["classid"] + ">" + reader["classname"] + "</option>";
            }
            reader.Close();
            reader.Dispose();
        }
        return str;
    }

调用:
 <select id="Select1" name="Select1">
            <%=BindDrpNode()%>
        </select>