我使用c#asp-net 4和ef4。
我有一个LINQ查询,它创建一个Anonymuse类型:
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置于编辑模式时,我需要更改一行。
我的问题是:我无法看到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的值而不查找单元格属性或标签?
很棒的代码示例。谢谢你们照常提供的帮助。
答案 0 :(得分:0)
创建一个看起来像您拥有的匿名类型的“ViewModel”类,并使用它。
使用具体的类,您将能够将对象转换为ViewModel并执行您想要执行的任何操作。