DropDownList 按级别显示
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Area.aspx.cs" Inherits="Area" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body >
<form id="form1" runat="server">
<select id="m_ddlRegion" runat="server" style="width: 182px">
<option selected="selected"></option>
</select>
<input id="Button1" type="button" value="button" onclick="ReplaceNbsp();" />
</form>
</body>
</html>
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 System.Data.SqlClient;
using SDCRM;
using System.Collections.Generic;
public partial class Area : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SqlConnection con = new SqlConnection(SqlHelper.ConnectionString))
{
con.Open();
FillDLL(0, con,-1);
}
}
}
private void FillDLL(int parID,SqlConnection con,int level)
{
level++;
SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@parID",SqlDbType.Int)};
parm[0].Value = parID;
using (DataSet ds= SqlHelper.ExecuteDataset(con, CommandType.StoredProcedure, "crmGetRegion", parm))
{
ListItem item = null;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string AddToItemText = "";
for (int j = 0; j < level; j++)
{
AddToItemText += " ";//全角空格
}
item = new ListItem(AddToItemText + "|--" + ds.Tables[0].Rows[i]["name"].ToString(), AddToItemText + "|--" + ds.Tables[0].Rows[i]["name"].ToString());
m_ddlRegion.Items.Add(item);
AddToItemText = "";
FillDLL(Convert.ToInt32(ds.Tables[0].Rows[i]["id"]), con,level);
}
level--;
}
}
}
T-SQL:select id,name from area where parid=@parID
浙公网安备 33010602011771号