2012-06-15 56 views
0

我有一個具有MailingState屬性的實體類。我想這樣做的另一個屬性是價值的一些操作,所以我說:如何綁定到我的實體部分類中的自定義屬性

<asp:DropDownList ID="ddlMailingState" runat="server" DataSourceID="ddlAllStates" 
     AppendDataBoundItems="True" DataTextField="StateAbbreviation" 
     DataValueField="StateAbbreviation" 
     SelectedValue='<%# Bind("MailingStateAbbreviation") %>'> 
    <asp:ListItem Value="" Text="" /> 
</asp:DropDownList> 

public string MailingStateAbbreviation 
{ 
    get { return getStateAbbreviation(MailingState); } 
    set { MailingState = value; } 
} 

然而,在DetailsViewEditItemTemplate,下面當我嘗試提交編輯表格失敗如果我改變BindEvalSelectedValue,形式submi

A property named 'MailingStateAbbreviation' was not found on the entity during an insert, update, or delete operation. Check to ensure that properties specified as binding expressions are available to the data source.

:我得到這個錯誤ts罰款,但MailingState屬性不更新。我怎樣才能將表單控件綁定到我的實體上的自定義屬性?

回答

0

雖然我寧願採用更多的聲明式方法,但不涉及處理事件,這很有效。我添加了一個OnItemUpdating處理程序的DetailsView,實現如下:

protected void dvOrg_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) 
{ 
    var view = (DetailsView)sender; 
    var mailStateDdl = (DropDownList)view.FindControl("ddlMailingState"); 
    e.NewValues["MailingState"] = mailStateDdl.SelectedValue; 
} 

在我的.aspx,我有這樣的:

<asp:DropDownList ID="ddlMailingState" runat="server" DataSourceID="ddlAllStates" 
     AppendDataBoundItems="True" DataTextField="StateAbbreviation" 
     DataValueField="StateAbbreviation" 
     SelectedValue='<%# Eval("MailingStateAbbreviation") %>'> 
    <asp:ListItem Value="" Text="" /> 
</asp:DropDownList> 
相關問題