数据在更新时未绑定到下拉列表

时间:2012-06-06 21:49:19

标签: asp.net entity-framework c#-4.0 linq-to-entities

我有两个下拉列表;第一个绑定到实体数据源,第二个根据用户从第一个列表中选择的内容进行更改(最初它也是根据第一个下拉列表预先填充的)。由于某种原因,第二个列表中的数据没有保存到数据库中,我无法弄清楚原因。

这是我的表格视图:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="SuggestionID" OnItemCommand="FormView1_ItemCommand" OnItemUpdated="FormView1_ItemUpdated"
    DataSourceID="UpdateSuggestionEntity" DefaultMode="Edit" OnDataBound="FormView1_Databound" OnItemUpdating="Formview1_OnItemUpdating">
    <EditItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 
                DataSourceID="ServiceAreaEntity" DataTextField="ServiceAreaConcatenated" AutoPostBack="true" 
                DataValueField="ServiceAreaID" SelectedValue='<%# Bind("ServiceAreaID") %>'>
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList2" runat="server" OnPreRender="AddEmptySelect">
            </asp:DropDownList>
        <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" />
        &nbsp;<asp:Button ID="UpdateCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
</asp:FormView>

以下是我在我的代码中设置的内容:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities context = new DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities();
            DropDownList DropDownList2 = (DropDownList)FormView1.FindControl("DropDownList2") as DropDownList;
            int ID = Int32.Parse(Request.QueryString["SuggestionID"]);
            var suggestion = from u in context.Suggestions
                             where u.SuggestionID == ID
                             select u;
            foreach (var i in suggestion) {
                var subServiceArea = from u in context.SubServiceAreas
                                    where u.ServiceAreaID == i.ServiceAreaID
                                    select u;
                DropDownList2.DataSource = subServiceArea;
                DropDownList2.DataTextField = "SubServiceAreaName";
                DropDownList2.DataValueField = "SubServiceAreaID";
                DropDownList2.DataBind();
                DropDownList2.SelectedValue = i.SubServiceAreaID.ToString();
            }
        }
    }

    // Dynamically populate the SubService Area Dropdown
    protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) {
        DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities context = new DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities();
        DropDownList DropDownList1 = (DropDownList)FormView1.FindControl("DropDownList1") as DropDownList;
        DropDownList DropDownList2 = (DropDownList)FormView1.FindControl("DropDownList2") as DropDownList;
        int valueSelected = Int32.Parse(DropDownList1.SelectedItem.Value);
        var subServiceArea = from u in context.SubServiceAreas
                             where u.ServiceAreaID == valueSelected
                             select u;
        DropDownList2.DataSource = subServiceArea;
        DropDownList2.DataValueField = "SubServiceAreaID";
        DropDownList2.DataTextField = "SubServiceAreaName";
        DropDownList2.DataBind();
    }

    protected void AddEmptySelect(Object sender, EventArgs e) {
        DropDownList DropDownList2 = (DropDownList)FormView1.FindControl("DropDownList2") as DropDownList;
        DropDownList2.Items.Insert(0, new ListItem("- Select -", "0"));
    }

除了DropDownList2之外,为什么其他所有内容都能正确保存?它完全忽略了用户在该下拉列表中选择的任何值。它绑定正确(一切都正确显示),但似乎没有与formview的其余部分一起提交。

1 个答案:

答案 0 :(得分:0)

我一直在四处寻找并发现这篇文章: http://www.codeproject.com/Questions/191898/DropDownList-and-capturing-values-after-SelectedIn

我觉得有更好的方法可以做到这一点,但我没有想出任何问题,这种方法就像一个魅力。如果有人愿意做出贡献,我有兴趣了解其他解决方案是否可行。