0

我在转发器中有 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); 
} 

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

4

2 回答 2

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,然后创建并将动态下拉列表添加到占位符:

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 事件还有一些工作要做,但这应该可以帮助您入门。

于 2012-11-01T10:33:25.583 回答
0

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

使用 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);
    }

  }

}
于 2016-05-02T07:40:35.193 回答