gridview的行编辑 - 动态绑定到一个DropDownList [英] Gridview row editing - dynamic binding to a 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的值列表加载到一个名为 _pbxTypes
中的的的OnLoad
事件本地成员形成。我证实了这一 - 它的工作原理,值有
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
(实际数据行),如果它在修改模式..因为你只有一次编辑一行。在 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屋!