您的位置:首页 > 其它

基于.NET实现淘宝发布宝贝功能(四)

2015-08-10 15:32 666 查看
1、产品分类,界面如下



实现难点,每点一个类目节点,需要动态创建其子类目控件,并要保证控件数据在刷新后不会被清空。

2、功能介绍

控件名暂定为“UCProductType.ascx”

用户选择相应的产品分类,当点击到最末一层分类时触发SelectedLastLevel事件,该事件有两个参数

this.SelectedLastLevel(this, new WebEventArgs("last", new string[] { ptt_id.ToString(), listBox1.ClientID }));

WebEventArgs,name=last表示当前用户选择的是最末层分类,并以数组形式返回产品分类表对应的pt_id和控件的客户端编号。

控件引用:

在页面初始化时,注册SelectedLastLevel事件进行监听,

protected void Page_Load(object sender, EventArgs e)
{
this.UCProductType1.SelectedLastLevel += UCProductType1_SelectedLastLevel;
}
        void UCProductType1_SelectedLastLevel(object sender, WebEventArgs e)
        {
//触发相应事件
        }


如果是另一个用户控件,需与"UCProductType.ascx"进行事件关联,则最好将UCProductType类的属性定义在其控件内,并在其控件中绑定相应事件,如:

控件UCFields.ascx,中需要实现功能,在点击类目控件“UCProductType.ascx”时,显示相应的类目节点,则可以通过以下方法实现。

public partial class UCFields

{

UCProductType uCProductType1;

public UCProductType UCProductType1
{
get { return uCProductType1; }
set { uCProductType1 = value;
if (this.uCProductType1 != null)
{
this.uCProductType1.SelectedLastLevel += uCProductType1_SelectedLastLevel;
}

}
}
void uCProductType1_SelectedLastLevel(object sender, WebEventArgs e)
{
if (!string.IsNullOrEmpty(e.Name) && e.Name == "last")
{
string[] arr = (string[])e.E;
alert(arr[0]);//弹出选中的类目id,最末层
}
else
{
alert(arr[0]);//弹出选中的类目id,非最末层
}
}




}

3、控件后台代码

public delegate void WebEventHanlder(object sender,WebEventArgs e);


public partial class UCProductType : System.Web.UI.UserControl
{
PClassManager pclassManager = new PClassManager();

public event WebEventHanlder SelectedLastLevel;

public string CssClass
{
set { this.tbProductType.CssClass = value; }
get { return this.tbProductType.CssClass; }
}

public List<ListBox> ListBoxs
{
get
{
List<ListBox> list = new List<ListBox>();
int j = 0;
foreach (int pt_id in this.List_PT_ID)
{
foreach (Control item in this.PlaceHolder1.Controls)
{
if (item is ListBox)
{
list.Add(item as ListBox);
}
}
j++;
}
return list;
}
}

public int LastSelectedValue
{
get
{
if (this.List_PT_ID.Count > 0)
{
int ii = this.List_PT_ID.Count<int>(w => { return w > 0; }) - 1;
foreach (Control item in this.PlaceHolder1.Controls)
{
if (item is ListBox)
{
ListBox lb = item as ListBox;
if (lb.ID.EndsWith("listBox_ct_" + ii.ToString(), StringComparison.OrdinalIgnoreCase))
{
if (lb.SelectedIndex > 0)
{
return Convert.ToInt32(lb.SelectedValue);
}
}
}
}
}
return -1;
}
}

/// <summary>
/// 当前类别选中的值
/// </summary>
public string SelectedValue
{
set {

if (!string.IsNullOrEmpty(value))
{
string[] arr = value.Split(',');
this.List_PT_ID = new List<int>();
this.List_PT_ID.Add(0);
foreach (var pt_id in arr)
{
this.List_PT_ID.Add(Convert.ToInt32(pt_id));
}
this.RefreshProductType();

int i = 0;
foreach (var item in this.PlaceHolder1.Controls)
{
if (item is ListBox)
{
ListBox lb1 = item as ListBox;
if (i <= arr.Length - 1)
{

ListItem ll = lb1.Items.FindByValue(arr[i]);
if (ll != null)
{
ll.Selected = true;
}
//设置最末层节点
if (i == this.PlaceHolder1.Controls.Count - 1)
{
this.tbProductType.Text = arr[i];
int ppt_id = Convert.ToInt32(arr[i]);
int icount = pclassManager.GetProductTypeListCountByFatherID(ppt_id);
if (icount <= 0)
{
if (this.SelectedLastLevel != null)
this.SelectedLastLevel(this, new WebEventArgs("last", new string[] {
arr[i], lb1.ClientID }));
return;
}
}
}
}
i++;
}

}
}
get
{
StringBuilder sb = new StringBuilder();
int j = 0;
foreach (int pt_id in this.List_PT_ID)
{
foreach (Control item in this.PlaceHolder1.Controls)
{
if (item is ListBox)
{
ListBox lb = item as ListBox;
if (lb.ID.EndsWith("listBox_ct_" + j.ToString(), StringComparison.OrdinalIgnoreCase))
{
if (lb.SelectedIndex > 0)
{
sb.AppendFormat(",{0}", lb.SelectedValue);
}
}
}
}
j++;
}
if (sb.Length > 0)
{
return sb.ToString().Substring(1);
}
else
return string.Empty;
}
}

public void SetFocus()
{
ListBox lbFirst = null;
foreach (int level in this.List_PT_ID)
{
foreach (Control item in this.PlaceHolder1.Controls)
{
if (item is ListBox)
{
ListBox lb = item as ListBox;
if (lbFirst == null)
lbFirst = lb;
if (lb.SelectedIndex < 0)
{
lb.Focus();
return;
}
}
}
}
if (lbFirst != null)
lbFirst.Focus();
}

public List<int> List_PT_ID
{
get
{
if (this.ViewState["List_PT_ID"] == null)
return new List<int>();
else
return (List<int>)this.ViewState["List_PT_ID"];
}
set
{
this.ViewState["List_PT_ID"] = value;
}
}

protected void RefreshProductType()
{
if (this.List_PT_ID.Count <= 0)
{
this.BindProductType(0, 0);

this.List_PT_ID.Add(0);
this.RemovePT(0);

}
else
{
int i = 0;
List<int> tmpList = new List<int>();
int[] arr = new int[this.List_PT_ID.Count];

this.List_PT_ID.CopyTo(arr);
this.List_PT_ID.Clear();

tmpList.AddRange(arr);

foreach (var item in tmpList)
{
this.BindProductType(i, item);
i++;
}
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (this.PlaceHolder1.Controls.Count <= 0)
{
this.RefreshProductType();
}
}

void RemovePT(int level)
{
List<int> tmpList = this.List_PT_ID;
for (int i = tmpList.Count - 1; i >= 0; i--)
{
if (i > level)
{
tmpList.RemoveAt(i);
}
}
this.List_PT_ID = tmpList;
}

void RemoveListbox(int level)
{
List<Control> listControl = new List<Control>();
foreach (Control ct in this.PlaceHolder1.Controls)
{
listControl.Add(ct);
}
for (int i = listControl.Count - 1; i >= 0; i--)
{
Control ct = listControl[i];
if (ct is ListBox)
{
ListBox listbox = ct as ListBox;
int controlLevel = ControlID2Level(listbox.ID);
if (level < controlLevel)
{
this.PlaceHolder1.Controls.Remove(ct);
}
}
}
}

/// <summary>
/// 转换层级
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
int ControlID2Level(string id)
{
int pos = id.IndexOf("_ct_");
if (pos > -1)
{
return Convert.ToInt32(id.Substring(pos + 4));
}
else
return 0;
}

void BindProductType(int level, int pt_id)
{
this.List_PT_ID.Add(pt_id);
this.RemovePT(pt_id);
List<ProductType> listType = pclassManager.GetProductTypeListByFatherID(pt_id);
if (listType != null && listType.Count > 0)
{
ListBox listBox1 = new ListBox();
listBox1.ID = "listBox_ct_" + level.ToString();
listBox1.CssClass = "ip mr10";
listBox1.AutoPostBack = true;
listBox1.Width = 180;
listBox1.Rows = 10;
listBox1.DataSource = listType;
listBox1.DataTextField = "PT_Name";
listBox1.DataValueField = "PT_ID";
listBox1.DataBind();
listBox1.Items.Insert(0, new ListItem("请选择服务类目", ""));

listBox1.SelectedIndexChanged += (sender, e) =>
{
int ii = level + 1;
int ivv = ControlID2Level(listBox1.ID);
if (listBox1.SelectedValue != "")
{
RemoveListbox(ivv);
RemovePT(ivv);
int ptt_id = Convert.ToInt32(listBox1.SelectedValue);
this.BindProductType(ii, ptt_id);
this.tbProductType.Text = this.SelectedValue;
int icount = pclassManager.GetProductTypeListCountByFatherID(ptt_id);
if (icount <= 0)
{
if (this.SelectedLastLevel != null)
this.SelectedLastLevel(this, new WebEventArgs("last", new string[] { ptt_id.ToString(), listBox1.ClientID }));
return;
}
}else
{
RemoveListbox(ivv);
RemovePT(ivv);
this.tbProductType.Text = this.SelectedValue;
}
if (this.SelectedLastLevel != null)
this.SelectedLastLevel(this, new WebEventArgs(ivv.ToString(), listBox1.ClientID));
};
this.PlaceHolder1.Controls.Add(listBox1);
}
}

}


4、控件UI层代码
说明:tbProductType是用于存储当前选中值,PlaceHolder用于承载动态创建的ListBox控件
<div style="display: none;">
<asp:TextBox ID="tbProductType" runat="server"></asp:TextBox>
</div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>


在引用的页面加入ScriptManager控件,和UpdatePanel控件,实现局部新联动。另外数据库访问类各位自己行解决吧,到此“产品分类目”创建完成
Site.Master页面,其它页面继承它即可。
<!DOCTYPE html>
<html lang="zh">
<head runat="server">
<meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<title><%: Page.Title %></title>
<link href="/Scripts/jquery-1.11.2/themes/redmond/jquery-ui.min.css" rel="stylesheet" />
<script src="/Scripts/jquery-1.11.2/jquery-1.11.2.min.js"></script>
<script src="/Scripts/jquery-1.11.2/jquery-ui.min.js"></script>
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="false" runat="server">
</asp:ScriptManager>
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</form>
</body>
</html>


引用控件页面:
<%@ Register Src="~/UserControls/UCProductType.ascx" TagPrefix="uc1" TagName="UCProductType" %>
<asp:UpdatePanel ID="upPT" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<uc1:UCProductType runat="server" ID="UCProductType1" />
</ContentTemplate>
</asp:UpdatePanel>




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: