中继器内动态创建控件 [英] Dynamically creating controls inside a repeater

查看:161
本文介绍了中继器内动态创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2滴下来的中继器内部控制和我有重复那些按钮点击我怎么能实现呢?

I have 2 Drop down controls inside a repeater and i have to repeat those with a button click how can i achieve it??

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.

推荐答案

我们可以从code后面使用占位符控件添加动态控件一样文本框,单选按钮列表,复选框列表insde Repeater控件。

We can add dynamic controls like Textbox, Radiobuttonlist, Checkbox List insde repeater control from code behind using placeholder control.

动态控制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);
    }

  }

}

这篇关于中继器内动态创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆