我已经接管了一些遗留代码,一个asp.net应用程序,它是用asp.net 3.5制作和运行的。 现在我们正在更新到.NET 4.0,但这导致了一些问题:
简短的网络应用程序: 我有一个放置在UpdatePanel内的ListView(在MultiView中的View内)。 在那个ListView中,我在InsertItemTemplate中有一个DropDownList。这对我来说是个大问题! 我还有一个ObjectDataSource,它连接到最终调用数据库的底层代码(业务层)。
这是问题: 下拉列表(在.aspx中)有一个DataSourceID,但这会使ListView无法工作(因为有些参数未传递给GetList方法)。如果我删除它,一切正常。当然,除了现在下拉列表没有值,这是没用的。 未传递的两个参数是 - 实际上它们是null:
但是当我在DropDownList中设置DataSourceID时,它们只是null!
我现在将展示一些选定的代码并希望它有意义:
的ObjectDataSource :
<asp:ObjectDataSource ID="odsBudget" runat="server" DeleteMethod="Delete" InsertMethod="InsertItem"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetList" TypeName="LoenFremskrivning.Common.Manager.BudgetManager"
UpdateMethod="UpdateItem" EnablePaging="True" SelectCountMethod="SelectCountForGetList"
SortParameterName="sortExpression">
[cutted out parts]
<InsertParameters>
<asp:ControlParameter ControlID="tbxDatamartServerName" Name="serverName" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="tbxDatamartDatabaseName" Name="databaseName" PropertyName="Text"
Type="String" />
<asp:Parameter Name="loenFremskrivningBudgetID" Type="Int32" />
<asp:Parameter Name="loenFremskrivningBudgetNavn" Type="String" />
<asp:Parameter Name="loenFremskrivningBudgetBeskrivelse" Type="String" />
<asp:Parameter Name="original_LoenFremskrivningBudgetID" Type="Int32" />
<asp:Parameter Name="CopyFromBudgetID" Type="Int32" DefaultValue="0" />
</InsertParameters>
InsertTemplate则
<InsertItemTemplate>
<tr>
<td>
<asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Gem" AccessKey="g"
Width="4em" />
<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Fortryd" AccessKey="f"
Width="4em" />
</td>
<td>
</td>
<td>
<asp:TextBox ID="LoenFremskrivningBudgetNavnTextBox" runat="server" CssClass="table-column-edit-text"
Width="10em" Text='<%# Bind("LoenFremskrivningBudgetNavn") %>' />
</td>
<td>
<asp:TextBox ID="LoenFremskrivningBudgetBeskrivelseTextBox" runat="server" CssClass="table-column-edit-text"
Width="15em" Text='<%# Bind("LoenFremskrivningBudgetBeskrivelse") %>' />
</td>
<td>
<asp:Label ID="OprettetDatoShowLabel" runat="server" CssClass="table-column-show-numeric"
Width="5em" Text='<%# Eval("OprettetDatoShow") %>' />
</td>
<td>
<asp:Label ID="OpdateretDatoShowLabel" runat="server" CssClass="table-column-show-numeric"
Width="5em" Text='<%# Eval("OpdateretDatoShow") %>' />
</td>
<td>
<asp:DropDownList ID="ddlCopyFromBudget" runat="server" DataSourceID="odsBudgetInitial"
DataTextField="LoenFremskrivningBudgetNavn" DataValueField="LoenFremskrivningBudgetID" CssClass="dropdown-list" >
</asp:DropDownList>
</td>
</tr>
所以这个混蛋给了我错误:
<asp:DropDownList ID="ddlCopyFromBudget" runat="server" *DataSourceID="odsBudgetInitial"* DataTextField="LoenFremskrivningBudgetNavn" DataValueField="LoenFremskrivningBudgetID" CssClass="dropdown-list" >
</asp:DropDownList>
现在,我已经搜索了很多,并找到了一些建议,告诉你在代码隐藏中绑定(从未发现与我的问题完全相同): 像这样:http://codeblog.shawson.co.uk/quick-ref/listview-using-a-dropdownlist-populated-using-code-behind/ 或者这个:DropDownList insideTemplateField is not accessible from code-behind。 我尝试过类似的东西,但我在代码隐藏中找不到dropdownlist控件: 我有fx尝试使用 OnItemDataBound 事件
protected void lsvBudget_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var ddlCopyFromBudgetInsert = (DropDownList)e.Item.FindControl("ddlCopyFromBudget");
if (ddlCopyFromBudgetInsert != null)
{
ddlCopyFromBudgetInsert.DataBind();
BindSeriesLookup(ddlCopyFromBudgetInsert, 1);
}
}
}
我必须承认我在asp.net世界中并不那么强大,所以我可以发现有什么问题,所以我希望有人可以指导我。 还记得它适用于.NET 3.5。所以它必然会有一些改变导致问题。也许3.5对数据绑定更宽容了? 是不是已经改变了,你不应该在列表视图中数据化下拉列表?
环境信息: Windows 7,Visual Studio 2010和2013。 Internet Explorer 9,11 Chrome。 我不认为问题与这些事情有关。