DropdownList 我在为其绑定数据源的时候,需要设置一个默认选中的,怎么写代码ASP控件大神来给看看
显示的是所有省份,但是我要通过登陆IP获取对应省份,所以这个省份是默认选中的,绑定数据源的时候,这个语句如何写,我忘球球了,大虾们给力啊...
显示的是所有省份,但是我要通过登陆IP获取对应省份,所以这个省份是默认选中的,绑定数据源的时候,这个语句如何写,我忘球球了,大虾们给力啊
展开
展开全部
MSDN实例,非常详细管用,看看吧。
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!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" >
<script runat="server" >
void Selection_Change(Object sender, EventArgs e)
{
Calendar1.DayStyle.BackColor =
System.Drawing.Color.FromName(ColorList.SelectedItem.Value);
}
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
ColorList.DataSource = CreateDataSource();
ColorList.DataTextField = "ColorTextField";
ColorList.DataValueField = "ColorValueField";
ColorList.DataBind();
ColorList.SelectedIndex = 0;
}
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));
dt.Rows.Add(CreateRow("White", "White", dt));
dt.Rows.Add(CreateRow("Silver", "Silver", dt));
dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));
dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));
dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));
DataView dv = new DataView(dt);
return dv;
}
DataRow CreateRow(String Text, String Value, DataTable dt)
{
DataRow dr = dt.NewRow();
dr[0] = Text;
dr[1] = Value;
return dr;
}
</script>
<head runat="server">
<title> DropDownList Data Binding Example </title>
</head>
<body>
<form id="form1" runat="server">
<h3> DropDownList Data Binding Example </h3>
Select a background color for days in the calendar.
<br /><br />
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br /><br />
<table cellpadding="5">
<tr>
<td>
Background color:
</td>
</tr>
<tr>
<td>
<asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!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" >
<script runat="server" >
void Selection_Change(Object sender, EventArgs e)
{
Calendar1.DayStyle.BackColor =
System.Drawing.Color.FromName(ColorList.SelectedItem.Value);
}
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
ColorList.DataSource = CreateDataSource();
ColorList.DataTextField = "ColorTextField";
ColorList.DataValueField = "ColorValueField";
ColorList.DataBind();
ColorList.SelectedIndex = 0;
}
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));
dt.Rows.Add(CreateRow("White", "White", dt));
dt.Rows.Add(CreateRow("Silver", "Silver", dt));
dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));
dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));
dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));
DataView dv = new DataView(dt);
return dv;
}
DataRow CreateRow(String Text, String Value, DataTable dt)
{
DataRow dr = dt.NewRow();
dr[0] = Text;
dr[1] = Value;
return dr;
}
</script>
<head runat="server">
<title> DropDownList Data Binding Example </title>
</head>
<body>
<form id="form1" runat="server">
<h3> DropDownList Data Binding Example </h3>
Select a background color for days in the calendar.
<br /><br />
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br /><br />
<table cellpadding="5">
<tr>
<td>
Background color:
</td>
</tr>
<tr>
<td>
<asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server"/>
</td>
</tr>
</table>
</form>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以下为asp.net(C#)用法
--------------------
先把数据绑定好,然后:
try
{ this.DropDownListProvince.Items.FindByValue("你想选中的省份").Selected = true; }
catch
{ }
为何用try:
首先,要确定DropDownListProvince.Items.FindByValue是否有返回值,没有的时候会出错;
其次,说如果你已经有选中项,再用这个,会提示出错。
--------------------
先把数据绑定好,然后:
try
{ this.DropDownListProvince.Items.FindByValue("你想选中的省份").Selected = true; }
catch
{ }
为何用try:
首先,要确定DropDownListProvince.Items.FindByValue是否有返回值,没有的时候会出错;
其次,说如果你已经有选中项,再用这个,会提示出错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在你的按钮事件里加入下面代码:(不一样的地方你自己再修改下)
string dlstState= state.SelectedValue.Trim();
string dlstUnit = unit.SelectedValue.Trim();
string dlstStarttime = starttime.SelectedValue.Trim();
StringBuilder sql = new StringBuilder();
if (!dlstState.Equals("全部"))//"全部"为dropdownlist中的第一项,选择此项则认为此条件不进行筛选.
{
sql.Append(" and State='" + dlstState + "'");
}
if (!dlstUnit.Equals("全部"))
{
sql.Append(" and Unit='" + dlstUnit + "'");
}
if (!dlstStarttime.Equals("全部"))
{
sql.Append(" and StartTime='" + dlstStarttime+ "'");
}
sql.Append(" order by StartTime");
gv.DataSourse=数据源;
gv.DataBind();
string dlstState= state.SelectedValue.Trim();
string dlstUnit = unit.SelectedValue.Trim();
string dlstStarttime = starttime.SelectedValue.Trim();
StringBuilder sql = new StringBuilder();
if (!dlstState.Equals("全部"))//"全部"为dropdownlist中的第一项,选择此项则认为此条件不进行筛选.
{
sql.Append(" and State='" + dlstState + "'");
}
if (!dlstUnit.Equals("全部"))
{
sql.Append(" and Unit='" + dlstUnit + "'");
}
if (!dlstStarttime.Equals("全部"))
{
sql.Append(" and StartTime='" + dlstStarttime+ "'");
}
sql.Append(" order by StartTime");
gv.DataSourse=数据源;
gv.DataBind();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询