2013-10-04 52 views
0

Hi和感謝提前,錯誤更新的GridView行使用LINQ,實體模型,C#

我試圖做一個內聯編輯在我的GridView的,但我不斷收到此錯誤:

Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

我有這個工作之前,但後來我不得不加做一些修改,如圖像,並做一些綁定列,而不是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#後端

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

} 
+0

哪一行,你得到的錯誤? – Nirmal

+0

@Nirmal在Firefox上使用Firebug,它不會給我錯誤發生在我的C#代碼的行。 – Neo84

+0

@Nirmal等進一步測試,似乎我在編輯C#中的BoundField(s)時遇到了麻煩。 – Neo84

回答

0

嘗試下面的鏈接,在那裏網格視圖編輯代碼:

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

+0

謝謝,我只需要在文本框中添加「Text ='<%#Eval(」Description「)%>''等內容。但現在我得到這個錯誤** Sys.WebForms.PageRequestManagerServerErrorException:Sys.WebForms.PageRequestManagerServerErrorException:處理服務器上的請求時發生未知的錯誤。從服務器返回的狀態代碼是:0 ** – Neo84

+0

歡迎您。如果您的問題已使用此鏈接解決,那麼您可以選擇我的答案爲正確的答案。 – Nirmal

+0

酷np ...如果你發現如何做與BoundField相同的功能,請讓我知道。 – Neo84