编程填充的dropdownlist如何进行数据绑定?

chb 2006-08-24 02:39:42
是个学生管理的系统
在formview里建立2个dropdownlist,dropdownlist1是"省",dropdownlist2是"市"
dropdownlist1绑定到学生数据表的“省”字段
dropdownlist2绑定到学生数据表的“市”字段

在dropdownlist2的PreRender事件中编程序根据dropdownlist1中选择的省名进行数据库查询,把对应的市名填入到dropdownlist2中。
现在formview进入edit模式的时候,出错提示“DropDownList2有一个无效 SelectedValue,因为它不在项目列表中”,我分析是市列表还没生成的时候就进行数据绑定了,列表里是空的,数据库里读出来个“北京市”,就出错了。

有什么好办法?
...全文
283 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fds2003 2006-08-24
  • 打赏
  • 举报
回复
http://www.cnblogs.com/singlepine/archive/2005/10/31/265678.html
上面有个例子,你看了就明白了!
chb 2006-08-24
  • 打赏
  • 举报
回复
上贴写错一个地方
应该是Bind("市", "{0}")
chb 2006-08-24
  • 打赏
  • 举报
回复
我现在已经通过Dropdownlist2_PreRender事件实现2个dropdownlist的连动了
现在想实现:
比如数据库中存储的是“广东省”,“广州市”,我希望编辑这个学生的资料时dropdownlist1列出所有省且选中的是“广东省”,dropdownlist2列出广东所有的市且选中的就是“广州市”

如果dropdownlist2中没有SelectedValue='<%# Bind("职务", "{0}") %>'这句,可以列出所有广东的市,加上这句就出错
qizhanfeng 2006-08-24
  • 打赏
  • 举报
回复
循环
smalladam 2006-08-24
  • 打赏
  • 举报
回复
在数据绑定的时候 先绑定DropDownList1

然后根据Dropdownlist1的选择绑定 Dropdownlist2的数据


leafsword_519 2006-08-24
  • 打赏
  • 举报
回复
后台代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
/// <summary>
/// DataGridDoubleDropDownList 的摘要说明。
/// </summary>
public class DataGridDoubleDropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgDoubleDrp;
public string orderID = "";
protected string productID = "";
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
private void Page_Load(object sender, System.EventArgs e)
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();

}

//进行数据绑定
private void DataGridDataBind()
{

//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select top 10 Orders.OrderID,ProductID from Orders,[Order Details] where Orders.OrderID = [Order Details].OrderID order by Orders.OrderID",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
dgDoubleDrp.DataSource = ds.Tables["testTable"];
dgDoubleDrp.DataBind();
}
catch(SqlException error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
public SqlDataReader BindTheOrderID()
{
//定义命令对象
cmd =new SqlCommand("select OrderID from Orders order by OrderID", conn);
//打开数据库联结
conn.Open();
//返回DataReader对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public SqlDataReader BindTheProductID(string OrderID)
{
string strSql = "";

if(OrderID != "" && OrderID != null)
{
strSql = "select ProductID from [Order Details] where OrderID = '"+OrderID+"'";
}
else
{
strSql = "select distinct ProductID from [Order Details]";
}
//定义命令对象
cmd =new SqlCommand(strSql, conn);
//打开数据库联结
conn.Open();
//返回DataReader对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgDoubleDrp.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDoubleDrp_CancelCommand);
this.dgDoubleDrp.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDoubleDrp_EditCommand);
this.dgDoubleDrp.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgDoubleDrp_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgDoubleDrp_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得当前行的订单号和商品号
orderID = ((Label) e.Item.FindControl("lblOrderID")).Text.Trim();
productID = ((Label) e.Item.FindControl("lblproductID")).Text.Trim();
//设置DataGrid控件的当前编辑项的索引
dgDoubleDrp.EditItemIndex = e.Item.ItemIndex;
//重新进行数据绑定
DataGridDataBind();
}

public void DropDownListChange(object sender, System.EventArgs e)
{
//建立drpOrder下拉列表控件的引用对象
DropDownList OrderDrp = (DropDownList) sender;
//取得选择的订单号
orderID = OrderDrp.SelectedItem.Text;
//重新进行数据绑定
DataGridDataBind();
}

private void dgDoubleDrp_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取消编辑
dgDoubleDrp.EditItemIndex = -1;
DataGridDataBind();
}

private void dgDoubleDrp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//判断是否是编辑项
if (e.Item.ItemType == ListItemType.EditItem)
{
//找到编辑项中的drpOrder下拉列表控件
DropDownList drpOrderID = (DropDownList) e.Item.FindControl("drpOrder");
//设置在上个操作中drpOrder下拉列表中选中的项
drpOrderID.SelectedIndex = drpOrderID.Items.IndexOf(drpOrderID.Items.FindByText(orderID));

//找到编辑项中的drpProduct下拉列表控件
DropDownList drpProductID = (DropDownList) e.Item.FindControl("drpProduct");
//设置在上个操作中drpProduct下拉列表中选中的项
drpProductID.SelectedIndex = drpProductID.Items.IndexOf(drpProductID.Items.FindByText(productID));
}
}

}
}
leafsword_519 2006-08-24
  • 打赏
  • 举报
回复
给你个例子
html代码:
<%@ Page language="c#" Codebehind="DataGridDoubleDropDownList.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridDoubleDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridDoubleDropDownList</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="dgDoubleDrp" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 16px"
runat="server" AutoGenerateColumns="False" Width="304px" BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="订单号">
<ItemTemplate>
<asp:Label id=lblOrderID runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.OrderID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=drpOrder runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListChange" DataSource="<%# BindTheOrderID() %>" DataTextField="OrderID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品号">
<ItemTemplate>
<asp:Label id=lblproductID runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ProductID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=drpProduct runat="server" DataSource="<%# BindTheProductID(orderID) %>" DataTextField="ProductID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>
stone3_hl 2006-08-24
  • 打赏
  • 举报
回复
根据Dropdownlist1的SelectedIndexChanged事件绑定 Dropdownlist2的数据
chb 2006-08-24
  • 打赏
  • 举报
回复
因为dropdownlist1在formview这个容器中,我在配置sqldatasource2时直接访问不到dropdownlist1这个控件,只能选择到formview,所以无法建立关联。
LHA 2006-08-24
  • 打赏
  • 举报
回复
看不懂

在数据绑定的时候 先绑定DropDownList1

然后根据Dropdownlist1的选择绑定 Dropdownlist2的数据
chb 2006-08-24
  • 打赏
  • 举报
回复
因为2个dropdownlist都在formview里,所以没法通过分别建立2个数据源,然后2个数据源关联的方法解决。

因为SqlDataSource2关联不到"省"这个控件上.
wxm0930 2006-08-24
  • 打赏
  • 举报
回复
给dropdownlist1\2分别绑定数据源

62,046

社区成员

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

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

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

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