当我尝试将项目添加到 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>
 <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>
让我知道我在这里做错了什么,并提前感谢您的回答!