使用下拉菜单创建动态网格

时间:2011-08-06 12:36:23

标签: c# asp.net

我想创建一个动态网格视图,第一行作为下拉单击编辑按钮。我对如何开始没有任何想法。你能帮忙吗?我已经浏览了一些articals,发现使用我们可以实现的InstantiateIn方法。

public class CreateItemTemplate : ITemplate
    {
        //Field to store the ListItemType value
        private ListItemType myListItemType;

        public CreateItemTemplate(ListItemType item)
        {
            myListItemType = item;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            //Code to create the ItemTemplate and its field.
            if (myListItemType == ListItemType.Item)
            {
                TextBox txtCashCheque = new TextBox();
                container.Controls.Add(txtCashCheque);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如果要在单个页面上显示此内容,则不应创建服务器控件。

使用网格的TemplateField。

注意:如果您使用AutoGenerateColumns = true,只需将该列添加到网格标记即可。它将首先添加。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:DropDownList id="someId" runat="server">
        <asp:ListItem Text="One" />
                    <asp:ListItem Text="twO" />
         </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

您可能需要提供有关此下拉列表的更多信息(是否需要默认值?)。 根据您的需要,您可以在标记中执行此操作,或者,您可能需要使用网格事件。

  • 布赖恩 更新:添加事件处理程序

如果在网格中设置onrowcreated =“GridView1_RowCreated”

    <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="true" 
        onrowcreated="GridView1_RowCreated">

并在你的代码中执行此操作:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var dropdown = e.Row.FindControl("someId") as DropDownList;
            //dropdown.DataSource= <>; bind it
            //dropdown.SelectedValue =<>"; / set value how you would 
        }
    }

您可以操纵创建的下拉广告。 如果它无法控制每个单元格中的控件:e.Row.Cells [[index]]。FindControl(“”someId“”)

相关问题