Solved

Button click -> add div with text boxes.

Posted on 2013-02-06
5
333 Views
Last Modified: 2013-03-07
Hello,

I would like it so if a click my add button
    <asp:ImageButton id="addButton" src="images/add.PNG" OnClientClick="return false;"  class="addButton" runat="server" />

Open in new window

. It will show the below code
   <label class="alignme"  for="repdist1">Rep1 - </label>
     <label  for="repdist1">Distance:</label>
     <input  onkeypress="return IsNumber(event)" name="repdist1" type="text" id="repsit1" style="width:30px;" />
     <select name="kmormile1" id="kmormile1">
     	<option value="Miles">Miles</option>
        	<option value="Km">Km</option>
            </select>
                       <label for="repmin1">Min:</label>
                     <input onkeypress="return IsNumber(event)" name="repmin1" type="text" value="00" maxlength="2" id="repmin1" style="width:30px;" />
                     
                       <label for="repsec1">Sec:</label>
                     <input onkeypress="return IsNumber(event)" name="repsec1" type="text" value="00" maxlength="2" id="repsec1" style="width:30px;" />
      
       

Open in new window


each time a user clicks the button it adds a new row and if the user clicks the delete button
          <asp:ImageButton id="removeButton" class="removeButton" src="images/close_square.PNG" OnClientClick="return false;"   runat="server" />

Open in new window


it deletes a row.

I can do this in javascript but i would really like to do it in vb.net
0
Comment
Question by:runnerjp2005
  • 2
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
I wonder why you do not use a control like a GridView or a Repeater instead of adding/deleting rows of textboxes. In this case, you can use a repeater bound to a datatable and add/delete rows in datatable and repeater would automatically show new rows.
0
 

Author Comment

by:runnerjp2005
Comment Utility
Its because its a form to fill in.
Basically its a training log and this has several fields.
I have a requirement where i have a rep field where a person could enter multiple reps e.g

rep1
rep2
rep3

so the button allows the user to enter more reps if need be.
0
 

Author Comment

by:runnerjp2005
Comment Utility
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
0
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 500 total points
Comment Utility
Hi,

Check the below code -
<div runat="server" id="container">
        
    </div>

    <asp:Button ID="btnAdd" Text="Add Row" runat="server" OnClick="RowHandler" />
    <asp:Button ID="btnDelete" Text="Delete Row" runat="server" OnClick="RowHandler" />


Protected Sub RowHandler(sender As Object, e As EventArgs)
	Dim b As Button = TryCast(sender, Button)
	Dim lControls As New List(Of Control)()

	If b.ID = "btnAdd" Then
		If Session("controls") IsNot Nothing Then
			lControls = TryCast(Session("controls"), List(Of Control))
			For Each ctrl As Control In lControls
				container.Controls.Add(ctrl)
			Next

			Dim text As New TextBox()
			container.Controls.Add(text)
			lControls.Add(text)
			Session("controls") = lControls
		Else
			Dim text As New TextBox()
			container.Controls.Add(text)
			lControls.Add(text)
			Session("controls") = lControls
		End If
	End If
	'end:btnAdd if
	If b.ID = "btnDelete" Then
		If Session("controls") IsNot Nothing Then
			lControls = TryCast(Session("controls"), List(Of Control))

			If lControls.Count > 0 Then
				Dim ctrl As Control = TryCast(lControls(lControls.Count - 1), Control)
				container.Controls.Remove(ctrl)
				lControls.Remove(ctrl)
				Session("controls") = lControls

				For Each ctrls As Control In lControls
					container.Controls.Add(ctrls)
				Next

			End If

		End If
	End If
	'end:btnDelete if
End Sub

Open in new window


It's the sample code, to give you an idea to design it according to your need!

I basically wrote in C# and i converted it to VB. I think event handling is different in VB, but the logic inside the handler is same :)

Hope it helps u...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now