在转发器内动态创建控件

时间:2012-10-31 12:20:31

标签: c# asp.net

我在转发器中有2个下拉控制器,我必须用按钮单击重复这些控制器如何才能实现它?

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
{ 
    TextBox t = new TextBox(); 
    t.ID = e.Item.ItemIndex.ToString(); 
    e.Item.Controls.Add(t); 
} 

这是正确的方法,但我怎样才能找到转发器内的按钮并将其触发。

2 个答案:

答案 0 :(得分:0)

将DropDownLists和Button控件添加到转发器中的PlaceHolder控件:

<asp:Repeater ID="Repeater1" runat="server" EnableViewState="true"
            onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Text="one"></asp:ListItem>
                    <asp:ListItem Text="two"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Text="three"></asp:ListItem>
                    <asp:ListItem Text="four"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:Button ID="Button1" runat="server" UseSubmitBehavior="false" Text="Button" CommandName="btn" />
                </asp:PlaceHolder>
            </ItemTemplate>
        </asp:Repeater>

在转发器的ItemCommand事件中,从按钮单击检查CommandName,然后创建&amp;将动态下拉列表添加到占位符:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
   if (e.CommandName == "btn")
   {
       DropDownList ddl = new DropDownList();
       ddl.ID = "DropDownList1";
       ddl.DataSource = new string[] { "one", "two" };
       ddl.DataBind();

       // your second dropdown would be created here in the same way
       pl.Controls.Add(ddl);
   }
}

要连接SelectedIndexChanged事件还有很多工作要做,但这应该可以让你开始。

答案 1 :(得分:0)

我们可以使用占位符控件从代码后面添加动态控件,如Textbox,Radiobuttonlist,Checkbox List insde repeater control。

Dynamic control inside Repeater using placeholder in Asp.Net(c#)?

protected void rptPrint_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)         
  {

    string options = (HiddenField)e.Item.FindControl("hfAnswer")).Value;

    string type = ((HiddenField)e.Item.FindControl("hfType")).Value;
    Label lblquestion = ((Label)e.Item.FindControl("LblQuestion"));
    PlaceHolder phRow = (PlaceHolder)e.Item.FindControl("phRow");

    if (type == "Text")
    {
      TextBox txtAnswer = new TextBox();
      phRow.Controls.Add(txtAnswer);
    }


    else if (type == "Check")
    {
      CheckBoxList chklist = new CheckBoxList();
      chklist.RepeatDirection = RepeatDirection.Horizontal;
      chklist.Font.Italic = true;

      chklist.RepeatColumns = 4;

      foreach (string option in options.Split(','))
      {

        ListItem items = new ListItem();
        items.Text = option;
        items.Value = option; 
        chklist.Items.Add(items);
      }                   
      phRow.Controls.Add(chklist);
    }

    else
    {
      RadioButtonList rdblist = new RadioButtonList();
      rdblist.RepeatDirection = RepeatDirection.Horizontal;
      rdblist.Font.Italic = true;

      rdblist.RepeatColumns = 4;

      foreach (string option in options.Split(','))
      {

        ListItem items = new ListItem();
        items.Text = option;
        items.Value = option; 
        rdblist.Items.Add(items);
      }                   

      phRow.Controls.Add(rdblist);
    }

  }

}