1
votes

I have a DropDownList Control populated with List of Items from SqlDataSource. The SqlDataSource QueryBuilder chooses the Column_Names from my Database Table.

If the DropDownList is provided with DataTextField="All_Columns" DataValueField="All_Columns" properties, The DropDownList_SelectedIndexChanged() retains the selected input text.

Current Issue: Whereas if the DropDownList is provided with DataTextField="All_Columns" DataValueField="DATA_TYPE" properties, the DropDownList_SelectedIndexChanged() does not retain the text based on the selected input. But it retains the first value from the list of items which satisfies the respective DATA_TYPE present in an Index.

Solution Required: How to Retain the selected input text based on the DATA_TYPE property ? I tried storing the Session["DDLValue"] = DropDownList.SelectedItem.Text but it always retains the first value from the list of items which satisfies the respective DATA_TYPE present in an Index.

i.e. if i choose "e" from The following DropDownList inputs the value retained in DropDownList is **"d"

How to retain "e" i.e. selected text with DATA_TYPE Property.**

COLUMN_NAME  DATA_TYPE  
a            decimal
b            decimal
c            decimal
d            int
e            int
f            varchar
g            varchar  
h            varchar
i            varchar
j            varchar

My aspx code:

<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="SqlDatasource1" DataTextField="All_Columns" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged"  AutoPostBack="true">
            </asp:DropDownList>

<asp:SqlDataSource ID="SqlDatasource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME AS 'All_Columns', DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'MyTable')">
            </asp:SqlDataSource>

C# Code:

protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
     {
         Session["DDLValue"] = DropDownList5.SelectedValue;
         /***Retains wrong text***/
     }
3

3 Answers

1
votes

Try this..

If you are binding your dropdown in pageload,then..

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             DropDownList5.DataSource = SqlDatasource1;
             DropDownList5.DataTextField = "All_Columns";
             DropDownList5.DataValueField = "DATA_TYPE";
             DropDownList5.DataBind();
        }
    }

Markup:

<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true"></asp:ScriptManager>

<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="SqlDatasource1" DataTextField="All_Columns" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged"  AutoPostBack="false" onChange=" abc();">
            </asp:DropDownList>

Javascript:

function abc() {
     var ddl = document.getElementById('DropDownList5');
     var datatype= ddl.options[ddl.selectedIndex].value;
     if(datatype=="int")
     {
       PageMethods.createdynamiccontrols_int();
     }  
}

C# Code:

[System.Web.Services.WebMethod]
     protected void createdynamiccontrols_int()
     {
        //My Logic continues

     }
0
votes

try this might help you

Session["DDLValue"] = DropDownList5.SelectedItem.Value;

or

Session["DDLValue"] = Request.Form[DropDownList5.UniqueID].ToString();

0
votes

Correct Solution :

If "e" is chosen from The following DropDownList inputs the value retained in DropDownList is "e"

COLUMN_NAME  DATA_TYPE  
a            decimal
b            decimal
c            decimal
d            int
e            int
f            varchar
g            varchar  
h            varchar
i            varchar
j            varchar

Source: DropDownList with repeated data value fields OnSelected get confused and chooses the first one why?

Aspx Code:

<asp:DropDownList ID="DropDownList5" runat="server"  AutoPostBack="true" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" DataSourceID="column_list_for_filter">
</asp:DropDownList>                           

<asp:SqlDataSource ID="column_list_for_filter" runat="server">
</asp:SqlDataSource>

C# code:

protected void Page_Load(object sender, EventArgs e)
    {      
        if (!IsPostBack)
        {
            BindDropDownLists();
        }

        else
        {
            if (!String.IsNullOrEmpty(DropDownList5.SelectedValue))
            {
                if (DropDownList5.SelectedValue.Contains("decimal"))
                {
                    createdynamiccontrols_decimal();
                }

                else if (DropDownList5.SelectedValue.Contains("varchar"))
                {
                    createdynamiccontrols_varchar();
                }
                else if (DropDownList5.SelectedValue.Contains("datetime"))
                {
                    create_cntrls();
                }

                else if (DropDownList5.SelectedValue.Contains("int"))
                {
                    Create();
                }


            }

        }
    }

private void BindDropDownLists()
     {
         column_list_for_filter.ConnectionString = connection;
         column_list_for_filter.SelectCommand = "SELECT DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE))as DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT' AND COLUMN_NAME IN ('a','b','c','d','e','f','g','h','i'))";
         DropDownList5.DataTextField = "COLUMN_NAME";
         DropDownList5.DataValueField = "DATA_TYPE";
         DropDownList5.DataBind();

     }