添加空行

时间:2011-01-03 15:56:38

标签: asp.net vb.net

我需要为用户提供添加行的功能。我无法弄清楚用什么控制来实现这一目标。

例如

带有空文本框的第一行始终可见:

    [first name] [last name] [middle name] 
    add more button

添加更多按钮应该创建另一个空的第一个,最后一个,中间的文本框行。

我在底部有一个提交按钮,用于验证和填充所有内容....同样,如果已有数据,则应使用数据填充累计行数,并在底部添加空文本框字段并添加更多内容按钮....

希望我解释好了......

任何指导都将不胜感激。

我已经尝试过使用转发器和gridview,但我无法弄清楚如何显示和添加空行数据......

我在VB.NET中这样做

1 个答案:

答案 0 :(得分:0)

您可以手动生成文本框,例如作为usercontrol来封装它们和验证(当然,这也可以使用转发器和网格视图):

usercontrol的ASCX:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="NameControls.ascx.vb" Inherits="AJAXEnabledWebApplication1.NameControls" %>
<asp:TextBox ID="TxtFirstName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="ReqFirstName" runat="server" ControlToValidate="TxtFirstName" ErrorMessage="Enter firstname" Display="Dynamic"></asp:RequiredFieldValidator>&nbsp;<asp:TextBox ID="TxtLastName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="ReqLastName" runat="server" ControlToValidate="TxtLastName" ErrorMessage="Enter lastname" Display="Dynamic"></asp:RequiredFieldValidator>&nbsp;<asp:TextBox ID="TxtMiddleName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="ReqMiddleName" runat="server" ControlToValidate="TxtMiddleName" ErrorMessage="Enter middlename" Display="Dynamic"></asp:RequiredFieldValidator>

userControl的代码隐藏:

Public Partial Class NameControls
    Inherits System.Web.UI.UserControl

    Public Property FirstName() As String
        Get
            Return Me.TxtFirstName.Text
        End Get
        Set(ByVal value As String)
            Me.TxtFirstName.Text = value
        End Set
    End Property

    Public Property MiddleName() As String
        Get
            Return Me.TxtMiddleName.Text
        End Get
        Set(ByVal value As String)
            Me.TxtMiddleName.Text = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return Me.TxtLastName.Text
        End Get
        Set(ByVal value As String)
            Me.TxtLastName.Text = value
        End Set
    End Property

    Public Property FirstNameRequired() As Boolean
        Get
            Return Me.ReqFirstName.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.ReqFirstName.Enabled = value
        End Set
    End Property

    Public Property MiddleNameRequired() As Boolean
        Get
            Return Me.ReqMiddleName.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.ReqMiddleName.Enabled = value
        End Set
    End Property

    Public Property LastNameRequired() As Boolean
        Get
            Return Me.ReqLastName.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.ReqLastName.Enabled = value
        End Set
    End Property

End Class

测试页的ASPX:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DyntextBoxes.aspx.vb" Inherits="AJAXEnabledWebApplication1.DyntextBoxes" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DyntextBoxes</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:DropDownList ID="DdlAddRow" runat="server" AutoPostBack="true" >
                    <asp:ListItem Value="-1" Text="select to add" Selected="True" />
                    <asp:ListItem Value="1" Text="1" />
                    <asp:ListItem Value="2" Text="2" />
                    <asp:ListItem Value="3" Text="3" />
                    <asp:ListItem Value="4" Text="4" />
                    <asp:ListItem Value="5" Text="5" />
                    <asp:ListItem Value="6" Text="6" />
                </asp:DropDownList><br />
    <asp:placeholder ID="Placeholder1" runat="server" >
    </asp:placeholder> 
    <asp:Button ID="ButtonSave" runat="server" Text="Save" Width="100px"  />
    </form>
</body>
</html>

Testpage的Testbehind:

Public Partial Class DyntextBoxes
    Inherits System.Web.UI.Page

    Public Property RowCount() As Int32
        Get
            If ViewState("RowCount") Is Nothing Then
                ViewState("RowCount") = 0
            End If
            Return DirectCast(ViewState("RowCount"), Int32)
        End Get
        Set(ByVal value As Int32)
            ViewState("RowCount") = value
        End Set
    End Property

    Private Sub DyntextBoxes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        addRows(RowCount)
    End Sub

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlAddRow.SelectedIndexChanged
        If DdlAddRow.SelectedValue <> "-1" Then
            Dim count As Int16 = Convert.ToInt16(DdlAddRow.SelectedValue)
            addRows(count)
            RowCount += count
        End If
    End Sub

    Private Sub addRows(ByVal count As Int32)
        For i As Int32 = Me.Placeholder1.Controls.Count To (Me.Placeholder1.Controls.Count + count - 1)
            Dim nameControl As NameControls = DirectCast(LoadControl("NameControls.ascx"), NameControls)
            nameControl.ID = "nameControl_" & i
            nameControl.FirstNameRequired = True
            nameControl.MiddleNameRequired = False
            nameControl.LastNameRequired = True
            Me.Placeholder1.Controls.Add(nameControl)
            Me.Placeholder1.Controls.Add(New LiteralControl("<br />"))
        Next
    End Sub

    Private Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        For i As Int32 = 0 To RowCount - 1
            Dim nameControl As NameControls = DirectCast(Me.PlaceHolder1.FindControl("nameControl_" & i), NameControls)
            'Do what you whant with it'
        Next
    End Sub

End Class