Asp.net 我想通过单击按钮将动态控件添加到面板中,该按钮不应覆盖现有的动态创建控件 这是设计:: 受保护的子按钮2\u单击(ByVal sender作为对象,ByVal e作为System.EventArgs)处理按钮2。单击 尺寸i为整数=0 尺寸j为整数=0 l1.Text=l2.Text l2.Text=Val(l2.Text)+10 i=Val(l1.Text) j=Val(l2.Text) MsgBox(i&“j”) 而(i

Asp.net 我想通过单击按钮将动态控件添加到面板中,该按钮不应覆盖现有的动态创建控件 这是设计:: 受保护的子按钮2\u单击(ByVal sender作为对象,ByVal e作为System.EventArgs)处理按钮2。单击 尺寸i为整数=0 尺寸j为整数=0 l1.Text=l2.Text l2.Text=Val(l2.Text)+10 i=Val(l1.Text) j=Val(l2.Text) MsgBox(i&“j”) 而(i,asp.net,.net,vb.net,Asp.net,.net,Vb.net,这包含一个点击按钮,它应该创建10个独特的按钮。。 我想用现有按钮附加所有创建的按钮。。 但是程序正在重写,而不是向面板添加控件 帮帮我。我刚刚试过这个代码。它不会覆盖任何内容。它将按钮添加到页面的最开头。如果您希望按钮显示在按钮“button 2”和标签之后,那么您应该放置面板,然后它应该是这样的 This is the Design::: <asp:Panel ID="Panel1" runat="server"> </asp:Panel> <asp:Butt

这包含一个点击按钮,它应该创建10个独特的按钮。。 我想用现有按钮附加所有创建的按钮。。 但是程序正在重写,而不是向面板添加控件
帮帮我。

我刚刚试过这个代码。它不会覆盖任何内容。它将按钮添加到页面的最开头。如果您希望按钮显示在按钮“button 2”和标签之后,那么您应该放置面板,然后它应该是这样的

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


如果您希望按钮沿着现有按钮和标签添加,那么您可以使用占位符代替面板,或者在面板中添加样式“display:inline”。

我认为您希望在每次单击按钮时添加10个动态控件。您现在面临的问题是,第一次单击时,它会添加10个按钮控件,随后单击时,它会覆盖以前的控件。每次请求页面时都会创建动态控件,因此您必须在每次请求时创建它们

我编写了以下代码来解决您的查询,它可以正常工作:

<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>
//Aspx代码
//代码隐藏
受保护的子按钮2\u单击(发件人作为对象,e作为事件参数)处理按钮2。单击
尺寸i为整数=0
尺寸j为整数=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(整型)=新列表(整型)'创建整型列表
1.添加(i)‘i的保存值’
第一项增补(j)
如果Not会话(“id_0”)为空,则“将检查会话是否存在”
对于k作为整数=0的DirectCast(会话(“单击”),整数)
Dim mylst As List(整型)=新列表(整型)
mylst=DirectCast(会话(“id_”&k),列表(整数))'将会话值分配给列表
如果不是我的话,那就什么都不是了
writeControl(mylst(0),mylst(1))'调用函数
如果结束
下一个
如果结束
Session(“id”&DirectCast(Session(“click”),Integer))=lst'将整个列表保存在Session中
WriteControl(i,j)'函数调用
端接头
Private Sub-WriteControl(i为整数,j为整数)函数用于写入按钮控件
而(i
您的问题不清楚。是否要在每次单击时添加按钮?您每次都在刷新,因为它是服务器端的,所以您需要重新创建以前的状态谢谢。。对于你的评论,当我试图再次点击时,它会被覆盖。这是我的问题
// 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