Я использую c # asp-net 4 и ef4.
У меня есть запрос 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
Я понимаю, что анонимный тип доступен только для чтения, и в данный момент я беру значения из GridView, используя свойства FindControl и Cell для редактируемой строки.
// 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 из анонимного типа, без поиска свойства ячейки или метки?
Было бы здорово иметь пример кода. Спасибо, ребята, за вашу помощь, как обычно.