Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to work with Web Controls in ASP.NET?

Posted on 2010-11-08
17
342 Views
Last Modified: 2012-05-10
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?


Regards,
akohan
0
Comment
Question by:akohan
  • 7
  • 5
  • 3
  • +1
17 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34086406
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)
              ListBox2.Items.Add(ListBox1.SelectedItem);
    }


then do the reverse for the Remove button.
0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34086410
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
</asp:listbox>

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)
   lstSelected.items.add(lstUnSelected.SelectedItem)
   lstUnSelected.Items.remove(lstUnSelected.SelectedItem)
End Sub

Protected sub cmdRemove_Click(sender as object, e as eventargs)
   lstUnSelected.Items.add(lstSelected.SelectedItem)
   lstSelected.Items.Remove(lstSelected.SelectedItem)
End Sub

0
 

Author Comment

by:akohan
ID: 34086432


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

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Expert Comment

by:mortimer452
ID: 34086462
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?
0
 

Author Comment

by:akohan
ID: 34086483

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

so can I ignore that?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34086492
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.
0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34086508
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.
0
 

Author Comment

by:akohan
ID: 34086644

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!
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34086682
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.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 34086730
Here is a basic tutorial: http://www.asp.net/ajax/tutorials/understanding-partial-page-updates-with-asp-net-ajax.
Two main things:
1: Add ScriptManager to you page
2: Place the code you want partial postback inside UpdatePanel/contentTemplate

More here:http://www.asp.net/web-forms/ajax
And you don't need AJAX toolkit for this.
0
 

Author Comment

by:akohan
ID: 34086760


Thank you all!
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34238096
Erm, my initial comment answered the original question. The rest of the stuff was outside of the original remit.
0
 

Author Comment

by:akohan
ID: 34252719

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.


Regards.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34252755
In that case hit the "Object" button and post a comment letting the moderators know what you would like to do.
0
 

Author Comment

by:akohan
ID: 34254418

Let me assign a score.
0
 

Author Closing Comment

by:akohan
ID: 34254444
Thanks to all.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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