我想通过点击按钮应该不会覆盖现有的动态创建的控件动态控件添加到面板 [英] i want to add dynamic controls to panel by button click which should not override existing dynamically created controls

查看:128
本文介绍了我想通过点击按钮应该不会覆盖现有的动态创建的控件动态控件添加到面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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个独特的按钮一键.. 我要附加创建的按钮与现有的按钮.. 但该方案中重写添加控件到面板,而不是 帮帮我吧。

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

这篇关于我想通过点击按钮应该不会覆盖现有的动态创建的控件动态控件添加到面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆