How to work with Web Controls in ASP.NET?

hello group,

I'm trying to catch up with .net and now I'm about to design a UI where in there I need to have 2 list-boxes and two buttons in between (as you have seen in regular apps). Buttons are "Add" and "Remove".

Let's say a reseller wants to fill out a form and tell us about which states he is dealing with (to deal with taxing) so when he chooses a state on left list-box he can click "Add" button and add that state to the list box on right and etc ... or remove them.

How can I do such a thing in C# & ASP.NET?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Something like that you might want to consider wrapping in a UserControl.

But basically you just need two ListBox controls and a couple of Buttons. Then, in the Click event handler for the Add button you can do something like:

    protected void AddButton_Click(object sender, EventArgs e)
         if (ListBox1.SelectIndex != -1)

then do the reverse for the Remove button.
This is pseudo-code here, but pretty much like this.

You'll have two listboxes on the page, lets' call them lstUnSelected and lstSelected

<asp:listbox id="lstUnSelected" runat="server" />
<asp:listbox id="lstSelected" runat="server" />

And two buttons, one to "add" and one to "remove"

<asp:button id="cmdAdd" runat="Server" text ="Add" />
<asp:button id="cmdRemove" runat="server" text="Remove" />

First you'll need code to populate the "From" listbox with all the states, or just type them in manually as listitems like this in the aspx page:
<asp:listbox id="lstFrom" runat="Server">
  <asp:listitem value="AL" text="Alabama" />
  <asp:listitem value="AK" text="Alaska" />
 et cetera, et cetera

In your code-behind page, you'll need to handle two events, the cmdAdd_Click and the cmdRemove_Click, with code something like this:

Protected sub cmdAdd_Click (sender as object, e as eventargs)
End Sub

Protected sub cmdRemove_Click(sender as object, e as eventargs)
End Sub

akohanAuthor Commented:

how about when I user clicks on Add or Remove button? how can I avoid submitting the form?

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

If you want to avoid postbacks you'll need to handle moving items from one list to another with Javascript instead -- no ASP.Net code required.  Is there a reason why you don't want it to post back?
akohanAuthor Commented:

no reason it is just I'm not familiar with post back yet! I thought I must avoid that.

so can I ignore that?
Carl TawnSystems and Integration DeveloperCommented:
Alternatively you you can use AJAX to perform a partial postback. Doing a full postback shouldn't really matter, because it is events you react to rather than simply the fact that the page has posted back.
Yes, to handle the OnClick event of the buttons you'll need to postback the form, which will submit the form data and refresh the page.  ASP.Net handles re-populating controls on the page with their old values automagically.

For example, if you also had a couple textboxes on that page, clicking the button to postback won't clear those out, when the page refreshes the values that were there before the click will persist.
akohanAuthor Commented:

OK, the problem is that I'm not familiar with AJAX. do I need to know AJAX to send a piece of information from a listbox A to listbox B?
if yes, then does anybody know a link where I can learn enough to make this work?
I have seen huge book on Ajax but I don't have that time.

Thank you!
Carl TawnSystems and Integration DeveloperCommented:
You don't need to know AJAX specifically. As long as you have the AJAX toolkit referenced from your project then you only really need to drop your controls into an AJAX UpdatePanel; the IDE code generator will plumb in all the background stuff for you.
Here is a basic tutorial:
Two main things:
1: Add ScriptManager to you page
2: Place the code you want partial postback inside UpdatePanel/contentTemplate

More here:
And you don't need AJAX toolkit for this.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akohanAuthor Commented:

Thank you all!
Carl TawnSystems and Integration DeveloperCommented:
Erm, my initial comment answered the original question. The rest of the stuff was outside of the original remit.
akohanAuthor Commented:

Sorry, I have been out of town and busy with unexpected things ... may I choose an answer?
I apologize all for being late. Currently (happens sometimes) I don't see any button on page to choose one or more answers.

Carl TawnSystems and Integration DeveloperCommented:
In that case hit the "Object" button and post a comment letting the moderators know what you would like to do.
akohanAuthor Commented:

Let me assign a score.
akohanAuthor Commented:
Thanks to all.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.