使用FindControl在Gridview EditItemTemplate中查找DropDownList

时间:2012-05-18 15:25:05

标签: vb.net gridview itemtemplate

当我尝试将项添加到我的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>

让我知道我在这里做错了什么,并提前感谢您的回答!

0 个答案:

没有答案