这里创建自定义控件是第一个控件代码
1. StartScreen.cs 和 ActDesc.cs 控件。
- 第一次加载页面时,我看到两个编辑按钮。
- 我单击其中任何一个,查看其中包含内容的文本框。
- 然后我点击保存保存按钮事件不会被触发。
- 在我看来,在初始 CreateChildControls 期间创建的控件会触发该事件。在 Postback 上创建的不会触发事件。
- 我在编辑按钮的 Command 事件处理程序上调用 ControlsCreate() 方法。我也尝试过使用 EnsureChildControls() 调用。还是没用?
谁能指导我实现这一目标的方法?
. .
public class Wrapper
{
public string Name { get; set; }
public string Description { get; set; }
}
namespace Cons
{
public class StartScreen : CompositeControl
{
public StartScreen()
{
//
// TODO: Add constructor logic here
//
}
protected override void CreateChildControls()
{
List<Wrapper> list = new List<Wrapper>();
for (int i = 0; i < 2; i++)
{
list.Add(new Wrapper { Name = "D", Description = "DDDDDDDDDD" });
}
ActDesc desc = new ActDesc(list);
desc.DataBind();
this.Controls.Add(desc);
base.CreateChildControls();
}
}
}
public class ActDesc : CompositeControl
{
IEnumerable<Wrapper> list = new Wrapper[] { };
public ActDesc(IEnumerable<Wrapper> list)
{
this.list = list;
}
private void ControlsCreate()
{
this.Controls.Clear();
Table activityDescription = new Table();
int i = 1;
foreach (var item in list)
{
TableRow tempRow = new TableRow();
TableCell tempLeftCell = new TableCell();
tempLeftCell.Text = item.Name;
TableCell tempRightCell = new TableCell();
tempRightCell.HorizontalAlign = HorizontalAlign.Right;
if (EditId == 0)
{
ImageButton editButton = new ImageButton();
editButton.ID = string.Format("editButton_{0}", i);
editButton.ImageUrl = "add_16.png";
editButton.CommandArgument = i.ToString();
editButton.Command += new CommandEventHandler(editButton_Command);
editButton.CommandName = "Edit";
tempRightCell.Controls.Add(editButton);
}
else
{
if (EditId == i)
{
ImageButton saveButton = new ImageButton();
saveButton.ID = string.Format("saveButton_{0}", i);
saveButton.ImageUrl = "~/save.png";
saveButton.CommandArgument = i.ToString();
saveButton.Command += new CommandEventHandler(editButton_Command);
saveButton.CommandName = "Save";
tempRightCell.Controls.Add(saveButton);
}
}
tempRow.Cells.Add(tempLeftCell);
tempRow.Cells.Add(tempRightCell);
//Add the first row which contains the header name and the edit button if AllowEdit is true
activityDescription.Rows.Add(tempRow);
tempRow = new TableRow();
tempLeftCell = new TableCell();
tempLeftCell.ColumnSpan = 2;
Control tempControl;
if (EditId > 0 && EditId.Equals(i))
{
TextBox txt = new TextBox();
txt.TextMode = TextBoxMode.MultiLine;
txt.ID = "txt" + i;
txt.Text = item.Description;
tempControl = txt;
}
else
{
Literal litContent = new Literal();
litContent.ID = string.Format("literalContent_{0}", i);
litContent.Mode = LiteralMode.Transform;
litContent.Text = item.Description;
tempControl = litContent;
}
tempLeftCell.Controls.Add(tempControl);
tempRow.Cells.Add(tempLeftCell);
// Add the second row which shows the detailed HTML description..
activityDescription.Rows.Add(tempRow);
i = i + 1;
}
this.Controls.Add(activityDescription);
}
protected override void CreateChildControls()
{
ControlsCreate();
base.CreateChildControls();
}
void editButton_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
string id = e.CommandArgument.ToString();
EditId = Convert.ToInt32(id);
ControlsCreate();
break;
default:
break;
}
}
public int EditId
{
set
{
this.ViewState["__EditId"] = value;
}
get
{
return Convert.ToInt32(this.ViewState["__EditId"]);
}
}
public bool AllowEdit { get; set; }
}