2011-03-15 56 views
1

我使用c#asp-net 4和ef4。如何编辑一个GridView中的行与Anonymouse绑定到LINQ查询类型

我有一个创建一个Anonymuse类型LINQ查询:

var queryRelatedContentsCategories = from cnt in context.CmsContents 
            from category in cnt.CmsCategories 
            join c in context.CmsCategories 
            on cnt.CategoryId equals c.CategoryId 
            join t in context.CmsTypes 
            on cnt.TypeContent equals t.TypeContent 
            join m in context.CmsModes 
            on cnt.ModeContent equals m.ModeContent 
            select new 
            { 
            cnt.ContentId, 
            ContentTitle = cnt.Title, 
            ContentCategoryTitle = c.Title, 
            ContentCategoryTitleAssociation = category.Title, 
            ContentIsPublished = cnt.IsPublished, 
            TypeContentDescription = t.Description, 
            ModeContentDescription = m.Description 
            }; 

而且一个GridView一定到LINQ查询:

<asp:GridView ID="uxManageRelatedContentsCategories" runat="server" AutoGenerateColumns="False" 
     OnRowEditing="uxManageRelatedContentsCategories_RowEditing" 
     OnRowCancelingEdit="uxManageRelatedContentsCategories_RowCancelingEdit" 
     onrowupdating="uxManageRelatedContentsCategories_RowUpdating"> 
     <Columns> 
      <asp:CommandField ShowEditButton="True" /> 
      <asp:BoundField DataField="ContentId" HeaderText="ContentId" ReadOnly="True" /> 
      <asp:BoundField DataField="ContentTitle" HeaderText="ContentTitle" ReadOnly="True" /> 
      <asp:BoundField DataField="ContentCategoryTitle" HeaderText="CategoryContentTitle" 
       ReadOnly="True" /> 
      <asp:TemplateField HeaderText="ContentCategoryTitleAssociation"> 
       <ItemTemplate> 
        <asp:Label ID="uxContentCategoryTitleAssociationDispalyer" runat="server" Text='<%# Eval("ContentCategoryTitleAssociation") %>'></asp:Label> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:Label ID="uxContentCategoryTitleAssociationCurrentDispalyer" runat="server" Text='<%# Eval("ContentCategoryTitleAssociation") %>'></asp:Label> 

        <asp:ListBox ID="uxCategoryIdNewSelector" runat="server" DataSourceID="uxEntityDataSourceListCategoriesPublished" 
         DataTextField="Title" DataValueField="CategoryId"></asp:ListBox> 
        <act:ListSearchExtender ID="uxCategoryIdNewSelector_ListSearchExtender" runat="server" 
         Enabled="True" IsSorted="True" QueryPattern="Contains" TargetControlID="uxCategoryIdNewSelector"> 
        </act:ListSearchExtender> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidatorListNewCategory" runat="server" 
         ErrorMessage="Title field is required." ControlToValidate="uxCategoryIdNewSelector">*</asp:RequiredFieldValidator> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:CheckBoxField DataField="ContentIsPublished" HeaderText="ContentIsPublished" 
       ReadOnly="True" /> 
      <asp:BoundField DataField="TypeContentDescription" HeaderText="TypeContentDescription" 
       ReadOnly="True" /> 
      <asp:BoundField DataField="ModeContentDescription" HeaderText="ModeContentDescription" 
       ReadOnly="True" /> 
     </Columns> 
     <EmptyDataTemplate> 
      Item not found. 
     </EmptyDataTemplate> 
    </asp:GridView> 

到现在为止一切都工作正常。

当用户将GridView置于编辑模式时,我需要更改一行。

我的问题是:我无法看到的DataItem为GridView,所以我不能取回在GridView控件事件的用户插入的值:

onrowupdating

我明白那个匿名类型是只读的,此刻我从正在编辑的行中使用FindControl和Cell属性从GridView获取值。

// Retrieve the row being edited. 
GridViewRow row = uxManageRelatedContentsCategories.Rows[uxManageRelatedContentsCategories.EditIndex]; 
// Retrieve Value in WebControls 
ListBox listCategories = (ListBox)row.FindControl("uxCategoryIdNewSelector"); 
int myContentId = Convert.ToInt32(row.Cells[1].Text); 
int myCategoryIdSelected = Convert.ToInt32(listCategories.SelectedValue); 
Label myCurrentCategoryTitleLink = (Label)row.FindControl("uxContentCategoryTitleAssociationCurrentDispalyer"); 

我的问题:

如何检索值从匿名类型一定到GridView控件不找小区物业或标签吗?

会很棒有一个代码示例。谢谢你像往常一样帮助你的帮助。

回答

0

创建一个类似于您所拥有的匿名类型的“ViewModel”类,然后使用它。

使用具体的类,您将能够将该对象投射到ViewModel中并执行任何您想要执行的操作。

+0

谢谢你能提供一个代码样本? – GibboK 2011-03-15 10:44:54

+0

我不使用MVC,但WebForms – GibboK 2011-03-15 10:45:20

+0

嗯,我只是因为它只用于演示文稿中的类而调用“ViewModel”。但它只是一个普通的类,而不是你拥有的匿名类型。而不是'new {...}'使用'new MyPresentationClass {...}',您将能够在事件处理程序中投射'DataItem'对象。 – 2011-03-15 10:50:38

相关问题