?
Solved

Adding multiple control in aspx page

Posted on 2011-05-13
5
Medium Priority
?
259 Views
Last Modified: 2012-06-21
Hi , i have a requirement where user can add multiple work experience means there is block in which 4 text boxes are present like company name, position, technology and working period.Now there is an option for user to "Add Another" work experience.When the user will click on "Add Another" that time another set of 4 text boxes will be opened.Now my concern is that i have no idea how many times the user will click on the "Add Another " option.There is no restriction for the end user.Initially i have planned to create 4 sets of control in which one will be remain opened and rest will be in hide .So when the user will click on the "Add Another " option i ll open the text box set 1 by 1.But when i checked with my manager he is saying there is no limitation.So it is very hard for me to manage all.What i like to develop it should be created on the fly and i want to store the data in XML format and when the user will click on the  final submit button i will store the data in a single submit click to avoid the database round trip of all control's data.But my concern is that if i ll create a user control of 4 text boxes , could i call the user control or lead the user control unlimited time.Now another issue of page post back and view state.I need a best possible solution to achieve my target to crate control dynamically and store the data in the database in best possible way.
0
Comment
Question by:tanu80
5 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 35754744
You manager may not be technically sound (no offense) I would suggest you use your judgement and set the limit to 100 (or far less) .. anyone that has 100 jobs and applying for another one needs help ;)

0
 
LVL 61

Assisted Solution

by:HainKurt
HainKurt earned 668 total points
ID: 35754798
put a limitation on "add another" say 10...
and each time set a hidden varieble to store the count... add items dynamically (change names on the fly using the current counter value (say Exp3, exp3_dateFrom, exp3_DateTo,...) using jQuery or js DOM
all is on client... when user submits finally, you will have some counters in hidden values

<input type=hidden name=expCount />

on posted value, get this count, say 5
then loop your request items like

loop i=1 to Request("expCount ")
  dim exp = Request("Exp" & i)  
  dim expFrom = Request("Exp" & i &"_DateFrom")
  dim expTo = Request("Exp" & i &"_DateTo")
  ...
  and post to db
end loop
0
 
LVL 3

Accepted Solution

by:
magadesign_sviluppo earned 1332 total points
ID: 35754855
hi,
I would have done like this:
page.aspx
<asp:ListView runat="server" ID="LV" OnItemCommand="LV_ItemCommand">
	<ItemTemplate>
		<table>
			<tr>
				<td class="selected_delete">
					<asp:LinkButton ID="lnk_del" runat="server" CommandName="del"></asp:LinkButton>
				</td>
				<td class="selected_num">
					<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>'></asp:Label>
				</td>
				<td class="selected_text">
					<asp:TextBox ID="txt1" runat="server" Text='<%# Eval("txt1") %>' Width="300"></asp:TextBox>
				</td>
				<td class="selected_text">
					<asp:TextBox ID="txt2" runat="server" Text='<%# Eval("txt2") %>' Width="300"></asp:TextBox>
				</td>
				<td class="selected_text">
					<asp:TextBox ID="txt3" runat="server" Text='<%# Eval("txt3") %>' Width="300"></asp:TextBox>
				</td>
				<td class="selected_text">
					<asp:TextBox ID="txt4" runat="server" Text='<%# Eval("txt4") %>' Width="300"></asp:TextBox>
				</td>
			</tr>
		</table>
	</ItemTemplate>
	<LayoutTemplate>
		<table id="itemPlaceholder" runat="server" />
	</LayoutTemplate>
</asp:ListView>
<table class="selected_item">
	<tr>
		<td>
			<asp:LinkButton ID="lnk_addItem" runat="server" OnClick="lnk_addItem_Click">
				Add Item
			</asp:LinkButton>
		</td>
	</tr>
</table>

Open in new window


page.aspx.cs
protected class myControls
{
	public int id { get; set; }
	public string txt1 { get; set; }
	public string txt2 { get; set; }
	public string txt3 { get; set; }
	public string txt4 { get; set; }
	public myControls(int _id, string _txt1, string _txt2, string _txt3, string _txt4)
	{
		id = _id;
		txt1 = _txt1;
		txt2 = _txt2;
		txt3 = _txt3;
		txt4 = _txt4;
	}
}
protected void lnk_addItem_Click(object sender, EventArgs e)
{
	List<myControls> _list = new List<myControls>();
	foreach (ListViewDataItem _item in LV.Items)
	{
		TextBox txt1 = _item.FindControl("txt1") as TextBox;
		TextBox txt2 = _item.FindControl("txt2") as TextBox;
		TextBox txt3 = _item.FindControl("txt3") as TextBox;
		TextBox txt4 = _item.FindControl("txt4") as TextBox;
		Label lbl = _item.FindControl("lbl_id") as Label;
		if (txt != null && lbl != null)
		{
			_list.Add(new myControls(_list.Count + 1, txt1.Text, txt2.Text, txt3.Text, txt4.Text));
		}
	}
	_list.Add(new myControls(_list.Count + 1, "", "", "", ""));
	LV.DataSource = _list;
	LV.DataBind();
}

protected void LV_ItemCommand(object sender, ListViewCommandEventArgs e)
{
	TextBox txt1 = null;
	TextBox txt2 = null;
	TextBox txt3 = null;
	TextBox txt4 = null;
	Label lbl = null;
	List<myControls> _list = new List<myControls>();
	List<myControls> _listOld = new List<myControls>();
	foreach (ListViewDataItem _item in LV.Items)
	{
		txt1 = _item.FindControl("txt1") as TextBox;
		txt2 = _item.FindControl("txt2") as TextBox;
		txt3 = _item.FindControl("txt3") as TextBox;
		txt4 = _item.FindControl("txt4") as TextBox;
		if (txt != null && lbl != null)
		{
			_listOld.Add(new myControls(lbl.Text.ToInt32(), txt1.Text, txt2.Text, txt3.Text, txt4.Text));
		}
	}
	lbl = e.Item.FindControl("lbl_id") as Label;
	if (txt != null && lbl != null)
	{
		myControls _sel = _listOld.SingleOrDefault(x => x.ID == lbl.Text.ToInt32());
		if (_sel != null)
			_listOld.Remove(_sel);
	}
	foreach (myControls _control in _listOld)
	{
		_list.Add(new myControls(_list.Count + 1, _control.txt1, _control.txt2, _control.txt3, _control.txt4));
	}
	LV.DataSource = _list;
	LV.DataBind();
}
protected void Save()
{
	TextBox txt1 = null;
	TextBox txt2 = null;
	TextBox txt3 = null;
	TextBox txt4 = null;
	Label lbl = null;
	List<myControls> _list = new List<myControls>();
	foreach (ListViewDataItem _item in LV.Items)
	{
		txt1 = _item.FindControl("txt1") as TextBox;
		txt2 = _item.FindControl("txt2") as TextBox;
		txt3 = _item.FindControl("txt3") as TextBox;
		txt4 = _item.FindControl("txt4") as TextBox;
		if (txt != null && lbl != null)
		{
			_list.Add(new myControls(lbl.Text.ToInt32(), txt1.Text, txt2.Text, txt3.Text, txt4.Text));
		}
	}
	// do something with the list
}

Open in new window

i have modified code i already had, may have errors, didn't test.
hope it helps
0
 
LVL 3

Assisted Solution

by:magadesign_sviluppo
magadesign_sviluppo earned 1332 total points
ID: 35754880
ooops :)
forgoten
protected void Page_Load(object sender, EventArgs e)
{
	DC_RENTAL = maga_DataContext.DC_RENTAL;
	if (!IsPostBack)
	{
		List<myControls> _list = new List<myControls>();
		_list.Add(new myControls(_list.Count + 1, "", "", "", "")); // one 
		//_list.Add(new myControls(_list.Count + 1, "", "", "", "")); // or two items initial
		LV.DataSource = _list;
		LV.DataBind();
	}
}

Open in new window

0
 

Author Closing Comment

by:tanu80
ID: 36559621
good one
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

862 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