Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Problem with second dropdownlist in DataList

$
0
0

Hello everyone,

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 after the first selection on the first datalist item.

Let me example:

example

On the second datalist item if I click on the next table name the first dropdownlist lose the selected value and is it seems to be rebinded.

Can you help me out?!

Here is my code:

<asp:DataList ID="dlQuery" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
                EnableViewState="true" OnItemDataBound="dlQuery_ItemDataBound"><ItemTemplate><table><tr><td align="right" style="height: 20px"><asp:Label ID="lblTable" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
                                    Text="Table" ForeColor="#005780"></asp:Label></td><td align="left"><asp:DropDownList ID="ddlTable" runat="server" AutoPostBack="True" Font-Names="Verdana"
                                    Font-Size="7pt" OnSelectedIndexChanged="ddlTable_SelectedIndexChanged" OnDataBound="ddlTable_DataBound"></asp:DropDownList></td></tr><tr><td align="right" style="height: 20px"><asp:Label ID="lblColumn" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
                                    Text="Column" ForeColor="#005780"></asp:Label></td><td align="left"><asp:DropDownList ID="ddlColumn" runat="server" Font-Names="Verdana" Font-Size="7pt"></asp:DropDownList></td></tr><tr><td align="right" style="height: 20px"><asp:Label ID="lblToShow" runat="server" Font-Bold="true" Font-Names="Verdana" Font-Size="7pt"
                                    Text="To Show" ForeColor="#005780"></asp:Label></td><td align="left"><asp:CheckBox ID="cbToShow" runat="server" Font-Names="Verdana"
                                    Font-Size="7pt" /></td><tr><td align="right" style="height: 20px"><asp:Label ID="lblCrit" runat="server" Font-Bold="true" Font-Names="Verdana"
                                        Font-Size="7pt" ForeColor="#005780" Text="Criteria"></asp:Label></td><td align="left"><asp:TextBox ID="txtCrit" runat="server" Font-Names="Verdana" Font-Size="7pt"
                                        Width="95%"></asp:TextBox></td></tr><tr><td align="right" style="height: 20px"><asp:Label ID="lblOr" runat="server" Font-Bold="true" Font-Names="Verdana"
                                        Font-Size="7pt" ForeColor="#005780" Text="Or"></asp:Label></td><td align="left"><asp:TextBox ID="txtOr" runat="server" Font-Names="Verdana" Font-Size="7pt"
                                        Width="95%"></asp:TextBox></td></tr></tr></table></ItemTemplate><SeparatorTemplate><table><tr><td align="right" style="width: 20px"></td></tr><tr><td align="right" style="width: 20px"></td></tr><tr><td align="right" style="width: 20px"></td></tr><tr><td align="right" style="width: 20px"></td></tr><tr><td align="right" style="width: 20px"></td></tr></table></SeparatorTemplate></asp:DataList>

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
                if (!scriptManager.IsInAsyncPostBack && !IsPostBack)
                {
                    GetPageHelp();
                    DataList_Load(DataListItems);
                }
            }
            catch (Exception ex)
            {
                ShowError(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)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddlTable");
                DropDownList ddl2 = (DropDownList)e.Item.FindControl("ddlColumn");
                if (ddl1 != null)
                {
                    ddlTable_Load(ddl1);
                }
                if (ddl2 != null)
                {
                    ddlColumn_Load(ddl2, "");
                }
            }
        }

        protected void ddlTable_Load(DropDownList ddl)
        {
            try
            {
                Connection cn = new Connection();
                DataSet ds = new DataSet();
                SqlConnection SQLConn = cn.SqlLocalConn;
                SqlCommand SQLcmd = new SqlCommand("stp_Portal_GlobalMgmtQueryItens", SQLConn);

                SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;

                ds = cn.ExecuteSqlCmd(SQLConn, SQLcmd);
                if (ds.Tables.Count > 0)
                {
                    ddl.Items.Insert(0, "Select one");
                    ddl.DataSource = ds;
                    ddl.DataTextField = "name";
                    ddl.DataValueField = "id";

                    ddl.DataBind();
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }

        }

        protected void ddlTable_DataBound(object sender, EventArgs e)
        {
            ((DropDownList)sender).Items.Insert(0, new ListItem("--Select--", "0"));
        }

        protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList ddl2 = (DropDownList)ddl.Parent.FindControl("ddlColumn");
                if (ddl.SelectedValue != "0")
                {
                    ddlColumn_Load(ddl2, ddl.SelectedValue);
                }

                //Check if it is the last Datalist item
                dlQuery_IsLastItem(((System.Web.UI.WebControls.DataListItem)(ddl.Parent)).ItemIndex);
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
        }

        protected void ddlColumn_Load(DropDownList ddl, string value)
        {
            try
            {
                if (value != "")
                {
                    ddl.Enabled = true;
                    Connection cn = new Connection();
                    DataSet ds = new DataSet();
                    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 = value;

                    ds = cn.ExecuteSqlCmd(SQLConn, SQLcmd);
                    if (ds.Tables.Count > 0)
                    {
                        ddl.DataSource = ds;
                        ddl.DataTextField = "name";
                        ddl.DataValueField = "id";
                        ddl.DataBind();
                    }
                }
                else
                {
                    ddl.Items.Clear();
                    ddl.Enabled = false;
                    ddl.Items.Insert(0, "Select table first");
                    ddl.DataBind();
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
        }





Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>