简体   繁体   English

在中继器内部动态创建控件

[英]Dynamically creating controls inside a repeater

I have 2 Drop down controls inside a repeater and i have to repeat those with a button click how can i achieve it?? 我在中继器内部有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); 
} 

is this is the correct way but how can i find the button inside a repeater and fire it. 这是正确的方法吗,但是我如何找到中继器内的按钮并将其触发。

Add the DropDownLists and Button controls to a PlaceHolder control within your repeater: 将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>

In the ItemCommand event for your repeater, check the CommandName from the button click, then create & add your dynamic dropdownlist to the placeholder: 在用于中继器的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);
   }
}

There's a bit more work to be done to hook up the SelectedIndexChanged events, but this should get you started. 要挂接SelectedIndexChanged事件,还需要做更多的工作,但这应该可以帮助您入门。

We can add dynamic controls like Textbox, Radiobuttonlist, Checkbox List insde repeater control from code behind using placeholder control. 我们可以使用占位符控件从后面的代码中添加诸如文本框,单选按钮列表,复选框列表insde转发器控件之类的动态控件。

Dynamic control inside Repeater using placeholder in Asp.Net(c#)? 使用Asp.Net(c#)中的占位符在Repeater内部进行动态控制?

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

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM