标题:web自定义控件赋值问题
只看楼主
wstcl
Rank: 1
等 级:新手上路
帖 子:381
专家分:5
注 册:2005-8-17
结帖率:100%
 问题点数:0 回复次数:2 
web自定义控件赋值问题
我目前学习开了一个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 编辑 ]
搜索更多相关主题的帖子: server 
2012-02-20 22:22
wstcl
Rank: 1
等 级:新手上路
帖 子:381
专家分:5
注 册:2005-8-17
得分:0 
原本的我代码还要复杂,为了能使用大家看懂我的问题,我将代码最简化。

Love Baby
2012-02-20 22:27
wstcl
Rank: 1
等 级:新手上路
帖 子:381
专家分:5
注 册:2005-8-17
得分:0 
没人可以帮我吗?

Love Baby
2012-02-21 09:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-361258-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.275269 second(s), 10 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved