mar*_*c_s 23 asp.net gridview dynamic-binding drop-down-menu
我正在尝试让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>
Run Code Online (Sandbox Code Playgroud)
不处于编辑模式时,网格显示OK - 所选PBX类型在asp:Label控件中显示其值.这并不奇怪.
我将DropDownList的值列表加载到窗体事件中调用的本地成员_pbxTypes
中OnLoad
.我验证了这一点 - 它有效,值得存在.
现在我的挑战是:当网格进入特定行的编辑模式时,我需要绑定存储在其中的PBX列表_pbxTypes
.
很简单,我想 - 只需抓住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)
}
Run Code Online (Sandbox Code Playgroud)
麻烦的是 - 我从来没有从FindControl
电话中得到任何回报- 似乎ddlPBXTypeNS
不存在(或无法找到).
我错过了什么?必须是真正愚蠢的东西......但到目前为止,我所有的谷歌搜索,阅读GridView控件,并询问好友没有帮助.
谁能发现缺失的链接?;-)
bal*_*dre 26
很容易......你做错了,因为在那个事件中控制不存在:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,它只对a DataRow
(实际的数据行)有效,如果它处于编辑模式...因为你一次只能编辑一行.将e.Row.FindControl("ddlPBXTypeNS")
只能找到您想要的控制.
归档时间: |
|
查看次数: |
145159 次 |
最近记录: |