After a couple of days of banging my head agaist the wall I am about to give up. I believe that what i am trying to do should be achieveable and that I am most of the way there.
I am attempting to create a product configuration page. The product ID is passed to the page via a querystring. I then retrieve a list of options that can be configured for that product from a SQL database. For each option I create a dropdown list. Each of these dropdowns are linked via an AJAX CascadingDropDown Control. Once the user has selected an option in all dropdown lists I want to concatenate the values of each dropdowns selectedvalue into a single string and then query the SQL database for a corresponding Product ID and display that on the page.
So far I have managed to create the cascading dropdowns and display them on the page. This much is working. However, when I attempt to retrieve the selected values I am getting nothing, Literally. Though the controls are on the page i cannot find them in the panel which I have placed them. I am fairly sure this is to do with losing the value on postback, but I am stumped as to how to fix it.
I would consider myself a beginner in the world of c# and .NET so any help would be much appreciated.
Below is the stripped out code to show where I am at the moment.
<asp:UpdatePanel ID="updItemDetails" runat="server"><ContentTemplate> Item No: <asp:Label ID="lblItemNo" runat="server">---</asp:Label></ContentTemplate><Triggers><asp:AsyncPostBackTrigger ControlID="cmdCalculate" EventName="click" /></Triggers> </asp:UpdatePanel> <asp:Panel ID="phOptions" runat="server"></asp:Panel> <asp:Button ID="cmdCalculate" runat="server" Text="Calculate" OnClick="cmdCalculate_Click" />
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int NodeID = Convert.ToInt32(Request.QueryString["NodeID"]);
if (NodeID != null)
{
PageInfoByNode(NodeID);
}
}
}
protected void PageInfoByNode(Int32 NodeID)
{
//Get Item Info From Shop
string sql = "Select ConfigItem From Table1 Where Node=@NodeID";
SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Shop"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@NodeID", SqlDbType.Int);
cmd.Parameters["@NodeID"].Value = NodeID;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//Add Product Options
AddOptions(dr["ConfigItem"].ToString());
}
dr.Close();
conn.Close();
}
protected void AddOptions(string ConfigItem)
{
string sql = "SELECT ConfigID, Configurable FROM Table2 WHERE ConfigItem = @ConfigItem)";
SqlConnection connParent = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Shop"].ConnectionString);
SqlCommand cmdParent = new SqlCommand(sql, connParent);
cmdParent.CommandType = CommandType.Text;
cmdParent.Parameters.Add("@ConfigItem", SqlDbType.Char, 20);
cmdParent.Parameters["@ConfigItem"].Value = ConfigItem;
SqlDataAdapter adapter = new SqlDataAdapter(cmdParent);
DataTable dt = new DataTable();
adapter.Fill(dt);
int i = 0;
foreach (DataRow row in dt.Rows)
{
i++;
//Create Dropdown List
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + i;
//Create Dropdown Extender
CascadingDropDown ddlE = new CascadingDropDown();
ddlE.ID = "ddlE" + i;
ddlE.TargetControlID = ddl.ID;
ddlE.Category = row["ConfigID"].ToString();
ddlE.PromptText = row["Configurable"].ToString();
ddlE.ServicePath = "/ConifgOptions.asmx";
ddlE.ServiceMethod = "GetOptions";
//Set Parent Dropdown for all child items
if (i > 1)
{ ddlE.ParentControlID = "ddl" + (i - 1).ToString(); }
//Add Controls to panel
phOptions.Controls.Add(ddl);
phOptions.Controls.Add(ddlE);
}
}
protected void cmdCalculate_Click(object sender, EventArgs e)
{
string orderingNumber;
foreach (Control c in phOptions.Controls)
{
if (c is DropDownList)
{
DropDownList pcddl = (DropDownList)c;
orderingNumber += pcddl.SelectedItem.Value;
}
}
lblItemNo.Text = orderingNumber;
}