使用Linq,实体模型,C#更新Gridview行时出错

时间:2013-10-04 12:22:05

标签: c# asp.net linq gridview nullreferenceexception

嗨,谢谢你,

我正在尝试在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>

C#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();

}

1 个答案:

答案 0 :(得分:0)