0

I'm developing a chat system. In this I'm creating dynamic tab panels in a tab container when I change the selected index of the list box of users. In these tab panels I'm creating 2 text boxes and 1 button dynamically and adding a handler for the button click event. I'm storing the tabid's in the session state and recreating all the tabs in the page_init event. I can fire the button click event in the tab panel, in which I'm unable to access these dynamically created text boxes but I can access a label which I created statically. I used the findcontrol() method but it is showing an error message: "Use new command to create the textbox instances". It is showing something like I haven't created the instances of textbox.

Any help is greatly appreciated!

squillman
  • 13,363
  • 3
  • 41
  • 60
krrish
  • 163
  • 5
  • 12
  • Why are you creating those 2 TextBoxes and the Button dynamically? You should wrap them in a UserControl instead. That makes things a lot easier. Apart from that i've yet not understand what you're trying to do. User selects an item of a ListBox in one TabPanel which causes n-TabPanels to be created. Can you explain that more detailed? – Tim Schmelter Jan 20 '12 at 18:13
  • I'm displaying signed in users in a listbox, when a user is selected from the users list I'm creating a tabpanel with Two text boxes and to type and one button to send data. If I selected another user I'm recreating the old tab and creating a new tab with new id and adding two text boxes and send button to it as before. I don't to how to wrap them in user control can you explain it to me with an example? Do you want to see my source code? – krrish Jan 20 '12 at 18:36

1 Answers1

0

Here is a complete working sample:

Page-ASPX:

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="true">
</asp:ToolkitScriptManager>
<div>
   <asp:UpdatePanel ID="UpdTabContainer" ChildrenAsTriggers="false" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
   <asp:TabContainer ID="TabContainer1" runat="server" AutoPostBack="true">
        <asp:TabPanel ID="TabUserList" runat="server" HeaderText="UserList">
            <ContentTemplate>
                <asp:UpdatePanel ID="UpdUserList" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:ListBox ID="ListBox1" SelectionMode="Single" AutoPostBack="true" OnSelectedIndexChanged="UserChanged" runat="server">
                            <asp:ListItem Text="User 1" Value="1"></asp:ListItem>
                            <asp:ListItem Text="User 2" Value="2"></asp:ListItem>
                            <asp:ListItem Text="User 3" Value="3"></asp:ListItem>
                        </asp:ListBox>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="TabContainer1" EventName="ActiveTabChanged" />
                    </Triggers>
                </asp:UpdatePanel>
            </ContentTemplate>
        </asp:TabPanel>
    </asp:TabContainer>
    </ContentTemplate> 
    </asp:UpdatePanel>
</div>

Codebehind:

Public Class TabContainerSample
    Inherits System.Web.UI.Page

    Property CreatedTabIDs As List(Of Int32)
        Get
            If Session("CreatedTabIDs") Is Nothing Then
                Session("CreatedTabIDs") = New List(Of Int32)
            End If
            Return DirectCast(Session("CreatedTabIDs"), List(Of Int32))
        End Get
        Set(value As List(Of Int32))
            Session("CreatedTabIDs") = value
        End Set
    End Property

    Private Sub TabContainerSample_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        For Each userID In CreatedTabIDs
            AddTab(userID)
        Next
    End Sub

    Private Sub AddTab(tabID As Int32)
        Dim ucLogin = DirectCast(Page.LoadControl("LoginControl.ascx"), LoginControl)
        ucLogin.UserID = tabID
        AddHandler ucLogin.LoggedIn, AddressOf userLoggedIn
        Dim newTabPanel = New AjaxControlToolkit.TabPanel
        newTabPanel.ID = String.Format("Tab{0}", tabID)
        newTabPanel.HeaderText = String.Format("TabPanel {0}", tabID)
        newTabPanel.Controls.Add(ucLogin)
        TabContainer1.Tabs.Add(newTabPanel)
    End Sub

    Protected Sub UserChanged(sender As Object, e As EventArgs)
        Dim tabID = Int32.Parse(DirectCast(sender, ListBox).SelectedValue)
        If Not CreatedTabIDs.Contains(tabID) Then
            AddTab(tabID)
            CreatedTabIDs.Add(tabID)
            Me.UpdTabContainer.Update()
        End If
    End Sub

    Private Sub userLoggedIn(login As LoginControl)
        ' do something '
    End Sub

End Class

The UserControl, for example a Login-Control:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="LoginControl.ascx.vb" Inherits="WebApplication1.LoginControl" %>
<asp:UpdatePanel ID="UpdLoginControl" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    User-ID: <asp:Label ID="LblUserID" runat="server" Text="0"></asp:Label>
    <br /><asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>
    <br /><asp:TextBox ID="TxtPassord" runat="server"></asp:TextBox>
    <br /><asp:Button ID="BtnLogin" runat="server" Text="Login" />
    </ContentTemplate>
</asp:UpdatePanel>

Codebehind:

Public Class LoginControl
    Inherits System.Web.UI.UserControl

    Public Event LoggedIn(sender As LoginControl)

    Public Property UserID As Int32
        Get
            Return Int32.Parse(Me.LblUserID.Text)
        End Get
        Set(value As Int32)
            Me.LblUserID.Text = value.ToString
        End Set
    End Property

    Private Sub BtnLogin_Click(sender As Object, e As System.EventArgs) Handles BtnLogin.Click
        Dim userName = TxtUserName.Text
        Dim password = TxtPassord.Text
        ' validate UserName and Password, if valid you can raise your custom LoggedIn-Event'
        Dim loggedIn As Boolean = True
        If loggedIn Then
            RaiseEvent LoggedIn(Me)
        End If
        Me.UpdLoginControl.Update()
    End Sub
End Class

If you have further question, ask.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Hi Tim Thanks for your reply. Is BtnLogin_Click event is working? in this event i tried to change the value of the text box when login button is clicked. i.e, i added txtUserName.text="your name" when i clicked the button. but it is not updating when i clicked the button. can you explain it to me? I'm new to ASP.NET. please don't mind if I irritate you for simple questions – krrish Jan 20 '12 at 21:36
  • @krish: Ok, i didn't know that you want to change a TextBox-Text in Button-Click. You should wrap the UserControl's content in it's own UpdatePanel. I've edited my answer to clearify what i mean(notice also the `UpdLoginControl.Update` in codebehind). – Tim Schmelter Jan 22 '12 at 20:43
  • Thank you so much...... Its working...i will post if i find any problems further.... – krrish Jan 23 '12 at 15:42
  • when I fire the text boxes with a send button to send messages, my tab panels are unable to retain its user name values and showing username as blank. I'm doing this to insert message into that particular users interface.....this is the same case when I open multiple tabs. Its not retaining its username values(i.e. listbox.selectedIndex.text). Can you help me with this?? – krrish Jan 27 '12 at 16:11
  • I'm afraid you should better open a new question. Provide your current code and a good description of the issue. It's difficult to help with this little informations. You might want to add a link to your question here as comment. Maybe i can help then, but unfortunately not before sunday :) – Tim Schmelter Jan 27 '12 at 16:14
  • new question at http://stackoverflow.com/questions/9101458/dynamically-deleting-tab-panel-in-tab-container-in-asp-net-using-vb-net please have a look – krrish Feb 01 '12 at 20:17