【问题标题】:How to Edit a Row in a GridView bounded to a LINQ Query with Anonymouse Type如何在绑定到匿名类型的 LINQ 查询的 GridView 中编辑行
【发布时间】:2011-03-15 10:35:02
【问题描述】:

我使用 c# asp-net 4 和 ef4。

我有一个创建 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
                                     };

还有一个绑定到 LINQ 查询的 GridView:

<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 置于编辑模式时,我需要更改一行。

我的问题是:我无法看到 GridView 的 DataItem,因此无法检索用户在 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 的值?

如果有代码示例就好了。一如既往地感谢大家的帮助。

【问题讨论】:

    标签: c# asp.net linq entity-framework gridview


    【解决方案1】:

    创建一个看起来像您拥有的匿名类型的“ViewModel”类,然后改用它。

    使用具体类,您将能够将对象投射到 ViewModel 中并执行您想做的任何事情。

    【讨论】:

    • 我不使用 MVC 而是使用 WebForms
    • 好吧,我称我为“ViewModel”只是因为它是一个只用于演示的类。但它只是一个普通的类,而不是你拥有的匿名类型。而不是new { ... } 使用new MyPresentationClass { ... },您将能够在您的事件处理程序中转换DataItem 对象。
    猜你喜欢
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多