我在GridView中的TemplateField中有一个下拉列表。
我想动态添加列表项,并编写代码来处理索引更改时的情况。既然我不能直接引用TemplateField中的DropDownList,那么我该如何操作这个列表呢?
下面是我的代码:
<asp:TemplateField HeaderText="Transfer Location" Visible="false">
<EditItemTemplate>
<asp:DropDownList ID="ddlTransferLocation" runat="server" ></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>发布于 2011-05-27 07:18:43
如果我没弄错你想做什么,你可以像这样把项目添加到下拉列表中:
foreach (GridViewRow currentRow in gvMyGrid.Rows)
{
DropDownList myDropDown = (currentRow.FindControl("ddlTransferLocation") as DropDownList);
if (myDropDown != null)
{
myDropDown.Items.Add(new ListItem("some text", "a value"));
}
}然后,如果您的意思是处理DropDownList的索引更改,则只需向控件添加一个事件处理程序:
<asp:DropDownList ID="ddlTransferLocation" runat="server" OnSelectedIndexChanged="ddlTransferLocation_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>然后,在该事件处理程序中,您可以使用(sender as DropDownList)从其中获取所需的任何内容:
protected void ddlTransferLocation_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList myDropDown = (sender as DropDownList);
if (myDropDown != null) // do something
{
}
}https://stackoverflow.com/questions/6143586
复制相似问题