DropDownList二级联动问题请教高手
我在做一个商品类别的二级联动,现在时点击大类时没显示出相应的小类,请教高手下面是后台和前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a_ProductTypeUser.aspx.cs" Inherits="useradmin_a_ProductTypeUser" %>
<!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>
<link href="../App_Themes/default/useradmin_Font.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<br />
<div style="text-align:center">
<table cellpadding="1" cellspacing="1" style="position: relative" height="20%" width="50%">
<tr>
<td colspan="2" style="text-align: center" class="bgcolor2">
<strong><span style="font-size: 11pt"> 添加产品类别</span></strong></td>
</tr>
<tr>
<td style="width: 100px; height: 21px; text-align: right;">产品大类:</td>
<td style="width: 256px; height: 21px; text-align: left;">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="产品大类名称不能为空" Width="192px"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td colspan="2" style="height: 21px; text-align: center;" class="bgcolor2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" Width="83px" OnClientClick="return InitInputChecked();"/>
</td>
</tr>
</table>
<br />
<br />
<table height="20%" width="50%">
<tr>
<td colspan="2" style="text-align: center" class="bgcolor2">
<strong><span style="font-size: 11pt"> 添加产品类别</span></strong></td>
</tr>
<tr>
<td style="width: 100px; height: 21px; text-align: right;">产品小类:</td>
<td style="width: 256px; height: 21px; text-align: left;">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px; text-align: right;">产品大类别:</td>
<td style="width: 256px; text-align: left;">
<asp:DropDownList ID="ddlProvince" runat="server" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged" >
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 100px; text-align: right;">产品小类别:</td>
<td style="width: 256px; text-align: left;">
<asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2" style="height: 21px; text-align: center;" class="bgcolor2">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="添加" Width="83px" OnClientClick="return InitInputChecked();"/>
</td>
</tr>
</table>
</div>
</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;
public partial class useradmin_a_ProductTypeUser : System.Web.UI.Page
{
public string strProvince
{
get
{
return ddlProvince.SelectedItem.Value;
}
set
{
ddlProvince.SelectedItem.Value = value;
}
}
public string strCity
{
get
{
return ddlCity.SelectedItem.Value;
}
set
{
ddlCity.SelectedItem.Value = value;
}
}
DBM.dbmethod mh = new DBM.dbmethod();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// if (Session["u_Id"] == null)
//{
// Response.Write("<script>alert(\"您还没有登录,或您的登录已超时,请登录后继续!\");window.top.location.href='../Default.aspx';</script>");
// return;
//}
//BindList();
ddlProvinceBind();
ddlCity.Items.Insert(0, "-请选择小类-");
}
}
private void ddlProvinceBind()
{
Session["u_Id"] =0;
int p_Uid = Convert.ToInt32(Session["u_Id"]);
//判断用户绑定DropDownList
DataSet ds = mh.returnds_bysting("Select * from TB_ProductType where p_Uid="+p_Uid+" and p_Pid=0");
ddlProvince.DataSource = ds;
ddlProvince.DataTextField = "p_Title";
ddlProvince.DataValueField = "p_Id";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, "-请选择大类-");
}
protected void Button1_Click(object sender, EventArgs e)
{
BLL.ProductType id = new BLL.ProductType();
Maticsoft.Model.TB_ProductType ti = new Maticsoft.Model.TB_ProductType();
ti.p_Title = this.TextBox1.Text.Trim();
ti.p_Uid = Convert.ToInt32(Session["u_Id"]);
id.Add(ti);
Response.Write("<script>alert('添加成功');location.href='a_ProductTypeUser.aspx';</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
BLL.ProductType id = new BLL.ProductType();
Maticsoft.Model.TB_ProductType ti = new Maticsoft.Model.TB_ProductType();
ti.p_Title = this.TextBox2.Text.Trim();
ti.p_Pid=int.Parse(this.ddlProvince.SelectedItem.Value);
ti.p_Uid = Convert.ToInt32(Session["u_Id"]);
id.Add(ti);
Response.Write("<script>alert('添加成功');location.href='a_ProductTypeUser.aspx';</script>");
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
Session["u_Id"] = 0;
int p_Uid = Convert.ToInt32(Session["u_Id"]);
if(ddlProvince.SelectedItem.Value == "-请选择大类-")
{
ddlCity.Items.Clear();
ddlCity.Items.Insert(ddlProvince.SelectedIndex,"-请选择小类-");
}
else
{
//判断用户绑定DropDownList
DataSet ds = mh.returnds_bysting("Select * from TB_ProductType where p_Uid=" + p_Uid + "and p_Pid='"+ddlProvince.SelectedItem.Value +"'");
ddlCity.DataSource = ds;
ddlCity.DataTextField = "p_Title";
ddlCity.DataValueField = "p_Id";
ddlCity.DataBind();
}
}
}