0

そのため、コード ビハインドにデータを入力しているグリッドビューがあり、ドロップダウンをリッスンして何を入力するかを確認しています。その部分はうまく機能します。しかし、グリッドビューから行編集イベントを発生させると、データバインド プロセスで NullReferenceException エラーがスローされます。

ここにページがあります:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

            <div id="categories">
                <h1>Categories</h1>
                <asp:DropDownList ID="ddlCategories" 
                    runat="server"
                    OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged"          
                    AutoPostBack="true"></asp:DropDownList>
            </div>
            <div id="products">
                <h1>Products</h1>
                <asp:GridView ID="gvProducts" 
                    runat="server"
                    AutoGenerateColumns="false" 
                    OnRowEditing="gvProducts_RowEditing" 
                    AutoGenerateDeleteButton="True" 
                    AutoGenerateEditButton="True"
                    OnRowCancelingEdit="gvProducts_RowCancelingEdit"
                    OnRowUpdating="gvProducts_RowUpdating">
                    <Columns>
                        <asp:BoundField
                            DataField="Category.Name"
                            HeaderText="Category" />
                        <asp:BoundField
                            Datafield="Name" 
                            HeaderText="Name"/>
                        <asp:BoundField
                            Datafield="Description" 
                            HeaderText="Description"/>
                        <asp:BoundField
                            DataField="Price" 
                            HeaderText="Price"
                            DataFormatString="{0:c}" 
                            HtmlEncode="False"/>
                        <asp:ImageField
                            DataImageUrlField="ImageURL"
                            HeaderText="Picture"></asp:ImageField>
                        <asp:CheckBoxField
                            DataField="Active"
                            Text="Active" 
                            HeaderText="Status"/>
                    </Columns>
                </asp:GridView>
            </div>

        </ContentTemplate>
    </asp:UpdatePanel>

コードビハインドは次のとおりです。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategoryList();
            BindProductList();
        }
    }

    protected void BindCategoryList()
    {
        ddlCategories.DataTextField = "Name";
        ddlCategories.DataValueField = "CategoryID";
        ddlCategories.DataSource = CategoryDB.GetCategories();
        ddlCategories.DataBind();
        ddlCategories.Items.Insert(0, new ListItem(string.Empty));
        ddlCategories.SelectedIndex = 0;
    }

    protected void BindProductList(int categoryID = 0)
    {
        gvProducts.DataSource = ProductDB.GetProductsByCategory(categoryID);
        gvProducts.DataBind();
    }

    protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProductList(Int32.Parse(ddlCategories.SelectedValue));
    }

    protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvProducts.EditIndex = e.NewEditIndex;
        BindProductList(Int32.Parse(ddlCategories.SelectedValue));
    }

エラーは BindProductList() メソッドで発生しますが、gvProducts_RowEditing から呼び出された場合のみです。それ以外の場合は、正常に動作します。デバッグすると、確実に正しい categoryID 値が渡され、DataBind 呼び出しまでエラーがスローされないことがわかります。これは、DataSource() 呼び出しの gvProducts を引き続き見つけることができることを意味します。

何か案は?ありがとう。

編集: これは、categorydb クラスと getcategories メソッドです。

public class CategoryDB
{
    public static List<Category> GetCategories()
    {
        using (var db = new ProductContext())
        {
            return (from c in db.Categories
                    orderby c.Name
                    select c).ToList<Category>();
        }
    }
4

6 に答える 6

1

他の人がすでにあなたの問題を解決しているかもしれませんが、ここで別の提案があります。以前に ddl の SelectedValue 属性に問題があったので、次を試してみてください。

BindProductList(Int32.Parse(ddlCategories.SelectedItem.Value));

あなたは、あなたの関数は他の場所で呼び出されたときに機能すると言いましたよね? ページの読み込み時にのみ機能しますか、それとも選択したインデックスの変更も機能しますか?

好奇心から、なぜ Edit イベントで ddl の ID を使用しているのですか? 編集中の行から派生したカテゴリを使用したいと思います。

于 2013-10-31T20:53:14.700 に答える
1

よくわかりませんが、列ごとにこれが必要だと思います編集する行が欠落していると思います

 <asp:TemplateField headertext="Category.Name">
          <ItemTemplate> <%#Eval("Category.Name")%></ItemTemplate>   
          <EditItemTemplate>
               <asp:TextBox id="txtCategory.Name" runat="server" Enabled="False" text='<%#Eval("Category.Name")%>'/>
          </EditItemTemplate>                
          <FooterTemplate>
         <asp:TextBox ID="Category.Name" runat="server">     </asp:TextBox>           
    </FooterTemplate >
      </asp:TemplateField> 

編集をどのように呼び出しているのかわかりませんが、別の列を提案します

<asp:TemplateField>
         <ItemTemplate>
              <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit"/>       
         </ItemTemplate>
          <EditItemTemplate>
              <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update" />
              <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"/>
              <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete"/>
          </EditItemTemplate>
          <FooterTemplate>
            <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" />
          </FooterTemplate>
      </asp:TemplateField> 
于 2013-10-31T20:02:42.603 に答える
0

あなたのグリッドがUpdatePanelにあることに気付いたので、何か他のことを考えました。以前に発生した問題と同様の問題が発生しているようです (正確ではありませんが、関連しているようです)

更新パネルからグリッドを取り出して試してみてください。うまくいくと思います。私の問題を見つけるために使用した参照は、このリンクhereでした

DataSource が変更され、非同期呼び出しで再バインドしている場合、それらの ID が上書きされる可能性があるため、ScriptManager はコールバックの前の行への参照を失います。特に、データ ソースが変更されている場合。

特にこのような問題に対処する場合は、UpdatePanels は価値がないことがわかりました。それでも使用する場合は、私の質問を見て、問題をどのように解決したかを確認してください。それはあまりきれいではありませんでした.私の状況は動的制御を扱っていました.

于 2013-11-01T14:20:51.240 に答える
0

ここで ListItem として空の値が設定されています。つまり、ドロップダウン アイテムは Text="" および Value="" になります。

ddlCategories.Items.Insert(0, new ListItem(string.Empty));

ddlCategories.SelectedValue が "" である可能性があります。ProductDB.GetProductsByCategory メソッドがレコードを返さない場合、空白の結果セットをバインドし、存在しないインデックスでグリッドを編集モードにしようとします。

それをチェックして、そこで何が起こっているのかについての洞察が得られるかどうかを確認してください.

通常、DropDownLists のプレースホルダー値を次のように設定します。

ddlCategories.Items.Insert(0, new ListItem("Select a category", "0")) 

また

ddlCategories.Items.Insert(0, new ListItem("", "NULL"))

次に、値の解析を行う前に、常にifステートメントを実行して、何かを選択した後にプレースホルダー値が選択されていないことを確認できます有効です。

この場合、常にint.

于 2013-10-31T21:43:22.190 に答える
0

それを見つけた。ImageField列でエラーが発生していました。データベースからの値は null でした。これは、編集しようとすると明らかに問題でした。ImageField をコメントアウトすると正常に動作します。私はそれを処理する方法を考え出さなければなりません。

于 2013-11-01T15:04:42.893 に答える
0

GridViewEditEventArgs.Cancelプロパティを trueに設定することをお勧めします。

于 2013-10-31T19:59:30.217 に答える