“DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中
DropDownList和其相关数据库SqlDataSource<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Address" DataValueField="Address"
SelectedValue='<%# Bind("Address") %>'>
<asp:ListItem>江苏</asp:ListItem>
</asp:DropDownList>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnString %>"
SelectCommand="SELECT * FROM [Provice]"></asp:SqlDataSource>
.cs文件代码如下
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// return;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
//User info = e.Row.DataItem ;
//e.Row.Cells[2].Text = (info.Sex as string?).Value == "M" ? "男" : "女";
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList1 = e.Row.FindControl("DropDownList1") as DropDownList;
if (DropDownList1 != null)
{
// DropDownList1.Items.Clear();
// DropDownList1.DataSource = SqlDataSource1;
// DropDownList1.DataTextField = "Address";
// DropDownList1.DataValueField = "Address";
// DropDownList1.DataBind();
DropDownList1.Items.Add("江苏");
// DropDownList1.Items.Insert(0, new ListItem("--请选择--", ""));
DropDownList1.SelectedValue = "江苏";
if (DataBinder.Eval(e.Row.DataItem, "Address") != null)
{
DropDownList1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Address").ToString();
}
else
{
DropDownList1.SelectedIndex = 0;
}
}
}
}
怎么解决啊????????????????????
[ 本帖最后由 轩齐 于 2011-4-10 15:37 编辑 ]