注册 登录
编程论坛 ASP.NET技术论坛

web自定义控件赋值问题

wstcl 发布于 2012-02-20 22:22, 669 次点击
我目前学习开了一个web自定义控件,可以在属性窗口赋值,但在代码中不能,下面请看我一下我代码的操作过程截图,请大侠帮我分析一下,为什么?

第一步,添加一个"web自定义控件",并生成dll文件
Imports System
Imports System.Collections.Generic
Imports
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1

    Inherits CompositeControl

    Dim MyTxt As New TextBox

    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
        Get
            Dim s As String = CStr(ViewState("Text"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("Text") = Value
        End Set
    End Property

    Protected Overrides Sub CreateChildControls()
        Controls.Clear()
        '
        CreateControlHierarchy()

        ClearChildViewState()
    End Sub

    Protected Overridable Sub CreateControlHierarchy()

        MyTxt.Text = Text
        Controls.Add(MyTxt)
    End Sub

   
End Class

第二步:新建一个网站,在工具箱中添加刚刚生成WebCustomControl1控件,接着在属性突发text赋值"ok"在控件WebCustomControl1中文本框的内容显示"ok"
如图1

只有本站会员才能查看附件,请 登录





第三步:添加一个Button,通过单击Button控件为WebCustomControl1赋值,代码如下


Partial Class _Default
    Inherits System.Web.UI.Page

   
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.WebCustomControl1_1.Text = "Hello World"
    End Sub
End Class
运行后,单击按扭,文本未改变(没有显示"Hello World",而仍然显示"ok"),如图2


只有本站会员才能查看附件,请 登录







[ 本帖最后由 wstcl 于 2012-2-20 22:25 编辑 ]
2 回复
#2
wstcl2012-02-20 22:27
原本的我代码还要复杂,为了能使用大家看懂我的问题,我将代码最简化。
#3
wstcl2012-02-21 09:09
没人可以帮我吗?
1