As we have said (part 1), we are ready to write code for the class UserControls_ListBoxesFT_C. Well! Inside the class we will use variables named according to the properties of the class. For example, the _WidthLB variable corresponds to the C_WidthLB property, _DT_In - to C_DT_In, etc. All properties are simple enough and are written like that:
.............
public int C_SortBy
{
get { return _SortBy; }
set { _SortBy = value; }
}
public int C_WidthLB
{
get { return _WidthLB; }
set { _WidthLB = value; }
}
.............
etc.Some difference have the C_DataIn and C_DataOut properties:
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;}
}
As you can see, first of all we set _DT_In as sorted value (with the help of the method dataTable_Sort) and then set _DT_Out as the cloned structure of the _DT_In. The method dataTable_Sort receives some DataTable and returns the sorted table(according to the _SortBy variable) :
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 < iMax; iCount++)
{
dr = dt.NewRow();
dr[0]= dv[iCount][0];
dr[1] = dv[iCount][1];
dt.Rows.Add(dr);
}
return dt;
}
Pay attention that the C_SortBy property has to be set before the C_DataIn property.
In order to bind the ListBox control we use the buildListBox method:
private void buildListBox(ListBox lb,DataTable dt)
{
lb.DataTextField = dt.Columns[1].Caption;
lb.DataValueField = dt.Columns[0].Caption;
lb.DataSource = dt;
lb.DataBind();
}
On "Load" event we bind our ListBox controls:
if (!IsPostBack)
{
buildListBox(ListBoxFrom, _DT_In);
buildListBox(ListBoxTo, _DT_Out);
}
On "PreRender" event we "rebuild" our control according to set values:
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;
}
else
{
ButtonFrom.Visible = true;
ButtonTo.Visible = true;
}
July Marcela Moreno GomezPosted Nov 9, 2006, 11:29 PM
Hello Michael: I have a problem with user controls that I made. For example, I have a user control with txtName, txtDescription and add button. When I use this controls, and load the product information in textboxs(txtName and txtDescription), I write: ctrProduct.IdProduct = 4; For example In the page load, I write: DataView dvproduct= getproduct(idProduct); txtName.Text = dvproduct.Table.Rows[0]["name"]; This work fine!. But when I click on add button, the IdProduct property is lost. And when I Add this product, i dont have idProduct. ths same occurs when I click on button >> the listbox, in the event onClickFrom, The DataIn is lost. could you help me, please? Thanks!!!