我正在asp.net中创建一个聊天应用程序。在此我将动态地为每个用户在选项卡容器中创建选项卡面板。当你选择一个新用户时,我正在为他生成一个带有两个文本框的选项卡,并发送按钮来发送消息。如果他不想继续聊天,我需要删除或删除该标签。有什么办法可以使用vb.net删除或删除asp.net中的那些选项卡吗?
答案 0 :(得分:1)
这是一个完整的工作样本,可以分解为这一行(在RemoveTab
中):
Me.TabContainer1.Tabs.Remove(tab)
但是看看自己......
页面-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>
代码隐藏:
Public Class TabContainerSample
Inherits System.Web.UI.Page
Const idSuffix = "_"c
Property CreatedTabIDs As List(Of String)
Get
If Session("CreatedTabIDs") Is Nothing Then
Session("CreatedTabIDs") = New List(Of String)
End If
Return DirectCast(Session("CreatedTabIDs"), List(Of String))
End Get
Set(value As List(Of String))
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 String)
Dim ucChat = DirectCast(Page.LoadControl("ChatControl.ascx"), ChatControl)
ucChat.UserName = tabID
AddHandler ucChat.ChatSubmitted, AddressOf chatSubmitted
AddHandler ucChat.ChatClosed, AddressOf chatClosed
Dim newTabPanel = New AjaxControlToolkit.TabPanel
newTabPanel.ID = String.Format("Tab{0}{1}", idSuffix, tabID)
newTabPanel.HeaderText = String.Format("TabPanel {0}", tabID)
newTabPanel.Controls.Add(ucChat)
TabContainer1.Tabs.Add(newTabPanel)
End Sub
Private Sub RemoveTab(tabID As String)
Dim tab = (From t In Me.TabContainer1.Tabs.Cast(Of AjaxControlToolkit.TabPanel)()
Where t.ID.Contains(idSuffix)
Let id = t.ID.Substring(t.ID.LastIndexOf(idSuffix) + 1)
Where id = tabID
Select t).First
Me.TabContainer1.Tabs.Remove(tab)
Me.CreatedTabIDs.Remove(tabID)
Me.UpdTabContainer.Update()
End Sub
Protected Sub UserChanged(sender As Object, e As EventArgs)
Dim userName = DirectCast(sender, ListBox).SelectedValue
If Not CreatedTabIDs.Contains(userName) Then
AddTab(userName)
CreatedTabIDs.Add(userName)
Me.UpdTabContainer.Update()
End If
End Sub
Private Sub chatSubmitted(chat As ChatControl)
' do something '
End Sub
Private Sub chatClosed(chat As ChatControl)
RemoveTab(chat.UserName)
End Sub
End Class
Chat-UserControl:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ChatControl.ascx.vb" Inherits="WebApplication1.ChatControl" %>
<asp:UpdatePanel ID="UpdChatControl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
Username: <asp:Label ID="LblUserName" runat="server" Text="0"></asp:Label>
<br /><asp:TextBox ID="TxtChat" runat="server" TextMode="MultiLine" Rows="4"></asp:TextBox>
<br /><asp:Button ID="BtnSend" runat="server" Text="Send" />
<br /><asp:Button ID="BtnClose" runat="server" Text="Close" />
</ContentTemplate>
</asp:UpdatePanel>
代码隐藏:
Public Class ChatControl
Inherits System.Web.UI.UserControl
Public Event ChatSubmitted(sender As ChatControl)
Public Event ChatClosed(sender As ChatControl)
Public Property UserName As String
Get
Return Me.LblUserName.Text
End Get
Set(value As String)
Me.LblUserName.Text = value
End Set
End Property
Public Property ChatText As String
Get
Return Me.TxtChat.Text
End Get
Set(value As String)
Me.TxtChat.Text = value
End Set
End Property
Private Sub BtnSend_Click(sender As Object, e As System.EventArgs) Handles BtnSend.Click
RaiseEvent ChatSubmitted(Me)
Me.UpdChatControl.Update()
End Sub
Private Sub BtnClose_Click(sender As Object, e As System.EventArgs) Handles BtnClose.Click
RaiseEvent ChatClosed(Me)
Me.UpdChatControl.Update()
End Sub
End Class
如果您还有其他问题,请询问。