【发布时间】: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