Ошибка обновления строки Gridview с Linq, Entity Models, С#

88
8

Привет и спасибо заранее,

Я пытаюсь сделать встроенное редактирование в моем gridview, но я продолжаю получать эту ошибку:

Sys.WebForms.PageRequestManagerServerErrorException: ссылка на объект не установлена в экземпляр объекта.

Раньше я работал над этим, но потом мне пришлось добавить несколько модификаций, таких как изображения, и сделать некоторые граничные поля вместо EditItemTemplate

Интерфейс Asp.net

<asp:GridView ID="GridViewRangeSetup" runat="server" AllowSorting="True" OnSorting="Gridview_Sort"
PagerStyle-Mode="NumericPages" AutoGenerateColumns="false" Width="100%" CssClass="gridView"
OnPageIndexChanging="GridViewRangeSetup_PageIndexChanging" AllowPaging="True"
PageSize="20" DataKeyNames="RangeId" OnRowCommand="GridViewRangeSetup_RowCommand"
OnRowEditing="GridViewRangeSetup_RowEditing" OnRowCancelingEdit="GridViewRangeSetup_CancelEditRow"
OnRowUpdating="GridViewRangeSetup_UpdateRow" OnRowDataBound="GridViewRangeSetup_RowDataBound">
<RowStyle CssClass="rowStyle"></RowStyle>
<HeaderStyle CssClass="headerBar" ForeColor="#ffffff"></HeaderStyle>
<AlternatingRowStyle CssClass="altRow" />
<Columns>
<asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%" HeaderStyle-ForeColor="#f2f2f2"
HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/images/icon_edit.png" CausesValidation="false"
CommandArgument='<%#Eval("RangeId") %>' CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="~/images/icon_update.png"
CausesValidation="false" CommandArgument='<%#Eval("RangeId") %>' CommandName="Update" />
<asp:ImageButton ID="ImageCancel" runat="server" ImageUrl="~/images/icon_cancel.png"
CausesValidation="false" CommandArgument='<%#Eval("RangeId") %>' CommandName="Cancel" />
</EditItemTemplate>
<HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="5%" />
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="Copy" HeaderStyle-Width="3%" HeaderStyle-ForeColor="#f2f2f2"
HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
<ItemTemplate>
<asp:ImageButton ID="imgCopy" runat="server" CausesValidation="false" CommandArgument='<%#Eval("RangeId") %> '
CommandName="Copy" ImageUrl="~/images/icon_copy.png" />
</ItemTemplate>
<HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="3%" />
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Delete" HeaderStyle-Width="3%" HeaderStyle-ForeColor="#f2f2f2"
HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" CausesValidation="false" OnClientClick="return DeleleAlert();"
CommandArgument='<%#Eval("RangeId") %>' CommandName="Remove" ImageUrl="~/images/icon_delete.png" />
</ItemTemplate>
<HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="3%" />
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<%--<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<EditItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="CountryName" DataValueField="CountryId">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State/Province" SortExpression="Region">
<EditItemTemplate>
<asp:DropDownList ID="ddlRegion" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="RegionName" DataValueField="GeographicRegionId">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRegion" runat="server" Text='<%# Bind("RegionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Base/Facility" SortExpression="Facility">
<EditItemTemplate>
<asp:DropDownList ID="ddlFacility" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="BaseName" DataValueField="BaseId">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFacility" runat="server" Text='<%# Bind("BaseName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MapName" HeaderText="Map Name" SortExpression="MapName" />
<asp:TemplateField HeaderText="Map">
<HeaderStyle HorizontalAlign="center" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink_Map1" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"MapPath") %>'
Text="">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"MapPath") %>'
Width="50px" Height="50px" />
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LowLat" HeaderText="Low Latitude" SortExpression="LowLat" />
<asp:BoundField DataField="LowLong" HeaderText="Low Longitude" SortExpression="LowLong" />
<asp:BoundField DataField="HighLat" HeaderText="High Latitude" SortExpression="HighLat" />
<asp:BoundField DataField="HighLong" HeaderText="High Longitude" SortExpression="HighLong" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Button ID="RangeSetup_Status" CssClass="page-btn blue" CausesValidation="false"
CommandArgument='<%#Eval("RangeId") %> ' runat="server" Text="Status" OnClick="btnRangeStatus_Click">
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

С# backend

protected void GridViewRangeSetup_UpdateRow(object sender, GridViewUpdateEventArgs e)
{

WISSModel.WISSEntities context = new WISSModel.WISSEntities();
//TextBox txtDescription = (TextBox)GridViewRangeSetup.Rows[e.RowIndex].Cells[3].Controls[0];
GridViewRow row = GridViewRangeSetup.Rows[e.RowIndex];
//TextBox txtDescription = row.FindControl("txtDescription") as TextBox;
//DropDownList ddlCountry = (DropDownList)GridViewRangeSetup.Rows[e.RowIndex].Cells[4].Controls[0];
DropDownList ddlCountry = row.FindControl("ddlCountry") as DropDownList;
DropDownList ddlRegion = row.FindControl("ddlRegion") as DropDownList;
DropDownList ddlFacility = row.FindControl("ddlFacility") as DropDownList;
//TextBox mapName = row.FindControl("MapName") as TextBox;
//TextBox lowLat = row.FindControl("LowLat") as TextBox;
//TextBox lowLong = row.FindControl("LowLong") as TextBox;
//TextBox highLat = row.FindControl("HighLat") as TextBox;
//TextBox highLong = row.FindControl("HighLong") as TextBox;

int rangeID = Convert.ToInt32(GridViewRangeSetup.DataKeys[e.RowIndex].Values[0].ToString());

if (rangeID > 0)
{
var rangeEdit = (from r in context.Ranges
where r.RangeId == rangeID
select r).SingleOrDefault();

//rangeEdit.Description = txtDescription.Text.Trim();
rangeEdit.Description = e.NewValues["Description"].ToString();
rangeEdit.CountryId = Convert.ToInt32(ddlCountry.SelectedValue);
rangeEdit.GeographicRegionId = Convert.ToInt32(ddlRegion.SelectedValue);
rangeEdit.BaseId = Convert.ToInt32(ddlFacility.SelectedValue);
rangeEdit.RangeMap.MapName = e.NewValues["MapName"].ToString();//mapName.Text.Trim();
rangeEdit.RangeMap.LowLat = Convert.ToDouble(e.NewValues["LowLat"].ToString());//Convert.ToDouble(lowLat.Text.Trim());
rangeEdit.RangeMap.LowLong = Convert.ToDouble(e.NewValues["LowLong"].ToString());//Convert.ToDouble(lowLong.Text.Trim());
rangeEdit.RangeMap.HighLat = Convert.ToDouble(e.NewValues["HighLat"].ToString());//Convert.ToDouble(highLat.Text.Trim());
rangeEdit.RangeMap.HighLong = Convert.ToDouble(e.NewValues["HighLong"].ToString());//Convert.ToDouble(highLong.Text.Trim());

context.SaveChanges();
}
else // copy row
{
WISSModel.Range range2 = new WISSModel.Range
{
//Description = txtDescription.Text.Trim(),
CountryId = Convert.ToInt32(ddlCountry.SelectedValue),
GeographicRegionId = Convert.ToInt32(ddlRegion.SelectedValue),
BaseId = Convert.ToInt32(ddlFacility.SelectedValue),
isDeleted = false

};
context.AddObject("Ranges", range2);
context.SaveChanges();
}

GridViewRangeSetup.EditIndex = -1;
LoadData();

}

спросил(а) 2021-01-25T16:35:22+03:00 4 месяца, 3 недели назад
1
Решение
116

попробуйте ссылку ниже, там код для редактирования сетки:

http://www.codeproject.com/Articles/37207/Editable-Gridview-with-Textbox-CheckBox-Radio-Butt

ответил(а) 2021-01-25T16:35:22+03:00 4 месяца, 3 недели назад
Ваш ответ
Введите минимум 50 символов
Чтобы , пожалуйста,
Выберите тему жалобы:

Другая проблема