none
About Asp.net's customized Control RRS feed

  • 问题

  • Hello all, in recent days I've developed a control like this:

    Imports System.ComponentModel

    Public Class MyTestControl
        Inherits CompositeControl

        <NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property Panels() As List(Of Panel)
            Get
                Dim s As Object = ViewState("_StaticPanels")
                If s Is Nothing Then
                    s = New List(Of Panel)
                End If
                Return CType(s, List(Of Panel))
            End Get
            Set(ByVal value As List(Of Panel))
                ViewState("_StaticPanels") = value
            End Set
        End Property

        Protected Overrides Sub CreateChildControls()
            Controls.Clear()

            For Each p In Panels
                Controls.Add(p)
            Next

        End Sub

        Protected Overrides Sub Render(ByVal w As HtmlTextWriter)
            If Panels() IsNot Nothing AndAlso Panels.Count > 0 Then
                ' content(s)
                w.AddAttribute(HtmlTextWriterAttribute.Style, "border:1px solid #666666; background-color:#eeeeee")
                w.RenderBeginTag(HtmlTextWriterTag.Div)
                w.RenderBeginTag(HtmlTextWriterTag.H3)
                w.Write("MyTestControl")
                w.RenderEndTag()
                For Each p As Panel In Panels
                    p.RenderControl(w)
                Next
                'w.RenderEndTag()
            End If
        End Sub

    End Class

    Public Class WebForm1
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                txt.Text = "Hello World"
            End If
            Dim s() As String = Split("One, Two, Three, Four", ",")
            ddl1.DataSource = s
            ddl1.DataBind()
        End Sub

    End Class

    And in the aspx markups are:

    <cc1:TabPanelHolder ID="TabPanel1" runat="server"> 
           
    <Panels> 
                   
    <e4:TabPanel ID="Panel1" runat="server"> 
                           
    <asp:TextBox ID="tbx1" runat="server" Text="Hello World" /> 
                           
    <asp:DropDownList ID="ddl1" runat="server" /> 
                           
    <asp:Button ID="btn" runat="server" Text="Post" /> 
                   
    </e4:TabPanel> 
           
    </Panels> 
    </cc1:TabPanelHolder>

    I call the control like this in the main.aspx page:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load          If Not Page.IsPostBack Then 
                    txt
    .Text = "Hello World" 
           
    End If 
           
    Dim s() As String = Split("One, Two, Three, Four", ",") 
            ddl1
    .DataSource =
            ddl1
    .DataBind() 
    End Sub

    However, the question is that if I move  the codes below into "If Not IsPostBack" when I click the button, nothing remaining in the Dropdownlist, I don't know why? How to solve that problem?

    Urgently waiting…… Here's the codes to be moved into "If Not IsPostBack"

     Dim s() As String = Split("One, Two, Three, Four", ",") 
            ddl1
    .DataSource =

            ddl1
    .DataBind()
     

    2011年3月11日 8:04