Nov 23, 2014

Posted in , , , ,

ASP. NET 4.5.2 Hosting - HostForLIFE.eu : Move Items from One ListBox to Another ListBox working with C#/VB. NET

With this article, I will be able to make a case for concerning ASP.NET Move Items from One ListBox to Another ListBox. And this is the code that I used in ASP.NET:
http://hostforlife.eu/European-ASPNET-452-Hosting
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Listbox Sample</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<table align="center">
<tr>
<td align="center" >
<b>Courses</b>
</td>
<td></td>
<td>
<b>Selected Courses</b></td>
</tr>
<tr>
<td>
<asp:ListBox ID="lstCourses" runat="server" Height="175px" Width="120px" SelectionMode="Multiple">
<asp:listitem Value="0">Asp.Net</asp:listitem>
<asp:listitem Value="1">C#.Net</asp:listitem>
<asp:listitem Value="2">VB.Net</asp:listitem>
<asp:listitem Value="3">JavaScript</asp:listitem>
<asp:listitem Value="4">Ajax</asp:listitem>
<asp:listitem Value="5">JQuery</asp:listitem>
</asp:ListBox>
</td>
<td>
<table>
<tr>
<td>
<asp:Button ID="btnMoveRight" runat="server" Text=">" Width="45px" onclick="btnMoveRight_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnMoveLeft" runat="server" Text="<" Width="45px" onclick="btnMoveLeft_Click" />
</td>
</tr>
</table>
</td>
<td>
<asp:ListBox ID="lstSelected" runat="server" Height="175px" Width="120px" SelectionMode="Multiple">
</asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class ListBox : System.Web.UI.Page
int student_id = 009;
ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
protected void btnMoveRight_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (lstCourses.SelectedIndex >= 0)
{
for (int i = 0; i < lstCourses.Items.Count; i++)
{
if (lstCourses.Items[i].Selected)
{
if (!arraylist1.Contains(lstCourses.Items[i]))
{
arraylist1.Add(lstCourses.Items[i]);
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!lstSelected.Items.Contains(((ListItem)arraylist1[i])))
{
lstSelected.Items.Add(((ListItem)arraylist1[i]));
}
lstCourses.Items.Remove(((ListItem)arraylist1[i]));
}
lstSelected.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in lstCourses to move";
}
}
protected void btnMoveLeft_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (lstSelected.SelectedIndex >= 0)
{
for (int i = 0; i < lstSelected.Items.Count; i++)
{
if (lstSelected.Items[i].Selected)
{
if (!arraylist2.Contains(lstSelected.Items[i]))
{
arraylist2.Add(lstSelected.Items[i]);
}
}
}
for (int i = 0; i < arraylist2.Count; i++)
{
if (!lstCourses.Items.Contains(((ListItem)arraylist2[i])))
{
lstCourses.Items.Add(((ListItem)arraylist2[i]));
}
lstSelected.Items.Remove(((ListItem)arraylist2[i]));
}
lstCourses.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in lstSelected to move";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string s = string.Empty;
for (int i = 0; i < lstSelected.Items.Count; i++)
{
s=s+ lstSelected.Items[i].ToString();
}
}
}

In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Partial Public Class ListBox
Inherits System.Web.UI.Page
Private student_id As Integer = 9
Private arraylist1 As New ArrayList()
Private arraylist2 As New ArrayList()
Protected Sub btnMoveRight_Click(ByVal sender As Object, ByVal e As EventArgs)
lbltxt.Visible = False
If lstCourses.SelectedIndex >= 0 Then
For i As Integer = 0 To lstCourses.Items.Count – 1
If lstCourses.Items(i).Selected Then
If Not arraylist1.Contains(lstCourses.Items(i)) Then
arraylist1.Add(lstCourses.Items(i))
End If
End If
Next
For i As Integer = 0 To arraylist1.Count – 1
If Not lstSelected.Items.Contains(DirectCast(arraylist1(i), ListItem)) Then
lstSelected.Items.Add(DirectCast(arraylist1(i), ListItem))
End If
lstCourses.Items.Remove(DirectCast(arraylist1(i), ListItem))
Next
lstSelected.SelectedIndex = -1
Else
lbltxt.Visible = True
lbltxt.Text = "Please select atleast one in lstCourses to move"
End If
End Sub
Protected Sub btnMoveLeft_Click(ByVal sender As Object, ByVal e As EventArgs)
lbltxt.Visible = False
If lstSelected.SelectedIndex >= 0 Then
For i As Integer = 0 To lstSelected.Items.Count – 1
If lstSelected.Items(i).Selected Then
If Not arraylist2.Contains(lstSelected.Items(i)) Then
arraylist2.Add(lstSelected.Items(i))
End If
End If
Next
For i As Integer = 0 To arraylist2.Count – 1
If Not lstCourses.Items.Contains(DirectCast(arraylist2(i), ListItem)) Then
lstCourses.Items.Add(DirectCast(arraylist2(i), ListItem))
End If
lstSelected.Items.Remove(DirectCast(arraylist2(i), ListItem))
Next
lstCourses.SelectedIndex = -1
Else
lbltxt.Visible = True
lbltxt.Text = "Please select atleast one in lstSelected to move"
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim s As String = String.Empty

For i As Integer = 0 To lstSelected.Items.Count – 1
s = s & lstSelected.Items(i).ToString()
Next
End Sub
End Class


The output from the above method as shown inside the under picture. Whenever you choose the item in left side ListBox and Click on '' button then your selected item such as JavaScript can move to right side ListBox.

ASP. NET 4.5.2 Hosting : Move Items from One ListBox to Another ListBox working with C#/VB. NET

Sep 16, 2014

Posted in , , , ,

ASP.NET 4.5.2 Hosting - HostForLIFE :: ASP.NET Auto-Suggest Textbox

The AutoCompleteExtender control was used with an ASP.NET textbox. Using the ASP.NET Ajax controls makes the auto complete work very easy.

You want to make sure that one and only one ScriptManager control is on the page. Inside the ScriptManager control you want to include the web service that the AutoCompleteExtender control will be communicating with.<asp:ScriptManager ID=”ScriptManager2″ runat=”server” >
<Services>
<asp:ServiceReference Path=”MyWebService.asmx” />
</Services>
</asp:ScriptManager> 

http://www.hostforlife.eu/European-ASPNET-452-Hosting


You want to add an ASP.NET textbox control. Make sure to set theautocomplete=”false” this will prevent suggestions of what you have entered before from being shown from the browser.<asp:TextBox ID=”TextBox1″ runat=”server” CssClass=”suggestText” autocomplete=”off” />

Add the AutoCompleteExtender control. Here is an example to what I set the attributes to.
<ajaxToolkit:AutoCompleteExtender
ID=”AutoCompleteExtender2″
runat=”server”
TargetControlID=”TextBox1″ /* The textbox the extend.*/
CompletionInterval=”10″ /* How many milliseconds to wait.*/
CompletionSetCount=”10″ /* How many results to show.*/
MinimumPrefixLength=”1″ /* How many letters need to be typed.*/
ServicePath=”MyWebService.asmx” /* Path to the web service.*/
ServiceMethod=”AutoSuggest” /* The web services method.*/ />


Now create your webservice.
[WebMethod]
public string[] AutoSuggest(string prefixText, int count)
{
DataView dvStates = new DataView(clsGlobal.dtStates, string.Format(“StateName like ‘{0}%’”, prefixText), “”, DataViewRowState.CurrentRows);System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>(count);for (int i = 0; i < dvStates.Count; i++)
{
items.Add(dvStates[i]["StateName"].ToString());
}
return items.ToArray();
} 


This webservice searches through a DataView of states where the name of the State, “StateName”, starts with the letters that the user entered. Here’s how it works:
  1. The DataView is populated from a DataTable, clsGlobal.dtStates, that gets the States from a database.
  2. A generic list is created to hold the results in an array, the count of the list is the value you specified as CompletionSetCount in the AutoCompleteExtender control.
  3. A for loop is used to loop through the results from the DataView and enters them in the array.
  4. The array is return to be loaded in the AutoCompleteExtendercontrol.

Remember the parameters for the method have to be (string prefixText, int count) or the AutoCompleteExtender will not work properly.