DropDownList二级联动问题请教高手

liumiao56 2009-05-05 11:02:03
我在做一个商品类别的二级联动,现在时点击大类时没显示出相应的小类,请教高手下面是后台和前台代码:
<%@ 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();
}
}
}
...全文
183 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tommy326 2009-05-06
  • 打赏
  • 举报
回复
将第一个下拉的AutoPostBack 设置为true
liumiao56 2009-05-06
  • 打赏
  • 举报
回复
谢谢,已经解决
是AutoPostBack的事
liumiao56 2009-05-06
  • 打赏
  • 举报
回复
??
k3109852 2009-05-05
  • 打赏
  • 举报
回复
<asp:DropDownList id="DropDownListBigKinds" runat="server" Width="100px" AutoPostBack="true" OnSelectedIndexChanged="DropDownListBigKinds_SelectedIndexChanged"></asp:DropDownList>
楼上的都正确,设置DropDownList的AutoPostBack属性为true。
但这样会导致全页面刷新,建议使用ajax。
ouyang3857116 2009-05-05
  • 打赏
  • 举报
回复
的确,要将AutoPostBack属性设为True
<asp:DropDownList ID="ddlProvince" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged" >
</asp:DropDownList>
lishuaide0517 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aa2223 的回复:]
如果不是AJAX连动,你必须使第一个dropdownlist里面可以autopostback,这样你后面的索引事件才起作用!
[/Quote]
up
屌丝女士111 2009-05-05
  • 打赏
  • 举报
回复
autopostback看看这个勾了么?
aa2223 2009-05-05
  • 打赏
  • 举报
回复
如果不是AJAX连动,你必须使第一个dropdownlist里面可以autopostback,这样你后面的索引事件才起作用!
dsr456 2009-05-05
  • 打赏
  • 举报
回复
建议使用ajaxtoolkit中的级联dropdown

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧