在我的ASP.NET 4.5 WebForms应用程序中,在我的ListView中,我试图绑定DropDownList,但我无法做到。实际上,绑定到ListView的Model具有UnitdirectionId&属性。在编辑&插入模板'我想显示所有UnitDirection表的下拉列表。
这是我的EditTempalte:
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="unitDirTxt" runat="server" Text='<%# Bind("UnitDirectionId") %>'></asp:TextBox>
<!-- <asp:DynamicControl runat="server" DataField="UnitDirectionId" ID="UnitDirectionId" Mode="Edit" /> -->
<asp:DropDownList ID="unitDirEditDrop" runat="server" ItemType="VincitoreCRMApplication.Models.UnitDirection"
DataTextField="DirectionTitle" DataValueField="UnitDirectionId"
SelectMethod="GetUnitDirections"></asp:DropDownList>
</td>
</tr>
</EditItemTemplate>
在Code背后,
public IQueryable<UnitDirection> GetUnitDirections()
{
return _db.UnitDirections;
}
现在,LsitView的模型具有如下优点:
[Display(Name= "Unit Direction")]
public int UnitDirectionId { get; set; }
[ForeignKey("UnitDirectionId")]
public virtual UnitDirection UnitDirection { get; set; }
现在,UnitDirection模型:
public class UnitDirection
{
[ScaffoldColumn(false)]
public int UnitDirectionId { get; set; }
[Required]
[Display]
public string DirectionTitle { get; set; }
}
我得到的错误是:
########## ERRO : An error occurred while executing the command definition. See the inner exception for details.
STACK : at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteSto reCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResul tType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.ListControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.Control.DataBindChildren()
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray)
at System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.ListView.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
在db中,UnitDirection表有3个项目。
通过上面的下拉,我相信我应该至少得到所有UnitDirection列表中的下拉列表。但就此而言,我遇到了错误。然后在“编辑模板”中设置所选项目。
你能帮我解释一下为什么我会收到这个错误吗? 任何帮助都非常感谢。
由于
答案 0 :(得分:1)
正如我们在stackoverflow聊天中讨论的那样,您在下拉绑定方法中面临以下错误。
System.InvalidOperationException:已经有一个与此命令关联的打开的DataReader,必须先关闭它。
请在您的web.config连接字符串中添加MultipleActiveResultSets=true
以解决您的问题。
Server=.\SQLEXPRESS;Database=master;Integrated Security=SSPI;
MultipleActiveResultSets=true;
要获取UnitDirection值,您需要编写一个类似下面的方法并返回UnitDirection标题作为返回类型。
public string getUnitDirectionTitle(int fiUnitDirectionId)
{
return UnitDirection.Where(c => c.UnitDirectionId == fiUnitDirectionId);
}
在ItemDataBound事件中调用上面的方法之后
TextBox.Text = getUnitDirectionTitle(loItem.UnitDirectionId);
如果您有任何疑问,请与我们联系。