NinethSense oWnZ mE!: Praveen’s drawing book

Choose a Topic:

Wed
26
Sep '07

Reverse text as you type

Time pass ;)

 
		public string Reverse(string x)
		{
			char[] charArray = new char[x.Length];
			int len = x.Length - 1;
			for (int i = 0; i < = len; i++)
				charArray[i] = x[len-i];
			return new string(charArray);
		}
 
 
		private void txtBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			textBox2.Text = Reverse(textBox1.Text);
		}
'

Reorder listbox items with JavaScript and ASP.NET

Default.aspx:
 
< %@ Page Language=“VB” AutoEventWireup=“false” CodeFile=“Default.aspx.vb” Inherits=“_Default” ValidateRequest=“false”  %>
 
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
 
<html xmlns=“http://www.w3.org/1999/xhtml” >
<head id=“Head1″ runat=“server”>
    <title>Untitled Page</title>
    <style type=“text/css”>
	#lstWishList {
		background-color:lightgrey;
		font:10px notmal arial;
		overflow:visible;;
	}
	
	#divNumbers {
		font:10px notmal arial;
		line-height:13px;
	}
	.imgbutton {
		cursor:pointer;
	}
</style>
<script language=“javascript”>
 
	function LoadNumbers() {
		lst = document.getElementById(“lstWishList”);		
		lst.size = lst.options.length;
		lstNumbers = document.getElementById(“lstWishListNumbers”);		
		div = document.getElementById(“divNumbers”);
		s = “”;
		for (i=1;i<lst .length+1;i++) {
			s += i+“<br>”;
		}
		div.innerHTML = s;
	}
	function ProcessUp(upordown) {
	    if (lst.selectedIndex < 0) return;
		v = (upordown==‘up’)?1:-1;
		lst = document.getElementById(“lstWishList”);
		oldindex = lst.selectedIndex
		newindex = lst.selectedIndex-v
		if (newindex &lt;0 || newindex > lst.options.length-1) return;
		
		
		temp = lst.options[oldindex].text;
		tempv = lst.options[oldindex].value;
		
		lst.options[oldindex].text = lst.options[newindex].text;
		lst.options[oldindex].value = lst.options[newindex].value;
 
		lst.options[newindex].text = temp;
		lst.options[newindex].value = tempv;
		
		lst.selectedIndex = newindex;
	}
	
	function ProcessRemoval() {
	    if (!confirm(“Are you sure you want to remove this item?”)) return;
	    lst = document.getElementById(“lstWishList”);
	    
	    for (i=0;i</lst><lst .options.length;i++) {
	        if (lst.options[i].selected)
	            lst.remove(i);
	    }
	    LoadNumbers();
	}
	
	function PopulateHiddenField(){
	    hid = document.getElementById(“hidListItems”);
	    lst = document.getElementById(“lstWishList”);
	    s = “”;
	    for(i=0;i<lst.options.length;i++) {
	        s += lst.options[i].value + “|” + lst.options[i].text;
	        if (i+1 != lst.options.length) s+= “~”;
	    }
	    hid.value = s;
	}
 
</script>
    
</lst></script></head>
<body>
    <form id=“form1″ runat=“server”>
    <table cellpadding=0 cellspacing=0>
	<tr>
		<td colspan=3 style=“border:solid 1px black”>
<table><tr><td valign=“top” style=“background-color:lightgrey;”>
<div id=“divNumbers”>test</div>
</td><td>
<asp :ListBox ID=“lstWishList” runat=“server”></asp>
</td></tr></table>
</td>
</tr>
 
<tr><td align=center valign=middle><img src=“up.gif” class=“imgbutton” border=1 onclick=“ProcessUp(’up’)”/></td>
<td align=center valign=middle><img src=“down.gif” class=“imgbutton” border=1 onclick=“ProcessUp(’down’)”/></td>
<td align=center valign=middle><img src=“x.gif” class=“imgbutton” border=1 onclick=“ProcessRemoval()”/></td>
</tr>
 
</table>
        <br />
        <asp :Button ID=“Button2″ runat=“server” Text=“Submit Changes” OnClientClick=“PopulateHiddenField()” /><br />
        <br />
        <asp :HiddenField ID=“hidListItems” runat=“server” />
        <br />
        <hr />
        &nbsp;<br />
        Preview of datatable dt :
        <br />
        <br />
        <asp :GridView ID=“GridView1″ runat=“server”>
        </asp>
        
    </form>
</body>
</html>
<script language = “JavaScript”>
	LoadNumbers();
</script>

Default.aspx.vb:
 
Imports System.Data
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim dt As New DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
 
        ‘ Fill sample data in listbox
        Dim dr As DataRow
        dt.Columns.Add(“id”)
        dt.Columns.Add(“title”)
        If (Not IsPostBack) Then
            dr = dt.NewRow() : dr(“id”) = “33″ : dr(“title”) = “My Title” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “13″ : dr(“title”) = “My Title blah” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “1523″ : dr(“title”) = “asd asdf asda” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “1723″ : dr(“title”) = “My Title asdf asdasdasd” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “123″ : dr(“title”) = “2222 My Title” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “1123″ : dr(“title”) = “122222 My Title” : dt.Rows.Add(dr)
 
            lstWishList.DataValueField = “id”
            lstWishList.DataSource = dt
            lstWishList.DataTextField = “title”
 
            lstWishList.DataBind()
        End If
 
    End Sub
 
 
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        ‘ You will get list items as a delimeted string in hidden field - hidListItems.Value - re-fill datatable dt
 
        dt.Clear()
        Dim s() As String = hidListItems.Value.Split(“~”)
        Dim i As Integer
        Dim j As Integer
        Dim dr As DataRow
        For i = 0 To s.Length - 1
            Dim s1() As String = s(i).Split(“|”)
            dr = dt.NewRow() : dr(“id”) = s1(0) : dr(“title”) = s1(1) : dt.Rows.Add(dr)
        Next
        lstWishList.Items.Clear()
        lstWishList.DataValueField = “id”
        lstWishList.DataSource = dt
        lstWishList.DataTextField = “title”
 
        lstWishList.DataBind()
 
        ‘Now you have re-ordered values in datatable dt - use it to update database.
        ‘Like - delete all the wishlist items of this customer, then add this dt list 
 
        ‘data preview in grid view
 
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub
End Class
The CodeProject Microsoft Developer Network Official ASP.NET Forums Microsoft .NET Framework Community Microsoft Most Valuable Professional Kidoos forums Microsoft Visual Studio Developer Home Professional Information Technology Solutions Microsoft Research Home Kerala Microsoft Users Group Community Website