2013-03-27 25 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,從而重新創建子控件。