I need to give the user an ability to add rows. i can't figure out what control to use to accomplish this.
for example
first row with empty text boxes is always visible:
[first name] [last name] [middle nam开发者_JS百科e]
add more button
the add more button should create an other empty first,last, middle textbox row.
i have a submit button on the bottom that will validate and populate everything.... similarly if there is already data, accourding number of rows should be populated with data, with an empty textbox field on the bottom with an add more button....
hope i explained it ok...
any guidance would be appreciated.
i've already tried using a repeater and a gridview, but i can't figure out how to show and add empty rows of data...
i'm doing it in VB.NET
You could generate your Textboxes manually, for example as usercontrol to encapsulate them and the validation(but of course this is also possible with repeaters and gridviews):
ASCX of the usercontrol:
<%@ 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> <asp:TextBox ID="TxtLastName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="ReqLastName" runat="server" ControlToValidate="TxtLastName" ErrorMessage="Enter lastname" Display="Dynamic"></asp:RequiredFieldValidator> <asp:TextBox ID="TxtMiddleName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="ReqMiddleName" runat="server" ControlToValidate="TxtMiddleName" ErrorMessage="Enter middlename" Display="Dynamic"></asp:RequiredFieldValidator>
Codebehind of the 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 of the testpage:
<%@ 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>
Codebehind of the Testpage:
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
精彩评论