asp.net – Gridview行编辑 – 动态绑定到DropDownList

我正在尝试获取一个ASP.NET 3.5 GridView在显示显示选定的值作为字符串,并显示一个DropDownList,以允许我在编辑时从给定的选项列表中选择一个值。看起来很简单?

我的gridview看起来像这样(简化):

<asp:GridView ID="grvSecondaryLocations" runat="server" 
              DataKeyNames="ID" OnInit="grvSecondaryLocations_Init" 
              OnRowCommand="grvSecondaryLocations_RowCommand" 
              OnRowCancelingEdit="grvSecondaryLocations_RowCancelingEdit"
              OnRowDeleting="grvSecondaryLocations_RowDeleting"
              OnRowEditing="grvSecondaryLocations_RowEditing" 
              OnRowUpdating="grvSecondaryLocations_RowUpdating"  >
<Columns>
    <asp:TemplateField>
         <ItemTemplate>
              <asp:Label ID="lblPbxTypeCaption" runat="server" 
                                 Text='<%# Eval("PBXTypeCaptionValue") %>' />
         </ItemTemplate>
         <EditItemTemplate>
                      <asp:DropDownList ID="ddlPBXTypeNS" runat="server" 
                               Width="200px" 
                               DataTextField="CaptionValue" 
                               DataValueField="OID" />
         </EditItemTemplate>
    </asp:TemplateField>
</asp:GridView>

当不在编辑模式时,网格显示为OK – 所选PBX类型在asp:Label控件中显示其值。没有惊喜

在表单的OnLoad事件中,将DropDownList的值加载到名为_pbxTypes的本地成员中。我验证了这个 – 它的作品,价值在那里。

现在我的挑战是:当网格进入特定行的编辑模式时,我需要绑定存储在_pbxTypes中的PBX列表。

很简单,我想 – 只需在RowEditing事件中抓住下拉列表对象并附加列表:

protected void grvSecondaryLocations_RowEditing(object sender,GridViewEditEventArgs e)
{
    grvSecondaryLocations.EditIndex = e.NewEditIndex;

    GridViewRow editingRow = grvSecondaryLocations.Rows[e.NewEditIndex];

    DropDownList ddlPbx = (editingRow.FindControl("ddlPBXTypeNS") as DropDownList);
    if (ddlPbx != null)
    {
        ddlPbx.DataSource = _pbxTypes;
        ddlPbx.DataBind();
    }

    .... (more stuff)
}

麻烦是 – 我从来没有得到任何东西从FindControl调用 – 似乎ddlPBXTypeNS不存在(或不能找到)。

我还缺少什么?必须是非常愚蠢的东西….但到目前为止,我所有的谷歌搜索,阅读GridView控件,并询问好友没有帮助。

谁能找到缺少的链接

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...