我已经创建了一个动态添加控件的表单,用户选择了他想要的控件类型放置在占位符中。现在我想添加一个Image Button Click事件来动态创建Image Button ..我没有成功在过去两天..我尝试过使用委托n事件处理,这对我来说是一个新概念.. 我用它来迭代并分配属性
<
void IterateThroughChildren(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.ImageButton") &&
c.ID == null)
{
((ImageButton)c).ImageUrl = @"pictures/close-icon.png";
((ImageButton)c).ID = "Imagebtn" +count.ToString();
((ImageButton)c).Click += new ImageButtonClickEventHandler(UserControl1_ImageButtonClickEvent);
}>
使用此委托和事件处理程序来指定函数
<public delegate void ImageButtonClickEventHandler(Object sender, EventArgs args);
public event ImageButtonClickEventHandler ImageButtonClickEvent;
private void imageButton_Click(object sender, System.EventArgs e)
{
if (ImageButtonClickEvent != null)
{
ImageButtonClickEvent(sender, e);
}
}
private void UserControl1_ImageButtonClickEvent(Object sender, EventArgs args)
{
CreateRadioButtons(sender , args);
}>
**
我还想知道其他调用函数的方法 动态创建conlrols ..
**
预先感谢任何一位助手.. 这是完整的代码..
<using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int count = 1;
static int m = 0;
static int n = 0;
protected void Page_Load(object sender, EventArgs e)
{
btnAddControl.Visible = false;
btnAddText.Visible = false;
}
void IterateThroughChildren(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") &&
c.ID == null)
{
((TextBox)c).ID = "txtQ" + count.ToString();
((TextBox)c).Text = "Add Text Here" + count.ToString();
((TextBox)c).Columns = 90;
((TextBox)c).Focus();
//((TextBox)c).TextChanged = "CreateRadioButtons()";
// ((TextBox)c).Attributes["onclick"] = "this.CreateRadiobuttons()";
//((TextBox)c).TextChanged();
//+= CreateRadioButton;
//((TextBox)c).Focus = CreateRadioButtons();
count++;
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButton") &&
c.ID == null)
{
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.ImageButton") &&
c.ID == null)
{
((ImageButton)c).ImageUrl = @"pictures/close-icon.png";
((ImageButton)c).ID = "Imagebtn" +count.ToString();
((ImageButton)c).Click += new ImageClickEventHandler(_Default_Click);
((ImageButton)c).Click += new ImageClickEventHandler(CreateRadioButtons);
}
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.Label") &&
c.ID == null)
{
((Label)c).Text = count.ToString() + ".";
}
if (c.Controls.Count > 0)
{
IterateThroughChildren(c);
}
}
}
void _Default_Click(object sender, ImageClickEventArgs e)
{
CreateRadioButtons(sender,e);
throw new NotImplementedException();
}
protected void abc_acb( object sender, EventArgs e)
{
}
protected void CreateRadioButtons(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
btnAddControl.Visible = true;
m++;
// now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
for (int i = 0; i < m; i++)
{
PlaceHolder1.Controls.Add(new RadioButton());
PlaceHolder1.Controls.Add(new TextBox());
PlaceHolder1.Controls.Add(new ImageButton());
LiteralControl literalBreak3 = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak3);
LiteralControl literalBreak = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak);
LiteralControl literalBreak2 = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak2);
}
EventHandler eh = new EventHandler(CreateRadioButtons);
TextBox Mytextbox = new TextBox();
Mytextbox.ID = "123";
Mytextbox.Columns = 90;
Mytextbox.AutoPostBack = true;
Mytextbox.Text = "Click to create another option";
Mytextbox.TextChanged += eh;
//Mytextbox.TextChanged += CreateRadioButtons;
PlaceHolder1.Controls.Add(new RadioButton());
PlaceHolder1.Controls.Add(Mytextbox);
LiteralControl literalBreak4 = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak4);
LiteralControl literalBreak5 = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak5);
// now, set the Text property of each TextBox
IterateThroughChildren(this);
}
}
protected void CreateTextBoxes(object sender , EventArgs e)
{
btnAddText.Visible = true;
if (Page.IsPostBack == true)
{
n++;
// now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
for (int i = 0; i < n; i++)
{
TextBoxesHere.Controls.Add(new Label());
TextBoxesHere.Controls.Add(new TextBox());
TextBoxesHere.Controls.Add(new ImageButton());
LiteralControl literalBreak = new LiteralControl("<br />");
TextBoxesHere.Controls.Add(literalBreak);
LiteralControl literalBreak1 = new LiteralControl("<br />");
TextBoxesHere.Controls.Add(literalBreak1);
}
// now, set the Text property of each TextBox
IterateThroughChildren(this);
}
}
protected void ddlSelectControl_SelectedIndexChanged(object sender, EventArgs e)
{
string a = ddlSelectControl.SelectedValue.ToString();
if (a == "0")
{
CreateRadioButtons(sender, e);
}
else
{
CreateTextBoxes(sender ,e);
}
}
}
>
答案 0 :(得分:1)
目前尚不清楚实际发生了什么,但您的订阅代码会更好(假设.NET 3.5,因此您可以使用LINQ):
foreach (ImageButton button in parent.Controls
.OfType<ImageButton>()
.Where(c => c.ID == null))
{
button.ImageUrl = @"pictures/close-icon.png";
buttonID = "Imagebtn" + count;
button.Click += new UserControl1_ImageButtonClickEvent;
}
请注意,这只会在parent
下迭代控件直接。如果有其他容器控件,您可能需要通过这些控件递归,例如
foreach (CompositeControl container in parent.Controls
.OfType<CompositeControl>())
{
IterateThroughChildren(container);
}
这可能是实际上错的......但你还没有说出你实际观察到的事情,这会让你更难帮助......
答案 1 :(得分:1)
在CreateTextBoxes()
for (int i = 0; i < n; i++)
{
ImageButton iM = new ImageButton();//
TextBoxesHere.Controls.Add(new Label());
TextBoxesHere.Controls.Add(new TextBox());
TextBoxesHere.Controls.Add(iM );
LiteralControl literalBreak = new LiteralControl("<br />");
TextBoxesHere.Controls.Add(literalBreak);
LiteralControl literalBreak1 = new LiteralControl("<br />");
TextBoxesHere.Controls.Add(literalBreak1);
}
在CreateRadioButtons()
for (int i = 0; i < m; i++)
{
ImageButton iM = new ImageButton();//
PlaceHolder1.Controls.Add(iM);
PlaceHolder1.Controls.Add(new TextBox());
PlaceHolder1.Controls.Add(new ImageButton());
LiteralControl literalBreak3 = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak3);
LiteralControl literalBreak = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak);
LiteralControl literalBreak2 = new LiteralControl("<br />");
PlaceHolder1.Controls.Add(literalBreak2);
}
在省略c.ID == null
和((ImageButton)c).ID = "Imagebtn" +count.ToString();
后,您可以尝试上述代码吗?这就是我想尝试的内容。
答案 2 :(得分:0)
让图像按钮在点击上工作的解决方案是这个..
}
protected void img_Click(object sender, ImageClickEventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "img",
"<script type = 'text/javascript'>alert('ImageButton Clicked');</script>");
}>
但是在我的情况下,我在CreateRadiobutton中创建按钮,所以点击页面加载函数被调用,因此该属性需要其他解决方案..