webforms – ASP.NET Web窗体(4.5)强类型模型绑定 – ListView的InsertItemTemplate中的DropDownList

前端之家收集整理的这篇文章主要介绍了webforms – ASP.NET Web窗体(4.5)强类型模型绑定 – ListView的InsertItemTemplate中的DropDownList前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
注意:这是.NET 4.5和NOT MVC中的ASP.NET Web窗体模型绑定.

我正在使用ASP.NET Web窗体(4.5)的新的强类型模型绑定功能生成可以编辑的项目列表.这适用于查看初始列表,编辑项目和删除项目.但是我在插入新项目时遇到了问题.

具体来说,在我的EditItemTemplate和InsertItemTemplate中,我有一个DropDownList(实际上,它是一个从DropDownList派生的自定义控件,但出于这个问题的目的,它是一个DropDownList).控件在标记内定义如下……

  1. <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server"
  2. SelectedValue="<%#: BindItem.ClientStatusID %>" />

在EditItemTemplate中,这很好但是在InsertItemTemplate中,这会在运行页面生成错误:数据绑定方法(如Eval(),XPath()和Bind()只能在数据绑定控件的上下文中使用.

因此,我删除了SelectedValue =“<%#:BindItem.ClientStatusID%>”部分.从InsertItemTemplate再次尝试.这次没有错误消息,但是当调用ListView.InsertMethod时,模型上的ClientStatusID属性未设置为DropDownList的值(而其他属性设置正确).

ListView.InsertMethod:

  1. public void ListView_InsertMethod(int ID) {
  2.  
  3. Model model = this.DbContext.Models.Create();
  4. if (this.TryUpdateModel(model)) {
  5. this.DbContext.SaveChanges();
  6. this.ListView.DataBind();
  7. }
  8.  
  9. }

Model类:

  1. public class Model{
  2.  
  3. public Int32 ID { get; set; }
  4. public String Description { get; set; }
  5. public Boolean IsScheduleFollowUp { get; set; }
  6. public Nullable<Int32> ClientStatusID { get; set; }
  7.  
  8. }

EditItemTemplate:

  1. <EditItemTemplate>
  2. <tr>
  3. <td>
  4. <asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
  5. </td>
  6. <td>
  7. <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
  8. </td>
  9. <td>
  10. <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" SelectedValue="<%#: BindItem.ClientStatusID %>" />
  11. </td>
  12. <td>
  13. <asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update" Text="Update" />
  14. <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel" Text="Cancel" />
  15. </td>
  16. </tr>
  17. </EditItemTemplate>

InsertItemTemplate:

  1. <InsertItemTemplate>
  2. <tr>
  3. <td>
  4. <asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
  5. </td>
  6. <td>
  7. <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
  8. </td>
  9. <td>
  10. <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" />
  11. </td>
  12. <td>
  13. <asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert" Text="Add" />
  14. </td>
  15. </tr>
  16. </InsertItemTemplate>

我原本以为是控件的ID用于确定模型上将传递给值的属性(即TextBox被称为“描述”的位置,该值将被传递给“描述”该模型的财产).显然情况并非如此,而是由“<%#BindItem.Description%>”控制,但是从这个问题的其余部分可以看出,我无法在“InsertItemTemplate”中使用此语法.我无法相信在这种情况下不支持DropDownList,但我找不到使用Google或Bing的4.5模型绑定使用DropDownList的任何示例(实际上,ASP中新模型绑定的例子很少. NET 4.5使用除了几个TextBox控件之外的任何东西).

任何人都可以进一步阐明这个问题(最好告诉我需要做些什么)?

关于我的其他问题我已经看过……

> Binding a DropDownList in ListView InsertItemTemplate throwing an error
> ASP.NET ListView with identical markup in EditItemTemplate and InsertItemTemplate
> Bind SelectedValue of ASP.net DropDownList to custom object
> Databinding to ASP.NET DropDownList list in ListView

所有这些都使用较旧的样式绑定方法,而不是4.5中的新方法

谢谢.

解决方法

我一直在做类似的事情,并且能够得到一个样本,所以我想我会发布我所拥有的,看看这对你有帮助.

这是我的页面代码

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="listview-databind.aspx.cs"
  2. Inherits="test_listview_databind" Debug="true" %>
  3.  
  4. <!DOCTYPE html>
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7. <title></title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <asp:ListView ID="lv" runat="server" ItemType="DataModel" DataKeyNames="Id" SelectMethod="lv_GetData"
  12. InsertItemPosition="FirstItem" InsertMethod="lv_InsertItem" UpdateMethod="lv_UpdateItem">
  13. <LayoutTemplate>
  14. <table>
  15. <tr id="itemPlaceholder" runat="server"></tr>
  16. </table>
  17. </LayoutTemplate>
  18. <ItemTemplate>
  19. <tr>
  20. <td>
  21. <asp:Literal ID="Description" runat="server" Text="<%# Item.Description %>" />
  22. </td>
  23. <td>
  24. <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# Item.IsScheduleFollowUp %>" />
  25. </td>
  26. <td>
  27. <asp:Literal ID="ClientStatusId" runat="server" />
  28. </td>
  29. <td>
  30. <asp:Button ID="Edit" runat="server" ClientIDMode="Static" CommandName="Edit"
  31. Text="Edit" />
  32. </td>
  33. </tr>
  34. </ItemTemplate>
  35. <InsertItemTemplate>
  36. <tr>
  37. <td>
  38. <asp:TextBox ID="Description" runat="server" Text="<%# BindItem.Description %>" />
  39. </td>
  40. <td>
  41. <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
  42. </td>
  43. <td>
  44. <asp:DropDownList ID="ClientStatusId" runat="server" SelectedValue="<%# BindItem.ClientStatusId %>">
  45. <asp:ListItem>1</asp:ListItem>
  46. <asp:ListItem>2</asp:ListItem>
  47. </asp:DropDownList>
  48. </td>
  49. <td>
  50. <asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert"
  51. Text="Add" />
  52. <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel"
  53. Text="Cancel" />
  54. </td>
  55. </tr>
  56. </InsertItemTemplate>
  57. <EditItemTemplate>
  58. <tr>
  59. <td>
  60. <asp:TextBox ID="Description" runat="server" Text="<%# BindItem.Description %>" />
  61. </td>
  62. <td>
  63. <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
  64. </td>
  65. <td>
  66. <asp:DropDownList ID="ClientStatusId" runat="server" SelectedValue="<%# BindItem.ClientStatusId %>">
  67. <asp:ListItem>1</asp:ListItem>
  68. <asp:ListItem>2</asp:ListItem>
  69. </asp:DropDownList>
  70. </td>
  71. <td>
  72. <asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update"
  73. Text="Update" />
  74. <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel"
  75. Text="Cancel" />
  76. </td>
  77. </tr>
  78. </EditItemTemplate>
  79. </asp:ListView>
  80. </form>
  81. </body>
  82. </html>

而我的代码背后:

  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. public partial class test_listview_databind : System.Web.UI.Page
  5. {
  6. public IQueryable<DataModel> lv_GetData()
  7. {
  8. var l = new List<DataModel>();
  9. l.Add(new DataModel() { Id = 1,Description = "Test 1",IsScheduleFollowUp = true,ClientStatusId = 1 });
  10. l.Add(new DataModel() { Id = 2,Description = "Test 2",IsScheduleFollowUp = false,ClientStatusId = 2 });
  11. return l.AsQueryable();
  12. }
  13.  
  14. public void lv_InsertItem()
  15. {
  16. var item = new DataModel();
  17. TryUpdateModel(item);
  18. if (ModelState.IsValid)
  19. {
  20. Response.Write(item.ClientStatusId);
  21. }
  22. }
  23. }

你没有发布你的整个ListView示例,所以我猜测你如何设置它.请告诉我这是否有用以及是否/如何对您有用,因为您的代码看起来可行并且我很好奇导致您的问题的原因.

猜你在找的asp.Net相关文章