使用后面的代码在Gridview中为DropDownList设置数据源时获取错误

时间:2017-07-18 07:33:22

标签: javascript c# asp.net gridview

我有一个GridView来显示我的产品,我还使用EditButton和DeleteButton在GridView中更新和删除我的产品。当我点击EditButton时,我的gridview有一个DropDownList来显示我的ProductType但是我收到这条消息的错误:

  

对象引用未设置为对象的实例

此图片将显示我收到该错误的位置:

enter image description here

这是我用来为我的DropDownList填充数据源的方法:

enter image description here

感谢您的帮助!



<asp:GridView ID="gridviewSanPham" runat="server" DataKeyNames="ID" AllowPaging="True" PageSize="4" AutoGenerateColumns="False" OnPageIndexChanging="gridviewSanPham_PageIndexChanging" OnRowCancelingEdit="gridviewSanPham_RowCancelingEdit" OnRowDeleting="gridviewSanPham_RowDeleting" OnRowEditing="gridviewSanPham_RowEditing" OnRowUpdating="gridviewSanPham_RowUpdating" OnRowDataBound="gridviewSanPham_RowDataBound">
        <Columns>
            <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="true" ShowCancelButton="true" />
            <asp:BoundField DataField="ID" HeaderText="Mã sản phẩm" ReadOnly="true"/>

            <asp:TemplateField HeaderText="Mã Loại">
                <ItemTemplate>
                    <%# Eval("TypeID") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label runat="server" ID="lblTypeID" Text='<%# Eval("TypeID") %>'></asp:Label>
                    <asp:DropDownList runat="server" ID="cboTypeID" ></asp:DropDownList> 
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Tên SP">
                <ItemTemplate>
                    <%# Eval("Name") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtName" Text='<%# Eval("Name") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Giá">
                <ItemTemplate>
                    <%# Eval("Price") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtPrice" Text='<%# Eval("Price") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mô tả">
                <ItemTemplate>
                    <%# Eval("Description") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtDescription" Text='<%# Eval("Description") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hình"> 
                <ItemTemplate>
                    <%# Eval("Image") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label runat="server" ID="lblImage" Text='<%# Eval("Image") %>'></asp:Label>
                    <asp:FileUpload runat="server" ID="uploadImage" />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
&#13;
&#13;
&#13;

这是我的aspx代码

2 个答案:

答案 0 :(得分:0)

Dopdownlist绑定代码放入if ((e.Row.RowState & DataControlRowState.Edit) > 0) RowDataBound事件中的protected void gridviewSanPham_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // check if dropdownlist in edittemplate if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList drop = (DropDownList)e.Row.FindControl("cboTypeID"); drop.DataSource = FillLoaiSP(); drop.DataBind(); } } }

{{1}}

答案 1 :(得分:-1)

它总是会抛出错误,因为您试图在gridviewSanPham_RowDataBound方法中找到下拉列表。只是尝试在gridviewSanPham_RowEditing方法中找到下拉列表。因为在RowDataBound事件中它将遍历所有行,包括页眉和页脚,因此在页眉和页脚行的情况下它将抛出空对象引用错误。

在编辑操作的情况下,您的下拉列表也会出现,因此您可以在gridviewSanPham_RowDataBound方法中为数据行和操作添加过滤器。但强烈建议您使用gridviewSanPham_RowEditing方法绑定下拉列表。