1

DropDownList の新しいインスタンスに項目を追加しようとすると、editPriorityDropDown で「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というメッセージが表示されます。私の最善の推測は、FindControl が DropDownList を見つけられないということです。FindControl の到達範囲から何らかの形で逃れたと思いますが、別の DropDown に対してまったく同じことを行いますが、Page_Load で正常に動作します。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then

    End If

    Dim PriorityDropDown As DropDownList = TryCast(gvProduct.HeaderRow.FindControl("ddlOrderPriorityHeader"), DropDownList)

    Dim index As Integer = 1
    While index <= gvProduct.Rows.Count
        Dim newItem As New ListItem
        newItem.Value = index
        newItem.Text = index.ToString

        PriorityDropDown.Items.Add(newItem)

        index = index + 1
    End While

    '' check login seesion
    'Functions.authentication_check()

End Sub

しかし、後で別のサブルーチンで同じことをしようとすると、エラーになります。

Protected Sub PopulateOrderPriority()
    If ViewState("SelectedRowIndex") > -1 Then
        'Response.Write(" Index: " & ViewState("SelectedRowIndex"))
        Dim editPriorityDropDown As DropDownList = TryCast(Me.gvProduct.Rows(ViewState("SelectedRowIndex")).FindControl("ddlOrderPriorityEdit"), DropDownList)

        Dim index As Integer = 1
        While index <= gvProduct.Rows.Count
            Dim newItem As New ListItem
            newItem.Value = index
            newItem.Text = index.ToString

            editPriorityDropDown.Items.Add(index)

            index = index + 1
        End While
    End If
End Sub

これは、関連するマークアップのほんの一部です。

<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" DataKeyNames="Product_ID"
    DataSourceID="sdsProduct" Caption="Products" CaptionAlign="Left" EnableModelValidation="True">
    <Columns>
        <asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="lnkbtnUpdate" runat="server" CausesValidation="True" CommandName="UpdateProd"
                    Text="Update" ValidationGroup="vgUpdateProduct"></asp:LinkButton>
                &#160;<asp:LinkButton ID="lnkbtnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <HeaderTemplate>
                <asp:Button ID="lnkbtnInsertHeader" runat="server" CommandName="InsertHeader" Text="Insert"
                    ValidationGroup="vgHeaderProduct" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="lnkbtnEdit" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Order Priority" SortExpression="Order_Priority">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlOrderPriorityEdit" runat="server">
                </asp:DropDownList>
                <%--<asp:DropDownList ID="ddlOrderPriorityEdit" runat="server" SelectedValue='<%# Bind("Product_Priority") %>'
                    DataSourceID="sdsProductActive" DataTextField="Product_Priority"
                    DataValueField="Product_ID">
                </asp:DropDownList>--%>
            </EditItemTemplate>
            <HeaderTemplate>
                Order Priority<br />
                <asp:DropDownList ID="ddlOrderPriorityHeader" runat="server">
                </asp:DropDownList>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:DropDownList ID="ddlOrderPriorityRead" runat="server" Enabled="false">
                </asp:DropDownList>
                <%--<asp:DropDownList ID="ddlOrderPriorityRead" runat="server" Enabled="false" SelectedValue='<%# Bind("Product_Type") %>'
                    DataSourceID="sdsProductActive" DataTextField="Product_Priority"
                    DataValueField="Product_ID">
                </asp:DropDownList>--%>
            </ItemTemplate>
        </asp:TemplateField>

ここで私が間違っていることを教えてください。あなたの答えに感謝します!

4

0 に答える 0