Databound control in a user control maintaining state

Superfly1611

Regular
Joined
Feb 17, 2004
Messages
66
Location
UK
Hello,

I'm having problems with a databound dropdownlist in a user control i'm creating.
I have a user control that contains a textbox, a checkbox, a dropdownlist, a button and a repeater. This user control is inside a PlaceHolder control.

Using the textbox, checkbox and dropdownlist to specify criteria the user presses the button to search a list of products the result of which are bound to the repeater.
The items in the dropdownlist is databound from a database.

But when the search button is clicked the information in the drop down list is lost.

My Code......

My ASPX.....
Code:
<td class="content">
	<asp:PlaceHolder id="contentPanel" runat="server" EnableViewState="False"></asp:PlaceHolder>
</td>
Code behind ASPX
Code:
override protected void OnInit(EventArgs e)
{
	//
	// CODEGEN: This call is required by the ASP.NET Web Form Designer.
	//
	InitializeComponent();
	base.OnInit(e);
	LoadControls();			
}

private void LoadControls()
{
	string mode = null;
	string controlPath = null;
	Control ctrl;
	try
	{
		mode = Request.QueryString["mode"].ToString().ToUpper();
	}
	catch
	{}

	switch(mode)
	{
		case "CREATENOW":
			controlPath = "controls/productionplan/CreateNow.ascx";
			break;
		case "VIEW":
			controlPath = "controls/productionplan/PPlanHome.ascx";
			break;
	}
	ctrl = LoadControl(controlPath);
	contentPanel.Controls.Add(ctrl);
}
ASCX
Code:
<table>
	<tr>
		<td colSpan="4">Use the form below to search for the product you are enquiring 
			about</td>
	</tr>
	<tr>
		<td>Name
		</td>
		<td>Brand
		</td>
		<td>Status
		</td>
		<td> </td>
	</tr>
	<tr>
		<td><asp:textbox id="txtName" Runat="server"></asp:textbox></td>
		<td><asp:dropdownlist id="ddlBrand" Runat="server" EnableViewState="True"></asp:dropdownlist></td>
		<td><asp:checkbox id="chkHasPlan" Runat="server" Text="Missing Current Plan"></asp:checkbox></td>
		<td><asp:button id="btnSearch" Runat="server" Text="Search"></asp:button></td>
	</tr>
</table>
<asp:repeater id="productList" Runat="server">
	<HeaderTemplate>
		<table id="reportTable">
			<tr>
				<td colspan="2"><h3 class="centerAlign">Product Details</h3>
				</td>
				<td colspan="3"><h3 class="centerAlign">Production Plans</h3>
				</td>
			</tr>
			<tr>
				<td><h3>Product Code</h3>
				</td>
				<td><h3>Product Name</h3>
				</td>
				<td><h3>Current</h3>
				</td>
				<td><h3>Next</h3>
				</td>
				<td><h3>Archives</h3>
				</td>
			</tr>
	</HeaderTemplate>
	<ItemTemplate>
		<tr>
			<td><%# DataBinder.Eval(Container.DataItem, "ID")%></td>
			<td><%# DataBinder.Eval(Container.DataItem, "Name")%></td>
			<td class="centerAlign"> 
				<asp:HyperLink ID="lnkPlan" Runat="server"></asp:HyperLink> </td>
			<td class="centerAlign"> 
				<asp:HyperLink ID="lnkNext" Runat="server"></asp:HyperLink> </td>
			<td class="centerAlign"> <a href=''>Archives</a> </td>
		</tr>
	</ItemTemplate>
	<FooterTemplate>
		</table>
	</FooterTemplate>
</asp:repeater>
Code behind ascx....
Code:
private void BindBrandDDL()
{
	string mySQL;
	DataAccess myData;
	
	mySQL = "Select Brand_ID, Brand_Description from Brand";
	myData = new DataAccess(mySQL);
	DataTable tempDT;
	tempDT = myData.getDataTable();
	ddlBrand.DataSource = tempDT;
	ddlBrand.DataTextField = "Brand_Description";
	ddlBrand.DataValueField = "Brand_ID";
	ddlBrand.DataBind();
	ListItem li = new ListItem("--SELECT A Brand--", "");
	ddlBrand.Items.Insert(0, li);
}

private void Page_Load(object sender, System.EventArgs e)
{
	if(Page.IsPostBack == false)
	{
		BindBrandDDL();
	}

 	//There is more code here but it isn't relevant
}

private void btnSearch_Click(object sender, System.EventArgs e)
{
        //Didn't think this was relevant to add in but it is the event that triggers the problem so I included it
	string mySQL;
	DataAccess myData;
	DataTable tempDT;

	mySQL = "SELECT p.product_id ID, b.brand_description brand, p.Product_ShortName Name FROM product p, brand b WHERE p.brand_id = b.brand_id AND p.status_id=1";
	if(txtName.Text.Length > 0)
	{
		string criteria = txtName.Text.Replace("'","''");
		mySQL += " AND p.Product_LongName Like '%" + criteria + "%'";
	}
	if(ddlBrand.SelectedIndex >= 0)
	{
		mySQL += " AND p.Brand_ID = '" + ddlBrand.SelectedValue + "'";
	}

	myData = new DataAccess(mySQL);
	tempDT = new DataTable();
	tempDT = myData.getDataTable();
	myData.Dispose();

	if(chkHasPlan.Checked == true)
	{
		foreach(DataRow row in tempDT.Rows)
		{
			int count;
			mySQL = "SELECT count(*) FROM production_plan_index WHERE product_id = " + row["id"].ToString() + " AND Period_ID = " + periodID.ToString();
			myData = new DataAccess(mySQL);
			count = (int)myData.getValue();
			myData.Dispose();
			
			if(count > 0)
			{
				row.Delete();
			}
		}
		tempDT.AcceptChanges();
	}
	productList.DataSource = tempDT;
	productList.DataBind();

}
Any ideas?
 
Fixed it - placeholder's enable viewstate property needed setting to true and then an quick fix of the application logic sorted it.
 
Back
Top