1

感谢@Angkor Wat,我朝着我的目标迈出了重要的一步:将字符串动态添加到字符串中。但是我遇到了另一件事我无法解决。

这是脚本:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="addtostring.aspx.vb" Inherits="demo_addtostring" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p><asp:textbox id="tb" runat="server"></asp:textbox></p>
        <asp:Panel ID="tbPanel" runat="server"></asp:Panel>
    </div>
    </form>
</body>
</html>

这是后面的代码:

Partial Class demo_addtostring
    Inherits System.Web.UI.Page

    Public Property gesStr As String
        Set(value As String)
            ViewState("gesStr") = value
        End Set
        Get
            Dim o As Object = ViewState("gesStr")
            If o Is Nothing Then
                Return ""
            Else
                Return o
            End If
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Page.IsPostBack Then
        Else
            gesStr = "1;"
        End If
        tb.Text = gesStr
        Dim iButton As New Button
        iButton.Text = "add"
        iButton.CommandArgument = "1;"
        iButton.CommandName = "1;"
        AddHandler iButton.Click, AddressOf add
        tbPanel.Controls.Add(iButton)

        If Page.IsPostBack Then
        Else
            anzeige()
        End If
    End Sub

    Private Sub add(ByVal sender As Object, ByVal e As EventArgs)
        Dim myButton As Button = DirectCast(sender, Button)
        Dim addString As String = myButton.CommandArgument
        gesStr += addString
        tb.Text = gesStr
        anzeige()
    End Sub

    Private Sub anzeige()
        Dim gesArray As Array = Split(gesStr, ";")
        For xLauf As Integer = 0 To UBound(gesArray) - 1
            Dim anzeigeDiv As New System.Web.UI.HtmlControls.HtmlGenericControl("div")
            Dim anzLabel As New Label
            anzLabel.Text = gesArray(xLauf)
            anzeigeDiv.Controls.Add(anzLabel)
            Dim iButton2 As New Button
            iButton2.Text = xLauf.ToString
            iButton2.ID = "test" & xLauf.ToString
            iButton2.CommandArgument = "1;"
            iButton2.CommandName = "1;"
            AddHandler iButton2.Click, AddressOf add
            anzeigeDiv.Controls.Add(iButton2)
            tbPanel.Controls.Add(anzeigeDiv)
        Next
    End Sub
End Class

单击添加按钮应添加“1;” to gesStr- 动态循环生成的按钮应该做同样的事情 -.- 有人知道吗?我将非常感谢您的帮助...

4

2 回答 2

1

为了让回发知道按钮的事件处理程序,需要在生命周期中调用处理程序的点之前重新创建按钮。换句话说,您总是需要在 Page_Load 中重新创建按钮。

这是对您的代码的修改:

<form id="form1" runat="server">
<div>
    <p><asp:textbox id="tb" runat="server"></asp:textbox></p>
    <br />
    <asp:Button runat="server" ID="btnAdd" Text="add" CommandArgument="1;" />
    <asp:Panel ID="tbPanel" runat="server"></asp:Panel>
</div>
</form>

和代码隐藏:

Public Property gesStr As String
    Get
        Dim o As Object = ViewState("gesStr")
        If o Is Nothing Then
            Return ""
        Else
            Return DirectCast(o, String)
        End If
    End Get
    Set(value As String)
        ViewState("gesStr") = value
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        gesStr = "1;"
    End If

    tb.Text = gesStr
End Sub

Private Sub add(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnAdd.Command
    Dim addString As String = e.CommandArgument
    gesStr += addString
    tb.Text = gesStr
    CreateNewButton(tbPanel.Controls.Count, addString.Substring(0, addString.Length - 1))
End Sub

Protected Overrides Sub CreateChildControls()
    MyBase.CreateChildControls()

    Dim gesArray As Array = Split(gesStr, ";")
    For xLauf As Integer = 0 To UBound(gesArray) - 1
        CreateNewButton(xLauf, gesArray(xLauf))
    Next
End Sub

Private Sub CreateNewButton(ByVal xLauf As Integer, ByVal labelText As String)
    Dim anzeigeDiv As New Panel
    anzeigeDiv.ID = "div" & xLauf.ToString()

    Dim anzLabel As New Label
    anzLabel.Text = labelText
    anzeigeDiv.Controls.Add(anzLabel)

    Dim iButton2 As New Button
    iButton2.Text = xLauf.ToString
    iButton2.ID = "test" & xLauf.ToString()
    iButton2.CommandArgument = "1;"
    iButton2.CommandName = "1;"
    AddHandler iButton2.Command, AddressOf add
    anzeigeDiv.Controls.Add(iButton2)

    tbPanel.Controls.Add(anzeigeDiv)
End Sub

我已经将始终存在的添加按钮移到了 aspx 页面中,这样动态面板将只包含基于gesStr值添加的按钮。

于 2012-07-17T14:25:46.703 回答
0

必须在每次回发时重新创建动态控件,有关详细信息,请参阅本文

于 2012-07-17T11:06:54.850 回答