加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 18:41:53 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试获取一个ASP.NET 3.5 GridView在显示时显示选定的值作为字符串,并显示一个DropDownList,以允许我在编辑时从给定的选项列表中选择一个值。看起来很简单? 我的gridview看起来像这样(简化): asp:GridView ID="grvSecondaryLocations" runat="serv
我正在尝试获取一个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控件,并询问好友没有帮助。

谁能找到缺少的链接?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读