Re: DropDownList problem



I would try saving the SelectedIndex property of the DropDownList during the
postback, and then set the SelectedIndex when the control is recreated.
--
Nathan Sokalski
njsokalski@xxxxxxxxxxx
http://www.nathansokalski.com/

"Julia" <Julia@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:591EFDAC-00C5-4E18-8D12-A2054EEE5F74@xxxxxxxxxxxxxxxx
Hi
I am trying to develop a composite control (i am new in this area). This
control will be compiled into a dll and used by a web page. The control
are
containing a DataGrid and some DropDownLists that are populated from a
dataset.

When I pick a value in a a DropDown I want to apply some RowFilter to my
DataGrid. The problem is that the entire control is rewritten
(CreateChildControls is called) so the value that I picket in the DropDown
are gone. How can I solve this?

I send you the code here aswell:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace ABBPISControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ProductDetails runat=server></{0}:ProductDetails>")]
public class ProductDetails : CompositeControl
{
private DataSet dsProducts = null;

private DropDownList[] drAttrDefault = null;
private Label[] lblAttrHeaders = null;
private DataView[] dvAttrDefault = null;
private DropDownList drAttrAdditional = new DropDownList();
private DataGrid dgProductList = new DataGrid();
private int iDefaultAttr = 0;

private Button btnTest = new Button();


protected override void CreateChildControls()
{
//Gets a dataset from a web service with product information
LoadProducts();

#region dgProductList
dgProductList.AutoGenerateColumns = false;

HyperLinkColumn cName = new HyperLinkColumn();
cName.HeaderText = "TypeCID;
cName.DataNavigateUrlField = "Url";
cName.DataTextField = "ProductId";

BoundColumn bcColumn1 = new BoundColumn();
bcColumn1.HeaderText = "AbbType";
bcColumn1.DataField = "AbbType";

BoundColumn bcColumn2 = new BoundColumn();
bcColumn2.HeaderText = "ClassCID";
bcColumn2.DataField = "ClassCID";

BoundColumn bcColumn3 = new BoundColumn();
bcColumn3.HeaderText = "Title";
bcColumn3.DataField = "Title";

BoundColumn bcColumn4 = new BoundColumn();
bcColumn4.HeaderText = "TypeCID";
bcColumn4.DataField = "TypeCID";

BoundColumn bcColumn5 = new BoundColumn();
bcColumn5.HeaderText = "UOM";
bcColumn5.DataField = "UOM";

dgProductList.Columns.Add(cName);
dgProductList.Columns.Add(bcColumn1);
dgProductList.Columns.Add(bcColumn2);
dgProductList.Columns.Add(bcColumn3);
dgProductList.Columns.Add(bcColumn4);
dgProductList.Columns.Add(bcColumn5);

dgProductList.DataSource = dsProducts.Tables[0];
dgProductList.DataBind();

this.Controls.Add(dgProductList);
#endregion

#region drAttrDefault & lblAttrHeaders
iDefaultAttr = dsProducts.Tables["AttributeHeader"].Rows.Count;
drAttrDefault = new DropDownList[iDefaultAttr];
lblAttrHeaders = new Label[iDefaultAttr];
dvAttrDefault = new DataView[iDefaultAttr];
int i = 0;
ArrayList alAttrHeaders = new ArrayList();
foreach (DataRow drAttr in
dsProducts.Tables["AttributeHeader"].Rows)
{
if (drAttr["Default"].ToString() == "1")
{
//Create the new DataView and RowFilter
dvAttrDefault[i] = new
DataView(dsProducts.Tables["AttributeValue"]);
dvAttrDefault[i].RowFilter = "AttrId = '" +
drAttr["AttrId"].ToString() + "'";

//Cretate the Label for the header
lblAttrHeaders[i] = new Label();
lblAttrHeaders[i].Text = drAttr["Name"].ToString();

//Create the DropDown and bind it to the datasourece
drAttrDefault[i] = new DropDownList();
drAttrDefault[i].AutoPostBack = true;
drAttrDefault[i].DataSource = dvAttrDefault[i];
drAttrDefault[i].DataValueField = "AttrId";
drAttrDefault[i].DataTextField = "Count";
drAttrDefault[i].DataBind();

drAttrDefault[i].SelectedIndexChanged += new
EventHandler(ProductDetails_SelectedIndexChanged);

//Add controls to page
this.Controls.Add(lblAttrHeaders[i]);
this.Controls.Add(drAttrDefault[i]);
i++;
alAttrHeaders.Add(drAttr["AttrId"]);
}
}
iDefaultAttr = i;
#endregion

#region drAttrAdditional
drAttrAdditional.AutoPostBack = true;
dsProducts.Tables["AttributeHeader"].DefaultView.RowFilter =
"Default = '0'";
drAttrAdditional.DataSource =
dsProducts.Tables["AttributeHeader"].DefaultView;
drAttrAdditional.DataValueField = "AttrId";
drAttrAdditional.DataTextField = "Name";
drAttrAdditional.DataBind();
drAttrAdditional.SelectedIndexChanged += new
EventHandler(myDropDown_SelectedIndexChanged);
this.Controls.Add(drAttrAdditional);
#endregion

btnTest.Text = "Test";
btnTest.Click += new EventHandler(btnTest_Click);
this.Controls.Add(btnTest);

//base.CreateChildControls();

}
protected override void RenderContents(HtmlTextWriter output)
{
dgProductList.RenderControl(output);
for (int i = 0; i < iDefaultAttr; i++)
{
lblAttrHeaders[i].RenderControl(output);
output.Write("<br>");
drAttrDefault[i].RenderControl(output);
output.Write("<br>");
}
drAttrAdditional.RenderControl(output);

btnTest.RenderControl(output);
}
#region Events
void myDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("test");
}
void btnTest_Click(object sender, EventArgs e)
{
Console.WriteLine("test");
}

void ProductDetails_SelectedIndexChanged(object sender, EventArgs
e)
{
Console.WriteLine("test");
}
#endregion

#region Private functions
private void LoadProducts()
{
ABBPortalBusinessComponents.Product oProducts = new
ABBPortalBusinessComponents.Product();
dsProducts = oProducts.GetProducts("Default.aspx");
}
#endregion


}
}

Thanks
J



.