0

我正在努力实现以下目标:

  1. 消除FormView’s默认的编辑链接按钮。

  2. 使用当前数据和其他 DDL 数据选择显示FormView处于编辑模式。Page_Load

目前我有以下将 DDL 绑定到基于id = x等的当前数据库值:

<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("xxx") %>' 
DataSourceID="xxx"
DataTextField="xxx"
DataValueField="xxx"
ondatabound="DropDownList1_DataBound">

通过 C# 代码,我这样做是为了添加额外的 DDL 列表项以供选择:(这是浪费我的精力,因为数据已经在数据库中了!)

protected void DropDownList1 _DataBound(object sender, EventArgs e)
{
     DropDownList DropDownList1 = (DropDownList)FormView1.FindControl("DropDownList1");
     DropDownList1.Items.Insert(1, new ListItem("0 - 1 km/h", "0"));
     DropDownList1.Items.Insert(2, new ListItem("2 - 5 km/h", "1"));
     DropDownList1.Items.Insert(3, new ListItem("6 - 11 km/h", "2"));
     DropDownList1.Items.Insert(4, new ListItem("12 - 19 km/h", "3"));
     DropDownList1.Items.Insert(5, new ListItem("20 - 28 km/h", "4"));
}

一定有更好的方法!

4

1 回答 1

0

不知道你是如何连接到数据库的,但如果你使用 datacontext linq to sql 是一个很好的方法。您的“ddl”将有一个 onLoad 事件,您可以在后面的代码中使用查询。例如:

<asp:DropDownList ID="DropDownList1" runat="server"
                  onLoad="DropDownList1_Load"
                  AutoPostBack="true" />

然后在后面的代码中:

protected void DropDownList1_Load(object sender, EventARgs e)
{
    YourDataContext db = new YourDataContext();
    var qry = from a in db.table1
              select a;
    foreach(var itm in qry)
    {
        DropDownList1.Items.Add(new ListItem(itm.speedRange.ToString(), itm.id.ToString()));
     db.Dispose();
}
于 2013-01-22T21:57:50.870 回答