我正在尝试创建一个 FormView,它在 EditTemplate 中可以根据其他一些标志显示一个文本框或一个绑定属性的下拉列表:
<asp:FormView
id="EmailFormView"
runat="server"
DefaultMode="Edit"
DataSourceID="EmailDataSource"
OnDataBound="EmailFormView_DataBound"
>
<EditItemTemplate>
<asp:PlaceHolder ID="DelayHoursPlaceHolder" runat="server">
<asp:TextBox CssClass="HoursBox" ID="ShortDelayTextBox" runat="server" Text='<%# Bind( "ShortDelay" ) %>' />
</asp:PlaceHolder>
<asp:PlaceHolder ID="DelayDropdownPlaceHolder" runat="server">
<asp:DropDownList ID="ShortDelay" runat="server" SelectedValue='<%# Bind( "ShortDelay" ) %>'
DataValueField="Value" DataTextField="Text" DataSourceID="DropDownDataSource" />
</asp:PlaceHolder>
</EditItemTemplate>
</asp:FormView>
这适用于以下代码:
protected void EmailFormView_DataBound( object sender, EventArgs e ) {
var email = EmailObj;
if (email.EmailType == EmailType.Type1) {
EmailFormView.Row.FindControl("DelayHoursPlaceHolder").Visible = false;
EmailFormView.Row.FindControl("DelayDropdownPlaceHolder").Visible = true;
}
else {
EmailFormView.Row.FindControl("DelayHoursPlaceHolder").Visible = true;
EmailFormView.Row.FindControl("DelayDropdownPlaceHolder").Visible = false;
EmailFormView.Row.FindControl("DelayDropdownPlaceHolder").Controls.Clear();
}
}
这可以很好地显示和隐藏适当的控件,但问题是当页面被回发时,该值正在丢失。
关于如何使它工作的任何想法?