1
This is the Design:::

<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:Label ID="l1" runat="server" Text="0"></asp:Label>
<asp:Label ID="l2" runat="server" Text="0"></asp:Label>



Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim i As Integer = 0
    Dim j As Integer = 0
    l1.Text = l2.Text
    l2.Text = Val(l2.Text) + 10
    i = Val(l1.Text)
    j = Val(l2.Text)
    MsgBox(i & " " & j)
    While (i < j)
        Dim b As Button = New Button()
        b.ID = "b" + i.ToString()
        b.Text = b.ID
        Panel1.Controls.Add(b)
        i = i + 1
    End While
End Sub

これにはクリック時のボタンが含まれており、10個の一意のボタンを作成する必要があります..作成したすべてのボタンを既存のボタンに追加したい..しかし、プログラムはパネルにコントロールを追加する代わりにオーバーライドしています.

4

2 に答える 2

2

このコードを試してみました。それは何も上書きしません。ボタンをページの最初に追加します。ボタン「ボタン 2」とラベルの後にボタンを表示する場合は、パネルを配置する必要があります。次のようにする必要があります。

<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:Label ID="l1" runat="server" Text="0"></asp:Label>
<asp:Label ID="l2" runat="server" Text="0"></asp:Label>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>

ボタンを既存のボタンとラベルと一緒に追加したい場合は、パネルの代わりにプレースホルダーを使用するか、スタイル 'display:inline' をパネルに追加します。

于 2012-09-09T10:33:04.480 に答える
0

ボタンがクリックされるたびに 10 個の動的コントロールを追加したいと思います。あなたが直面している問題は、最初のクリックで 10 個のボタン コントロールが追加され、その後のクリックで以前のコントロールが上書きされることです。動的コントロールは、ページがリクエストされるたびに作成されるため、リクエストごとに作成する必要があります。

クエリを解決するために次のコードを記述しましたが、機能します。

// Aspx code
<asp:Panel ID="Panel1" runat="server">
 </asp:Panel>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:Label ID="l1" runat="server" Text="0"></asp:Label>
<asp:Label ID="l2" runat="server" Text="0"></asp:Label>

// Code BEhind

 Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim i As Integer = 0
    Dim j As Integer = 0
    l1.Text = l2.Text
    l2.Text = Val(l2.Text) + 10
    i = Val(l1.Text)
    j = Val(l2.Text)
    MsgBox(i & " " & j)

    Dim lst As List(Of Integer) = New List(Of Integer)  'Create a list of integer type
    lst.Add(i)  'Save value of i
    lst.Add(j)

    If Not Session("id_0") Is Nothing Then ' Will Check session for First Click exist or not

        For k As Integer = 0 To DirectCast(Session("click"), Integer)

            Dim mylst As List(Of Integer) = New List(Of Integer)
            mylst = DirectCast(Session("id_" & k), List(Of Integer)) ' Assign Session Value to List
            If Not mylst Is Nothing Then
                WriteControls(mylst(0), mylst(1)) ' Call the function
            End If


        Next


    End If

    Session("id_" & DirectCast(Session("click"), Integer)) = lst  ' Saves the entire list in Session

    WriteControls(i, j)  ' Function Call

End Sub

Private Sub WriteControls(i As Integer, j As Integer)  ' Function to write button control
    While (i < j)
        Dim b As Button = New Button()
        b.ID = "b" + i.ToString()
        b.Text = b.ID
        b.ViewStateMode = UI.ViewStateMode.Enabled
        Panel1.Controls.Add(b)
        i = i + 1
    End While
    Session("click") = DirectCast(Session("click"), Integer) + 1
End Sub

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then          
        Session("click") = 0   ' Stores the no. of Clicks of the button
    End If

End Sub

' Output
' If I press three times button then there will be 30 buttons
b0 b1 b2 b3 b4 ............................................... b29
20 30
于 2012-09-09T11:26:38.567 に答える