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).控件在标记内定义如下……

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

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

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

ListView.InsertMethod:

public void ListView_InsertMethod(int ID) {

  Model model = this.DbContext.Models.Create();
  if (this.TryUpdateModel(model)) {
    this.DbContext.SaveChanges();
    this.ListView.DataBind();
  }

}

Model类:

public class Model{

  public Int32 ID { get; set; }
  public String Description { get; set; }
  public Boolean IsScheduleFollowUp { get; set; }
  public Nullable<Int32> ClientStatusID { get; set; }

}

EditItemTemplate:

<EditItemTemplate>
  <tr>
    <td>
      <asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
    </td>
    <td>
      <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
    </td>
    <td>
      <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" SelectedValue="<%#: BindItem.ClientStatusID %>" />
    </td>
    <td>
      <asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update" Text="Update" />
      <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel" Text="Cancel" />
    </td>
  </tr>
</EditItemTemplate>

InsertItemTemplate:

<InsertItemTemplate>
  <tr>
    <td>
      <asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
    </td>
    <td>
      <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
    </td>
    <td>
      <agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" />
    </td>
    <td>
      <asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert" Text="Add" />
    </td>
  </tr>
</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中的新方法

谢谢.

解决方法

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

这是我的页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listview-databind.aspx.cs"
    Inherits="test_listview_databind" Debug="true" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ListView ID="lv" runat="server" ItemType="DataModel" DataKeyNames="Id" SelectMethod="lv_GetData"
            InsertItemPosition="FirstItem" InsertMethod="lv_InsertItem" UpdateMethod="lv_UpdateItem">
            <LayoutTemplate>
                <table>
                    <tr id="itemPlaceholder" runat="server"></tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Literal ID="Description" runat="server" Text="<%# Item.Description %>" />
                    </td>
                    <td>
                        <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# Item.IsScheduleFollowUp %>" />
                    </td>
                    <td>
                        <asp:Literal ID="ClientStatusId" runat="server" />
                    </td>
                    <td>
                        <asp:Button ID="Edit" runat="server" ClientIDMode="Static" CommandName="Edit"
                            Text="Edit" />
                    </td>
                </tr>
            </ItemTemplate>
            <InsertItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="Description" runat="server" Text="<%# BindItem.Description %>" />
                    </td>
                    <td>
                        <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
                    </td>
                    <td>
                        <asp:DropDownList ID="ClientStatusId" runat="server" SelectedValue="<%# BindItem.ClientStatusId %>">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert"
                            Text="Add" />
                        <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel"
                            Text="Cancel" />
                    </td>
                </tr>
            </InsertItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="Description" runat="server" Text="<%# BindItem.Description %>" />
                    </td>
                    <td>
                        <asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
                    </td>
                    <td>
                        <asp:DropDownList ID="ClientStatusId" runat="server" SelectedValue="<%# BindItem.ClientStatusId %>">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update"
                            Text="Update" />
                        <asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel"
                            Text="Cancel" />
                    </td>
                </tr>
            </EditItemTemplate>
        </asp:ListView>
    </form>
</body>
</html>

而我的代码背后:

using System.Collections.Generic;
using System.Linq;

public partial class test_listview_databind : System.Web.UI.Page
{
    public IQueryable<DataModel> lv_GetData()
    {
        var l = new List<DataModel>();
        l.Add(new DataModel() { Id = 1,Description = "Test 1",IsScheduleFollowUp = true,ClientStatusId = 1 });
        l.Add(new DataModel() { Id = 2,Description = "Test 2",IsScheduleFollowUp = false,ClientStatusId = 2 });
        return l.AsQueryable();
    }

    public void lv_InsertItem()
    {
        var item = new DataModel();
        TryUpdateModel(item);
        if (ModelState.IsValid)
        {
            Response.Write(item.ClientStatusId);
        }
    }
}

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

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...