我想通过点击按钮应该不会覆盖现有的动态创建的控件动态控件添加到面板控件、我想、动态、按钮

由网友(以我之姓冠你之名)分享简介:This is the Design:::

这包含了点击它应该创建10个独特的按钮一键.. 我要附加创建的按钮与现有的按钮.. 但该方案中重写添加控件到面板,而不是 帮帮我吧。

This contains a button on click it should create 10 unique buttons.. i want to append all created buttons with existing buttons.. but the program is overriding instead of adding controls to the panel help me.

推荐答案

我想你想在每一个按钮的点击添加10动态控件。你所面临的权利知道的问题是,在第一次单击它增加了10键的控制,并在后续的点击它会覆盖previous控制。动态控制,所以你必须在每次请求创建它们创建的页面被请求每次。

I think you want to add 10 dynamic controls on every button click. The problem you are facing right know is that on first click it adds 10 button controls, and on subsequent clicks it overrides the previous controls. Dynamic controls are created every time the page is requested so you have to create them on every request.

我写了下面的code,以解决您的查询和它的作品:

I write the following code to solve your query and it works:

// 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
阅读全文

相关推荐

最新文章