表中有数据
aa bb
--- ---
f1 0
f2 0
f3 1
f4 0
aa是在DropDownList显示的值
当bb=1时DropDownList默认就是F3如何操作呀,谢了
aa bb
--- ---
f1 0
f2 0
f3 1
f4 0
aa是在DropDownList显示的值
当bb=1时DropDownList默认就是F3如何操作呀,谢了
解决方案 »
- 指定的转换无效
- vs2005测试没问题,为什么放到inter上测试怎么就卡住了呢?
- 请教数据库设计的复杂问题,高手们请进……………………¥¥¥¥
- 如何取得datatable里的某个字段里的值?
- 分页缓存的问题?
- asp.net session对象使用问题!急!!在线等!!!!!
- 关于在后台调用前台js函数
- 为什么directoryinfo在服务器上不能建立子目录?
- 读取oracle中blob字段时,取出来的值跟输入的不一样,请高手指点。急~~~~~~~
- 如何访问HeaderTemplate中的控件?
- KB956391导致无法打印,卸载后仍然不可以
- 调用kindeditor时报Microsoft JScript 运行时错误: 'style' 为空或不是对象
<asp:DropDownList ID="ddlRateName" runat="server" Style="font-size: 11px; color: blue"
DataSource = '<%# BindRateName() %>' DataTextField = "rateName" SelectedValue='<%# Bind("rateName") %>'>
</asp:DropDownList>后台:
#region 绑定职称
protected DataTableReader BindRateName()
{
string selectSql = "select distinct rateName from zd_employee where rateName is not null and rateName != '' order by rateName ";
SqlData da = new SqlData();
DataTableReader dr = null; try
{
dr = da.ExceDr(selectSql);
}
catch
{
Response.Write("<script>alert('绑定职称信息失败!')</script>");
return null;
} return dr;
}
#endregion
DropDownList1.DataTextField="aa"//显示的值
DropDownList1.DataValueField="bb"隐藏的值,取的时候就用DropDownList1.SelectedValue
这样就可以取到bb的值了
this.DropDownList1.DataTextField = "aa";
this.DropDownList1.DataValueField = "bb";
this.DropDownList1.DataBind();
this.DropDownList1.SelectedValue = "1";
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;namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitData();
}
} private void InitData()
{
DataTable table = GenerateDataTable();
this.DropDownList1.DataSource = table;
this.DropDownList1.DataTextField = "aa";
this.DropDownList1.DataValueField = "bb";
this.DropDownList1.DataBind();
} /// <summary>
/// 产生DataTable
/// </summary>
/// <returns></returns>
private DataTable GenerateDataTable()
{
DataTable table = new DataTable();
DataColumn aa = new DataColumn("aa", typeof(string));
DataColumn bb = new DataColumn("bb", typeof(int));
table.Columns.Add(aa);
table.Columns.Add(bb); DataRow row = table.NewRow();
row["aa"] = "f1";
row["bb"] = 0;
table.Rows.Add(row); row = table.NewRow();
row["aa"] = "f2";
row["bb"] = 0;
table.Rows.Add(row); row = table.NewRow();
row["aa"] = "f3";
row["bb"] = 1;
table.Rows.Add(row); row = table.NewRow();
row["aa"] = "f4";
row["bb"] = 0;
table.Rows.Add(row); return table;
} protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ Response.Write(string.Format("您选择的值是:{0}", DropDownList1.SelectedValue));
}
}
}<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %><!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">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged ="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>
DropDownList1.Items.Insert(0, "F3");
1. 如果自己写数据绑定 方法:
//绑定年
public static void BindYear(DropDownList list)
{ list.Items.Clear();
ListItem item = new ListItem("2010");
list.Items.Insert(0, item);
item = new ListItem("2009");
list.Items.Insert(1, item);
item = new ListItem("2008");
list.Items.Insert(2, item);
}2.如果查询数据库数据绑定 方法:
//绑定部门下拉框
public static void BindDept(DropDownList list)
{
DataSet ds = new DataSet();
ds = 你的查询方法
list.DataSource = ds;
list.DataTextField = "deptname";
list.DataValueField = "deptcode";
list.DataBind(); }
this.DropDownList1.DataSource = table;
this.DropDownList1.DataTextField = "aa";
this.DropDownList1.DataValueField = "bb";
this.DropDownList1.DataBind();
foreach (ListItem item in this.DropDownList1.Items)
{
if (item.Value.ToString() == "1")
{
item.Selected = true;
break;
}
}
再用SelectedValue属性来设置
我非常喜欢这种写法。实际开发使用时,应该注意 BindRateName() 内部应该首先查询数据缓存,用查询Sql语句作为缓存单元的key进行查询,当没有缓存时才真正查询数据库。
this.DropDownList1.DataTextField = "aa";
this.DropDownList1.DataSource = dt;
this.DropDownList1.DataValueField = "bb";
this.DropDownList1.DataTextField = "aa";
this.DropDownList1.DataBind();