Gridview 行编辑 - 动态绑定到 DropDownList [英] Gridview row editing - dynamic binding to a DropDownList

查看:24
本文介绍了Gridview 行编辑 - 动态绑定到 DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 ASP.NET 3.5 GridView 在显示时将选定的值显示为字符串,并显示一个 DropDownList 以允许我在编辑时从给定的选项列表中选择一个值.看起来够简单吗?

I'm trying to get an ASP.NET 3.5 GridView to show a selected value as string when being displayed, and to show a DropDownList to allow me to pick a value from a given list of options when being edited. Seems simple enough?

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

My gridview looks like this (simplified):

<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>

网格在未处于编辑模式时显示正常 - 所选的 PBX 类型在 asp:Label 控件中显示其值.毫不奇怪.

The grid gets displayed OK when not in editing mode - the selected PBX type shows its value in the asp:Label control. No surprise there.

我将 DropDownList 的值列表加载到表单的 OnLoad 事件中名为 _pbxTypes 的本地成员中.我验证了这一点 - 它有效,值就在那里.

I load the list of values for the DropDownList into a local member called _pbxTypes in the OnLoad event of the form. I verified this - it works, the values are there.

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

Now my challenge is: when the grid goes into editing mode for a particular row, I need to bind the list of PBX's stored in _pbxTypes.

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

Simple enough, I thought - just grab the drop down list object in the RowEditing event and attach the list:

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 不存在(或找不到).

Trouble is - I never get anything back from the FindControl call - seems like the ddlPBXTypeNS doesn't exist (or can't be found).

我错过了什么??一定是非常愚蠢的事情......但到目前为止,我所有的谷歌搜索、阅读 GridView 控件以及询问好友都没有帮助.

What am I missing?? Must be something really stupid.... but so far, all my Googling, reading up on GridView controls, and asking buddies hasn't helped.

谁能发现缺失的链接?;-)

Who can spot the missing link? ;-)

推荐答案

很简单...你做错了,因为那个事件控件不存在:

Quite easy... You're doing it wrong, because by that event the control is not there:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && 
        (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
    { 
        // Here you will get the Control you need like:
        DropDownList dl = (DropDownList)e.Row.FindControl("ddlPBXTypeNS");
    }
}

也就是说,它只对 DataRow(包含数据的实际行)有效,如果它处于 Edit 模式......因为你只编辑一个一次排.e.Row.FindControl("ddlPBXTypeNS") 只会找到你想要的控件.

That is, it will only be valid for a DataRow (the actually row with data), and if it's in Edit mode... because you only edit one row at a time. The e.Row.FindControl("ddlPBXTypeNS") will only find the control that you want.

这篇关于Gridview 行编辑 - 动态绑定到 DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆