Building the ListBoxesFT_C ASP.NET Web User Control in Visual Studio 2005: Part IV

| Posted in

The full text of the ListBoxesFT_C.ascx.cs is the following (the comments to the code you can find in the part 1, part 2 and part 3):

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class UserControls_ListBoxesFT_C : System.Web.UI.UserControl

{

#region "forClass"

int _WidthLB = 60;

int _HeightLB = 120;

string _LabelFrom = ">>";

string _LabelTo = "<<";

DataTable _DT_In = null;

DataTable _DT_Out = null ;

bool _Client = false;

bool _SortByText = true;

int _SortBy = 1;

#endregion

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

buildListBox(ListBoxFrom, _DT_In);

buildListBox(ListBoxTo, _DT_Out);

if (_Client)

{

fillHidden(_DT_In, HiddenFromText, HiddenFromValue);

fillHidden(_DT_Out, HiddenToText, HiddenToValue);

}

}

else

{

if (_Client)

{

getDT_Hidden();

buildListBox(ListBoxFrom, _DT_In);

buildListBox(ListBoxTo, _DT_Out);

}

}

}

protected void Page_PreRender(object sender, EventArgs e)

{

if (IsPostBack)

{ return; }

ListBoxFrom.Width = Unit.Pixel(_WidthLB);

ListBoxFrom.Height = Unit.Pixel(_HeightLB);

ListBoxTo.Width = Unit.Pixel(_WidthLB);

ListBoxTo.Height = Unit.Pixel(_HeightLB);

ButtonFrom.Text = _LabelFrom;

ButtonTo.Text = _LabelTo ;

ButtonHTMLFrom.Value = _LabelFrom;

ButtonHTMLTo.Value = _LabelTo;

ButtonFrom.Visible = false;

ButtonTo.Visible = false;

ButtonHTMLFrom.Visible = false;

ButtonHTMLTo.Visible = false;

if (_Client)

{

ButtonHTMLFrom.Visible = true;

ButtonHTMLTo.Visible = true;

ButtonHTMLFrom.Attributes.Add("onClick",

"javascript:ButtonHTML_onclick(" +

this.Parent.ID + "." + FindControl(ListBoxFrom.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(ListBoxTo.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenToText.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenToValue.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenFromText.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenFromValue.ID).ClientID + "," +

_SortByText.ToString().ToLower() + "," + "true" + ")");

ButtonHTMLTo.Attributes.Add("onClick",

"javascript:ButtonHTML_onclick(" +

this.Parent.ID + "." + FindControl(ListBoxFrom.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(ListBoxTo.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenToText.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenToValue.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenFromText.ID).ClientID + "," +

this.Parent.ID + "." + FindControl(HiddenFromValue.ID).ClientID + "," +

_SortByText.ToString().ToLower() + "," + "false" + ")");

}

else

{

ButtonFrom.Visible = true;

ButtonTo.Visible = true;

}

}

#region "properties"

public int C_SortBy

{

get { return _SortBy; }

set { _SortBy = value; }

}

public int C_WidthLB

{

get { return _WidthLB; }

set { _WidthLB = value; }

}

public int C_HeightLB

{

get { return _HeightLB; }

set { _HeightLB = value; }

}

public string C_LabelFrom

{

get { return _LabelFrom; }

set { _LabelFrom = value; }

}

public string C_LabelTo

{

get { return _LabelTo; }

set { _LabelTo = value; }

}

public bool C_Client

{

get { return _Client; }

set { _Client = value; }

}

public bool C_SortByText

{

get { return _SortByText; }

set { _SortByText = value; }

}

public DataTable C_DataIn

{

set

{

_DT_In = dataTable_Sort ( value);

//Clones the structure of the _DT_In DataTable

_DT_Out = _DT_In.Clone();

//here you can set default value for the _DT_Out

}

}

public DataTable C_DataOut

{

get{return _DT_Out;}

}

#endregion

//-----------------------------------------------------------------

private DataTable dataTable_Sort(DataTable dt)

{

DataView dv;

DataTable dtHelp;

DataRow dr;

int iCount = 0;

int iMax = dt.Rows.Count;

dtHelp = dt.Copy();

dv = dtHelp.DefaultView;

dv.Sort = dt.Columns[_SortBy].Caption ;

dt.Clear();

for (iCount = 0; iCount <>

{

dr = dt.NewRow();

dr[0]= dv[iCount][0];

dr[1] = dv[iCount][1];

dt.Rows.Add(dr);

}

return dt;

}

//-----------------------------------------------------

private void fillHidden(DataTable dt,

HtmlInputHidden HiddenText,

HtmlInputHidden HiddenValue)

{

int iCount;

int iMax;

string sHiddenText = "";

string sHiddenValue = "";

string sText = "";

string sValue = "";

iMax = dt.Rows.Count;

for (iCount = 0; iCount <>

{

sText = dt.Rows[iCount][1].ToString().Trim();

sValue =dt.Rows [iCount ][0].ToString().Trim();

sHiddenText += sText;

sHiddenValue += sValue;

if (iCount != (iMax - 1))

{

sHiddenText += ",";

sHiddenValue += ",";

}

}

HiddenText.Value = sHiddenText;

HiddenValue.Value = sHiddenValue;

}

//-----------------------------------------------------

private void getDT_Hidden()

{

Array arrFromText;

Array arrFromValue;

Array arrToText;

Array arrToValue;

char[] separator ={ ',' };

DataRow dr;

int iCount;

int iMax;

arrFromText = HiddenFromText.Value.ToString().Trim().Split(separator);

arrFromValue = HiddenFromValue.Value.ToString().Trim().Split(separator);

iMax = arrFromText.Length;

_DT_In.Clear();

for (iCount = 0; iCount <>

{

dr = _DT_In.NewRow();

dr[0] = arrFromValue.GetValue(iCount);

dr[1] = arrFromText.GetValue(iCount);

if (arrFromValue.GetValue(iCount).ToString().Trim() != ""

&& arrFromText.GetValue(iCount).ToString().Trim() != "")

{

_DT_In.Rows.Add(dr);

}

}

arrToText = HiddenToText.Value.ToString().Trim().Split(separator);

arrToValue = HiddenToValue.Value.ToString().Trim().Split(separator);

iMax = arrToText.Length;

_DT_Out.Clear();

for (iCount = 0; iCount <>

{

dr = _DT_Out.NewRow();

dr[0] = arrToValue.GetValue(iCount);

dr[1] = arrToText.GetValue(iCount);

if (arrToValue.GetValue(iCount).ToString ().Trim () != ""

&& arrToText.GetValue(iCount).ToString().Trim() != "")

{

_DT_Out.Rows.Add(dr);

}

}

}

private void onClickFromTo(bool clickFrom)

{

ListBox lb;

DataTable dtFrom;

DataTable dtTo;

int iItemsList = 0;

int iCount = 0;

int iCountHelp = 0;

if (clickFrom)

{

lb = ListBoxFrom;

dtFrom = _DT_In;

dtTo = _DT_Out;

}

else

{

lb = ListBoxTo;

dtFrom = _DT_Out;

dtTo = _DT_In;

}

iItemsList = lb.Items.Count;

for (iCount = 0; iCount <>

{

if (lb.Items[iCount].Selected)

{

DataRow dr;

dr = dtTo.NewRow();

dr[0] = dtFrom.Rows[iCount - iCountHelp][0];

dr[1] = dtFrom.Rows[iCount - iCountHelp][1];

dtTo.Rows.Add(dr);

dtFrom.Rows[iCount - iCountHelp].Delete();

iCountHelp = iCountHelp + 1;

}

}

if (clickFrom)

{

_DT_In = dataTable_Sort( dtFrom.Copy());

_DT_Out = dataTable_Sort ( dtTo.Copy());

}

else

{

_DT_Out = dataTable_Sort( dtFrom.Copy());

_DT_In = dataTable_Sort( dtTo.Copy());

}

buildListBox(ListBoxFrom,_DT_In );

buildListBox(ListBoxTo,_DT_Out );

}

//--------------------------------------------------------------

private void buildListBox(ListBox lb,DataTable dt)

{

lb.DataTextField = dt.Columns[1].Caption;

lb.DataValueField = dt.Columns[0].Caption;

lb.DataSource = dt;

lb.DataBind();

}

//---------------------------------------------------------------

protected override void LoadViewState(object VS_saved)

{

if (VS_saved != null)

{

object[] VS_all = (object[])VS_saved;

if (VS_all[0] != null)

{

base.LoadViewState(VS_all[0]);

}

if (VS_all[1] != null)

{

_DT_In = (DataTable)VS_all[1];

}

if (VS_all[2] != null)

{

_DT_Out = (DataTable)VS_all[2];

}

if (VS_all[3] != null)

{

_Client = (bool)VS_all[3];

}

if (VS_all[4] != null)

{

_SortBy = (int)VS_all[4];

}

if (VS_all[5] != null)

{

_WidthLB = (int)VS_all[5];

}

if (VS_all[6] != null)

{

_HeightLB = (int)VS_all[6];

}

if (VS_all[7] != null)

{

_LabelFrom = (string)VS_all[7];

}

if (VS_all[8] != null)

{

_LabelTo = (string)VS_all[8];

}

if (VS_all[9] != null)

{

_SortByText = (bool)VS_all[9];

}

}

}

protected override object SaveViewState()

{

object VS_base = base.SaveViewState();

object[] VS_all = new object[10];

VS_all[0] = VS_base;

VS_all[1] = _DT_In;

VS_all[2] = _DT_Out;

VS_all[3] = _Client;

VS_all[4] = _SortBy;

VS_all[5] = _WidthLB;

VS_all[6] = _HeightLB;

VS_all[7] = _LabelFrom;

VS_all[8] = _LabelTo;

VS_all[9] = _SortByText;

return VS_all;

}

//---------------------------------------------------------------

protected void ButtonFrom_Click(object sender, EventArgs e)

{

onClickFromTo(true);

}

protected void ButtonTo_Click(object sender, EventArgs e)

{

onClickFromTo(false );

}

}

Comments (0)