2013-03-27 76 views
2

创建自定义控件 这里是第一个控制代码CompositeControl在回发上创建控件?

1。 StartScreen.cs和ActDesc.cs控件。

  1. 第一次加载页面时,我看到两个编辑按钮。
  2. 我点击其中的任何一个,看它的内容的文本框。
  3. 然后我点击保存保存按钮事件没有被解雇。
  4. 对我来说,就像在初始CreateChildControls触发事件期间创建的控件一样。在Postback上创建的人不会触发事件。
  5. 我在编辑按钮的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; } 
} 

回答

0

为了使控制具有事件,您必须使用事件声明。这是在vb.net中,但可以在这里转换:http://converter.telerik.com/。这也从一个ScriptControl的,而不是一个复合继承,但它应该工作一样:

<Assembly: WebResource("myProject.FileUpload.css", "text/css", PerformSubstitution:=True)> 
< _ 
ToolboxData("<{0}:FileUpload ID='FileUpload{0}' runat=""server""> </{0}:FileUpload>"), _ 
ClientCssResource("myProject.FileUpload.css")> _ 
Public Class FileUpload 
    Inherits ScriptControl 
    Implements INamingContainer, IPostBackEventHandler 

    Private WithEvents btnMyButton As New Button 

    .... 

    Protected Overrides Sub CreateChildControls() 
     'method to create my controls 
     createDynamicControls() 

     'if you are using update panels and wish to do an async 
     ' post back, you must first register the conrol with 
     ' the pages script manager. If you are not using update panels, you can 
     ' skip this 
     ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(Me.btnMyButton)  

     MyBase.CreateChildControls() 
    End Sub 

    Protected Overrides Function SaveViewState() As Object 
     Return New Pair(MyBase.SaveViewState(), Nothing) 
    End Function 


    Protected Overrides Sub LoadViewState(ByVal savedState As Object) 
     MyBase.LoadViewState(CType(savedState, Pair).First) 
     EnsureChildControls() 
    End Sub 

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs) 
     attachWebResources() 
     MyBase.OnInit(e) 

    End Sub 

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) 
     MyBase.OnLoad(e) 
    End Sub 

    Private Sub attachWebResources() 
     'use this to attach an embedded CSS file with the DLL 
     Dim styleLink As String = "<link rel='stylesheet' text='text/css' href='{0}' />" 
     Dim location As String = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "myProject.FileUpload.css") 
     Dim styleInclude As New LiteralControl([String].Format(styleLink, location)) 
     DirectCast(Page.Header, HtmlControls.HtmlHead).Controls.Add(styleInclude) 
    End Sub 

    Protected Sub btnMyButton _click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMyButton .Click 

     '....do stuff 
    end sub 
     Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent 
      'required for IPostBackEventHandler 
    End Sub 

    Private Sub createDynamicControls() 
    controls.Clear() 
    'instantiate your control here and add it to the controls object 
    end sub 
end Class 

让我知道如果这能帮助或者如果你是某个地方卡住了。

+0

谢谢杰森。我想要事件“ImageButton”点击事件被触发,而不是CustomControl的任何自定义事件 – user581157 2013-03-28 06:16:21

0

我确实找到了我的问题的答案问题是我认为我没有将ID分配给可能用不同的ID重新创建的控件因此与之前回发期间保存的值不匹配。我还更改了回发处理程序中的代码,以在设置this.ChildControlsCreated = false时调用EnsureChildControls,从而重新创建子控件。

相关问题