扩展和基本地址框

时间:2011-10-05 20:17:10

标签: asp.net user-controls

我的Web应用程序中有一个用户控件。它是输入通讯地址。

有2种模式可输入地址:extended and basic

extended中,我们在不同的控件中输入address1,address2,address3,city,state,zip和country。在basic mode中,我们在一个简单的多行文本框中输入所有上述详细信息。< / p>

切换按钮用于在这些模式之间切换。

如果我从扩展切换到基本,它会起作用,因为用vbcrlf添加所有非空文本框很简单,并将其显示在多行文本框中。

我的问题是当我们从基本切换到扩展时。我想知道是否有空行。 如果你能为我提供具有相同功能的现有用户控件,我会更好。

感谢。

===================================

Extended Mode - I enter every thing individually in different textboxes.

+-------------+
|Address 1    |
+-------------+
|             |
+-------------+
|Address 3    |
+-------------+
|             |
+-------------+
|State        |
+-------------+
|             |
+-------------+
|Country      |
+-------------+

          Toggle    


Present scenario - The array

array(0) = "Address1"
array(1) = ""
array(2) = "Address2"
array(3) = ""
array(4) = "State"
array(5) = ""
array(6) = "Country"


I click on the Toggle and switch to Basic mode. Here I am displaying all the address lines which are not empty in a long text box with multiline = True.

+-------------+
|Address1     |
|Address3     |
|State        |
|Country      |
|             |
+-------------+

    Toggle

Now, i will edit the address and delete the Address3 which is on the line 2.

+-------------+
|Address1     |
|State        |
|Country      |
|             |
+-------------+

    Toggle

Now when i click on the toggle, i need to see the textboxes in the extended mode are having the correct values.

+-------------+
|Address 1    |
+-------------+
|             |
+-------------+
|             |
+-------------+
|             |
+-------------+
|State        |
+-------------+
|             |
+-------------+
|Country      |
+-------------+

          Toggle    

I think, it is clear. i need to go by line by line only as i can enter the same text in two lines.

for example
City - New York.
State - New York.

in the above case, i need to identify which new york (City / State) is deleted / modified.

Thanks.
-=====================

1 个答案:

答案 0 :(得分:0)

好的,根据您编辑过的问题,我创建了一个适合您的UserControl。当然,您必须根据您的要求更改布局:

<强> ASCX

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MultiFieldTextbox.ascx.vb" Inherits="WebApplication2.MultiFieldTextbox" %>
<asp:Panel ID="PnlBasic" Visible="false" runat="server">
    <asp:TextBox ID="TxtFields" runat="server"  TextMode="MultiLine" Width="200" Height="200"></asp:TextBox>
</asp:Panel>
<asp:Panel ID="PnlExtended" Visible="true" runat="server">
    <asp:TextBox ID="TxtAddress1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtAddress2" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtAddress3" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtCity" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtState" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtZip" runat="server"></asp:TextBox>
    <asp:TextBox ID="TxtCountry" runat="server"></asp:TextBox>
</asp:Panel>
<asp:Button ID="BtnSwitchMode" runat="server" Text="switch mode" />

<强> ASCX-代码隐藏

Public Class MultiFieldTextbox
    Inherits System.Web.UI.UserControl

    Public Enum ViewMode As Int32
        Basic
        Extended
    End Enum

    Public Property Mode As ViewMode
        Get
            If Me.PnlBasic.Visible Then
                Return MultiFieldTextbox.ViewMode.Basic
            Else
                Return MultiFieldTextbox.ViewMode.Extended
            End If
        End Get
        Set(ByVal value As ViewMode)
            Me.PnlBasic.Visible = value = ViewMode.Basic
            Me.PnlExtended.Visible = value = ViewMode.Extended
            InitNewMode(value)
        End Set
    End Property

    Public Property Addr1 As String
        Get
            Return Me.TxtAddress1.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtAddress1.Text = value.Trim
        End Set
    End Property
    Public Property Addr2 As String
        Get
            Return Me.TxtAddress2.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtAddress2.Text = value.Trim
        End Set
    End Property
    Public Property Addr3 As String
        Get
            Return Me.TxtAddress3.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtAddress3.Text = value.Trim
        End Set
    End Property
    Public Property City As String
        Get
            Return Me.TxtCity.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtCity.Text = value.Trim
        End Set
    End Property
    Public Property State As String
        Get
            Return Me.TxtState.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtState.Text = value.Trim
        End Set
    End Property
    Public Property Zip As String
        Get
            Return Me.TxtZip.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtZip.Text = value.Trim
        End Set
    End Property
    Public Property Country As String
        Get
            Return Me.TxtCountry.Text.Trim
        End Get
        Set(ByVal value As String)
            Me.TxtCountry.Text = value.Trim
        End Set
    End Property

    Private Property FieldInfos As Dictionary(Of String, FieldInfo)
        Get
            If ViewState("FieldInfos") Is Nothing Then
                ViewState("FieldInfos") = New Dictionary(Of String, FieldInfo)
            End If
            Return DirectCast(ViewState("FieldInfos"), Dictionary(Of String, FieldInfo))
        End Get
        Set(ByVal value As Dictionary(Of String, FieldInfo))
            ViewState("FieldInfos") = value
        End Set
    End Property

    Private Sub InitNewMode(ByVal newMode As ViewMode)
        Select Case newMode
            Case ViewMode.Basic
                Dim builder As New Text.StringBuilder
                Dim lineNumber As Int32 = 0
                Dim key = "Addr1"
                Dim field = Me.Addr1
                FieldInfos = New Dictionary(Of String, FieldInfo)

                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))
                key = "Addr2"
                field = Me.Addr2
                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))
                key = "Addr3"
                field = Me.Addr3
                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))
                key = "City"
                field = Me.City
                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))
                key = "State"
                field = Me.State
                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))
                key = "Zip"
                field = Me.Zip
                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))
                key = "Country"
                field = Me.Country
                lineNumber += 1
                If field.Length <> 0 Then builder.Append(String.Format("{0}{1}", field, ControlChars.NewLine))
                FieldInfos.Add(key, New FieldInfo(lineNumber, key, field))

                If builder.Length <> 0 Then builder.Length -= ControlChars.NewLine.Length
                Me.TxtFields.Text = builder.ToString
            Case ViewMode.Extended
                Dim lineNumber As Int32 = 0
                Dim allLines = Me.TxtFields.Text.Split(New String() {ControlChars.NewLine}, StringSplitOptions.None)
                For Each line In allLines
                    lineNumber += 1
                    ' set value via old line number '
                    Dim q = From field In Me.FieldInfos
                                Where field.Value.Line = lineNumber
                                Select field
                    If q.Any Then
                        Me.FieldInfos(q.First.Key).Value = line
                    End If
                Next
                Dim diff = FieldInfos.Count - allLines.Length
                If diff <> 0 Then
                    Dim line As Int32
                    For line = (1 + allLines.Length) To (diff + allLines.Length)
                        ' set value=String.Empty for each missing line '
                        Dim q = From field In Me.FieldInfos
                                    Where field.Value.Line = line
                                    Select field
                        If q.Any Then
                            Me.FieldInfos(q.First.Key).Value = String.Empty
                        End If
                    Next
                End If
                Me.Addr1 = Me.FieldInfos("Addr1").Value
                Me.Addr2 = Me.FieldInfos("Addr2").Value
                Me.Addr3 = Me.FieldInfos("Addr3").Value
                Me.City = Me.FieldInfos("City").Value
                Me.State = Me.FieldInfos("State").Value
                Me.Zip = Me.FieldInfos("Zip").Value
                Me.Country = Me.FieldInfos("Country").Value
        End Select
    End Sub

    Protected Sub BtnSwitchMode_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnSwitchMode.Click
        Dim newMode As ViewMode = If(Me.Mode = ViewMode.Basic, ViewMode.Extended, ViewMode.Basic)
        Me.Mode = newMode
    End Sub

End Class

<Serializable()>
Public Class FieldInfo
    Public Property ID As String
    Public Property Value As String
    Public Property Line As Int32
    Public Sub New(ByVal line As Int32, ByVal ID As String, ByVal Value As String)
        Me.Line = line
        Me.ID = ID
        Me.Value = Value
    End Sub
End Class