实现树形下拉列表 GridView数据绑定

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AxDBClass;

public partial class Manage_UserPower : AxPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        checkSession(this, "EEABAB");
        if (!checkFunctionPower("EEABAB02"))
        {
            btnAdd.Visible = false;
            this.userList.Columns[4].Visible = false;
        }

        if (!Page.IsPostBack)
        {
            bindUserList();

            //添加树形下拉列表
            DataTable datatable = this.GetDataTable();
            DataRow[] row = datatable.Select("ParentNo='0'");
            //添加根目录
            ListItem li = new ListItem(row[0]["Name"].ToString());
            depList.Items.Add(li);
            addOtherDll("", Convert.ToInt32(row[0]["No"]), datatable, 1);

          

           //
    
        }
    }

    //绑定用户列表
    private void bindUserList()
    {
        this.userList.DataSource = null;
        this.userList.DataBind();
        SystemManage user = new SystemManage();
        funResult.RetStr = user.userList(out axDataXmlOut);
        if (funResult.Code == 0)
        {
            AxDataSet axds = new AxDataSet(axDataXmlOut);
            userList.DataSource = axds.table;
            userList.DataBind();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bindUserList();
    }

   
    protected void userList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SystemManage user = new SystemManage();
        if (e.CommandName == "DeleteUser")
        {
            funResult.RetStr = user.deleteUser(e.CommandArgument.ToString().Trim());
            if (funResult.Code == 0)
            {
                bindUserList();
            }
            else
            {
                Response.Write("<script>alert('" + funResult.Msg + "');</script>");
            }
           
        }
    }

 

    /**/
    /// <summary>
    /// 添加其他节点
    /// </summary>
    /// <param name="Pading">空格</param>
    /// <param name="DirId">父路径ID</param>
    /// <param name="datatable">返回的datatable</param>
    /// <param name="deep">树形的深度</param>
    private void addOtherDll(string Pading, int DirId, DataTable datatable, int deep)
    {
        DataRow[] rowlist = datatable.Select("ParentNo='" + DirId + "'","Name");
        foreach (DataRow row in rowlist)
        {
            string strPading = "";
            for (int j = 0; j < deep; j++)
            {
                strPading += " ";         //用全角的空格
            }
            //添加节点
            ListItem li = new ListItem(strPading + "|--" + row["Name"].ToString());
            depList.Items.Add(li);
            //递归调用addOtherDll函数,在函数中把deep加1
            addOtherDll(strPading, Convert.ToInt32(row["No"]), datatable, deep + 1);
        }
    }


    protected void DepList_SelectedIndexChanged(Object sender, EventArgs e)
    {
        string tmp = depList.SelectedItem.Text;
        int index = tmp.LastIndexOf("-");
        if (index > 0)
        {
            tmp = tmp.Substring(index + 1);

            SystemManage user = new SystemManage();
            funResult.RetStr = user.userList(tmp, out axDataXmlOut);
            if (funResult.Code == 0)
            {
                AxDataSet axds = new AxDataSet(axDataXmlOut);

                userList.DataSource = axds.table;
                userList.DataBind();

            }
            else
            {
                userList.DataSource = null;
                userList.DataBind();
            }
               
        
        }
        else
        {
            SystemManage user = new SystemManage();
            funResult.RetStr = user.userList( out axDataXmlOut);
            if (funResult.Code == 0)
            {
                AxDataSet axds = new AxDataSet(axDataXmlOut);

                userList.DataSource = axds.table;
                userList.DataBind();

            }
            else
            {
                userList.DataSource = null;
                userList.DataBind();
            }

 
        }
    }

 

 

 

    private DataTable GetDataTable()
    {
       
        DBClass db = new DBClass();
        DataSet datset = new DataSet();
        datset = db.getDataSetBySql("select * from UserDept_T");
        db.close();
        return datset.Tables[0];
    }

 

 

 

    //DataTable tb = new DataTable();
    //private void BindItemTree()
    //{
       
       
    //    depList.Items.Clear();
    //    SystemManage user = new SystemManage();
    //    funResult.RetStr = user.userList( out axDataXmlOut);
    //    if(funResult .Code ==0)
    //    {
    //        AxDataSet axds = new AxDataSet (axDataXmlOut );
    //        tb = axds.table;
    //        string parenthead = "┝";
    //        BindItemsList(parenthead ,"0");
    //        try { depList.SelectedValue = Session["item"].ToString(); }
    //        catch { }

    //    }
    //}

    //private void BindItemsList(string parenthead, string parentid)
    //{
    //    DataRow[] drs = tb.Select("DeptId='" + parentid + "'");
    //    //DataRow[] drs = tb.Select("");
    //    for (int i = 0; i < drs.Length; i++)
    //    {
    //        depList.Items.Add(new ListItem (parenthead + drs[i]["DepartName"].ToString (),drs[i]["DeptId"].ToString ()));
    //        string treehead = parenthead + "┝";
    //        BindItemsList(treehead, drs[i]["DeptId"].ToString());
    //    }

    //}

 

 

 

    绑定栏目树
    //protected void BindItemTree()
    //{
    //    drpItems.Items.Clear();
    //    NewsManage user = new NewsManage();
    //    funResult.RetStr = user.ReadItemContentByUser(userId, int.Parse(drpWebSite.SelectedValue), out axDataXmlOut);
    //    if (funResult.Code == 0)
    //    {
    //        AxDataSet axds = new AxDataSet(axDataXmlOut);
    //        tb = axds.table;
    //        string parenthead = "┝";
    //        BindItemsList(parenthead, "0");
    //        try { drpItems.SelectedValue = Session["item"].ToString(); }
    //        catch { }
    //    }
    //}

    列出每一个子树
    //protected void BindItemsList(string parenthead, string parentid)
    //{
    //    DataRow[] drs = tb.Select("parentid='" + parentid + "'", "orderno");
    //    for (int i = 0; i < drs.Length; i++)
    //    {
    //        drpItems.Items.Add(new ListItem(parenthead + drs[i]["itemname"].ToString(), drs[i]["itemid"].ToString()));
    //        string treehead = parenthead + "┝";
    //        BindItemsList(treehead, drs[i]["itemid"].ToString());
    //    }
    //}

 

    //  protected void Deplist_SelectedIndexChanged(Object sender, EventArgs e)
    // {

    //this.userList.DataSource = null;
    //this.userList.DataBind();
    //SystemManage user = new SystemManage();
    //funResult.RetStr = user.userList(out axDataXmlOut);
    //if (funResult.Code == 0)
    //{
    //    AxDataSet axds = new AxDataSet(axDataXmlOut );
    //    string tmp=  depList.SelectedItem.Text;
    //    string filter = "DepartName = '"+ tmp +"'";//

    //    DataTable dt = new DataTable();
    //    dt = axds.table.Clone();
    //    DataRow[] foundRows = axds.table.Select (filter );
    //    for (int i = 0; i < foundRows.Length; i++)
    //        dt.ImportRow(foundRows [i]);

    //    userList.DataSource = dt;

    //    userList.DataBind();

    //}


    //  }
}

 

[WebMethod(Description = "后台管理用户列表。axDataXmlOut:返回的dataset字符串")]
    public string userList(out string axDataXmlOut)
    {
        axDataXmlOut = "";
        DBClass dbc = new DBClass();
        string sqlstr = "select *,userTypeName = dbo.GetBasicValueById_F(userType),DepartName=(select Name From UserDept_T where No=userTable_T.DeptId) From usertable_T where userName<>'admin' order by username ";
        AxDataSet axds = dbc.getAxDataSetBySql(sqlstr);
        if (axds.read())
        {
            axDataXmlOut = axds.toXML();
            funResult.Msg = "读取用户列表成功";
            funResult.Code = 0;
        }
        else
        {
            funResult.Code = 10140000;
            funResult.Msg = "读取用户列表失败";
        }
        dbc.close();
        return funResult.RetStr;
    }

    //根据机构名称查询用户列表
    public string userList(string departName, out string axDataXmlOut)
    {
        axDataXmlOut = "";
        DBClass dbc = new DBClass();
        string sqlstr = "select *,userTypeName = dbo.GetBasicValueById_F(userType),DepartName =(select Name From UserDept_T where No=userTable_T.DeptId ) From usertable_T where userName<>'admin' and DeptId= (select No from UserDept_T where Name='" + departName + "') order by username"; 
        AxDataSet axds = dbc.getAxDataSetBySql(sqlstr );
        if (axds.read())
        {
            axDataXmlOut = axds.toXML();
            funResult.Msg = "读取用户列表成功";
            funResult.Code = 0;
        }
        else
        {
            funResult.Code = 10140000;
            funResult.Msg = "读取用户列表失败";
        }
        dbc.close();
        return funResult.RetStr;

    }

 

 

#region 类UAPCallResult,通用的语句操作是否成功判断
/// <summary>
/// 将返回代码和提示信息组合成字符串
/// </summary>
public class UAPCallResult
{
    private int _code = 0;
    private string _msg = "";
    private string _retstr = "";

    public UAPCallResult()
    {
        _retstr = "0|";
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="retStr">输入结果串Code+char(18)+Msg</param>
    public UAPCallResult(string retStr)
    {
        RetStr = retStr;
    }

    /// <summary>
    /// 返回结果代码
    /// </summary>
    public int Code
    {
        get { return _code; }
        set
        {
            _code = value;
            _retstr = _code.ToString() + "|" + _msg;
        }
    }

    /// <summary>
    /// 返回提示信息
    /// </summary>
    public string Msg
    {
        get { return _msg; }
        set
        {
            _msg = value;
            _retstr = _code.ToString() + "|" + _msg;
        }
    }

    /// <summary>
    /// 返回结果组合串Code+"|"+Msg
    /// </summary>
    public string RetStr
    {
        get { return _retstr; }
        set
        {
            if (value.IndexOf("|") > 0)
            {
                try
                {
                    _code = Int32.Parse(value.Substring(0, value.IndexOf("|")));
                    _msg = value.Substring(value.IndexOf("|") + 1);
                    _retstr = _code.ToString() + "|" + _msg;
                }
                catch { }
            }
        }
    }
}
#endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值