问题描述
在我ASP.NET 4.5 WebForms的应用程序,在我的ListView我想绑定DropDownList的,但我不能够做到这一点。其实,模型是绑定到ListView中,有UnitdirectionId和放大器的属性;在编辑和放大器;插入模板我想告诉所有UnitDirection表的下拉列表。
In my ASP.NET 4.5 WebForms app, In my ListView I am trying to bind DropDownList, but am not able to do it. Actually, the Model that is bind to ListView, has a Property of UnitdirectionId & on Edit & Insert Templates' I want to show the dropdown of all UnitDirection table.
下面是我的EditTempalte:
Here is my 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的模型具有proerpty如下:
Now, the Model of LsitView has the proerpty as follows :
[Display(Name= "Unit Direction")]
public int UnitDirectionId { get; set; }
[ForeignKey("UnitDirectionId")]
public virtual UnitDirection UnitDirection { get; set; }
现在,UnitDirection型号:
Now, the UnitDirection Model :
public class UnitDirection
{
[ScaffoldColumn(false)]
public int UnitDirectionId { get; set; }
[Required]
[Display]
public string DirectionTitle { get; set; }
}
错误,我得到的是:
ERROR that I get is :
########## 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)
在分贝UnitDirection表有3项。
In db the UnitDirection table has 3 items.
通过上面的下拉,我相信我应该在ATLEAST下降得到所有UnitDirection的列表中向下。但是,我得到错误。接下来是设置在编辑模板选择的项目。
With the above drop down, I believe I should atleast get list of all UnitDirection in drop down. But on that I am getting error. Then comes to set the selected item in Edit Template.
能否请你帮我鉴定为什么我收到这个错误?
任何帮助高度AP preciated。
Can you please help me identify why I am getting this error ?? Any help is highly appreciated.
感谢
推荐答案
正如我们在计算器聊天讨论,你正面临如下的错误在你的下拉装订方法。
As we discuss in the stackoverflow chat, you are facing below error in your drop down binding method.
System.InvalidOperationException:有已使用此命令,必须先关闭相关联的打开的DataReader
请添加 MultipleActiveResultSets = TRUE
你的web.config连接字符串中为您解决问题。
Please add MultipleActiveResultSets=true
in your web.config connection string to solve your problem.
Server=.\SQLEXPRESS;Database=master;Integrated Security=SSPI;
MultipleActiveResultSets=true;
有关接你UnitDirection价值,你需要写一个方法像下面,并返回UnitDirection标题返回类型。
For fetch you UnitDirection value, you need to write one method like below and return UnitDirection title as return type.
public string getUnitDirectionTitle(int fiUnitDirectionId)
{
return UnitDirection.Where(c => c.UnitDirectionId == fiUnitDirectionId);
}
在你的ItemDataBound事件上面的方法调用之后,
after that call above method in your ItemDataBound event,
TextBox.Text = getUnitDirectionTitle(loItem.UnitDirectionId);
请让我知道如果你有任何问题。
Please let me know if you have any questions.
这篇关于绑定在编辑和放大器的ListView下拉;插入模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!