I have a datalist where i have two dropdowns the first to select a table and the second to select the column. So far so go...the problem starts when I go for the next item in the datalist and make a selection on the Dropdownlist "ddlTable". After doing this the second dropdownlist of the previous changes its value to the first possible item. Can you help me out?!
Here is the code:
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- try
- {
- ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
- if (!scriptManager.IsInAsyncPostBack && !IsPostBack)
- {
- DataList_Load(DataListItems);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- protected void DataList_Load(int itens)
- {
- try
- {
- List<int> myList = new List<int>();
- for (int i = 0; i < itens; i++)
- myList.Add(i);
- dlQuery.DataSource = myList;
- dlQuery.DataBind();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- protected void dlQuery_ItemDataBound(object sender, DataListItemEventArgs e)
- {
- try
- {
- if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
- {
- var ddl = e.Item.FindControl("ddlTable") as DropDownList;
- ddlTable_Load(ddl);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- protected void ddlTable_Load(DropDownList ddl)
- {
- try
- {
- Connection cn = new Connection();
- SqlConnection SQLConn = cn.SqlLocalConn;
- SqlCommand SQLcmd = new SqlCommand("stp_Portal_GlobalMgmtQueryItens", SQLConn);
- SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
- ddl.DataSource = cn.ExecuteSqlCmd(SQLConn, SQLcmd);
- ddl.DataTextField = "name";
- ddl.DataValueField = "id";
- ddl.DataBind();
- ddl.Items.Insert(0, "Select one");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
- {
- try
- {
- DropDownList ddl1 = sender as DropDownList;
- if (ddl1.SelectedIndex > 0)
- {
- //string aux = ddl1.UniqueID;
- DropDownList ddl2 = (DropDownList)ddl1.Parent.FindControl("ddlColumn");
- //string aux2 = ddl2.UniqueID;
- ddlColumn_Load(ddl2, ddl1.SelectedValue);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- protected void ddlColumn_Load(DropDownList ddl, string val)
- {
- try
- {
- Connection cn = new Connection();
- SqlConnection SQLConn = cn.SqlLocalConn;
- SqlCommand SQLcmd = new SqlCommand("stp_Portal_GlobalMgmtQueryItemColumns", SQLConn);
- SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
- SQLcmd.Parameters.Add("@intTableId", SqlDbType.Int).Value = val;
- ddl.DataSource = cn.ExecuteSqlCmd(SQLConn, SQLcmd);
- ddl.DataTextField = "name";
- ddl.DataValueField = "id";
- ddl.DataBind();
- ddl.Items.Insert(0, "Select one");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- protected void ddlColumn_SelectedIndexChanged(object sender, EventArgs e)
- {
- try
- {
- DropDownList ddl = sender as DropDownList;
- if (ddl.SelectedIndex > 0)
- {
- //just for test
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }