//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.ComponentModel;
namespace OrmLib{
[EditorBrowsable(EditorBrowsableState.Never)]
public class WebHelpers
{
private WebHelpers(){}
public static string GenerateRandomString()
{
string result = "";
char[] letters = {'b','c','d','2','f','g','h','3','j','k','m','n','4','p','q','r','s','t','5','v','w','x','y','z' };
System.Random r = new System.Random();
for(int i = 0; i < 8; i++)
{
int j = r.Next( 24);
char c = letters[j];
result += c;
}
return result;
}
public static void AddTableRow( Table t, params object[] values)
{
TableRow tr = new TableRow();
int Pos = 0;
foreach( object s in values)
{
TableCell cell = new TableCell();
if (s == null)
cell.Text = " ";
else if ((s is string) && ((string)s == ""))
cell.Text = " ";
else if (!(s is string))
{
System.Diagnostics.Debug.WriteLine( "Adding a control");
cell.Controls.Add( (System.Web.UI.Control)s);
}
else
cell.Text = (string)s;
if (t.Rows.Count > 0 )
{
cell.CopyBaseAttributes( t.Rows[0].Cells[Pos]);
cell.VerticalAlign = t.Rows[0].Cells[Pos].VerticalAlign;
cell.HorizontalAlign = t.Rows[0].Cells[Pos].HorizontalAlign;
}
tr.Cells.Add(cell);
Pos++;
}
t.Rows.Add(tr);
}
public static Hashtable GetSelectionsHash( ListItemCollection c)
{
Hashtable h = new Hashtable();
foreach( ListItem li in c)
{
if (li.Selected == true) h.Add( li.Value, li.Value);
}
return h;
}
public static void PopulateList( DropDownList list, DataTable source, string nameColumn, string valueColumn, string selectedValue)
{
list.Items.Clear();
foreach( DataRow r in source.Rows)
{
ListItem li = new ListItem( (string) r[nameColumn], (string) r[valueColumn]);
if ( selectedValue == (string) r[valueColumn] ) li.Selected = true;
list.Items.Add( li );
}
}
public static void PopulateList( DropDownList list, ICollection namesValues, string selectedItem)
{
PopulateListNameValues(list, namesValues, namesValues, selectedItem);
}
public static void PopulateListNameValues( DropDownList list, ICollection names, ICollection values, string selectedItem)
{
ArrayList Names = new ArrayList( names );
ArrayList Values = new ArrayList( values );
list.Items.Clear();
for(int i = 0; i < names.Count; i++)
{
ListItem li = new ListItem( (string) Names[i], (string) Values[i]);
if ((string) Values[i] == selectedItem) li.Selected = true;
list.Items.Add(li);
}
}
public static string GetControlSelections( Page page, string name)
{
Control c = page.FindControl( name );
if (c is TextBox)
return ((TextBox)c).Text;
if (c is DropDownList)
return ((DropDownList)c).SelectedItem.Value;
return "";
}
public static void PopulateControlSelections( Page page, string name, string value)
{
Control c = page.FindControl( name );
if (c is TextBox)
{
((TextBox)c).Text = value;
}
if (c is DropDownList)
{
DropDownList l = (DropDownList) c;
foreach(ListItem li in l.Items)
{
if (li.Value == value)
{
li.Selected = true;
break;
}
}
}
}
}
}
|