dito1710

dito1710

  • NA
  • 9
  • 0

Databound Controls in a User Control Note maintaining viewstate.

Mar 12 2005 12:35 PM
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 behind ASPX 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; controlPath = "controls/productionplan/PPlanHome.ascx"; break; } ctrl = LoadControl(controlPath); contentPanel.Controls.Add(ctrl); } ASCX
Use the form below to search for the product you are enquiring about
Name Brand Status  

Product Details

Production Plans

Product Code

Product Name

Current

Next

Archives

<%# DataBinder.Eval(Container.DataItem, "ID")%> <%# DataBinder.Eval(Container.DataItem, "Name")%>          Archives 
Code behind ascx.... 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?

Answers (2)