2011-08-06 27 views
0

我想创建一个动态网格视图,第一行为点击编辑按钮时的下拉菜单。我对如何开始没有任何想法。你能帮忙吗?我已经通过一些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); 
      } 
     } 
    } 
+0

这是针对页面上单个网格的实例,还是你创建一个可重用的控件? –

+0

单个实例 – premg

回答

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> 

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

  • 布莱恩 UPDATE:添加事件处理程序

如果您在网格

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

设置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“”)

+0

在这种情况下,整个列将变为右下角?我正在使用AutoGenerateColumns = true。只有该网格才会填充可数据表格 – premg

+0

在这种情况下,您将添加一个仅有一个下拉列的新列。添加事件处理程序示例 –