1

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***/
     }
Shrivatsan
  • 105
  • 1
  • 18

3 Answers3

1

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

     }
Shrivatsan
  • 105
  • 1
  • 18
Jameem
  • 1,840
  • 3
  • 15
  • 26
  • It still remains the same retaining wrong index. But if i try if(IsPostBack) {// Bind } it retains the very first index of my DropdownList. – Shrivatsan May 07 '14 at 06:52
  • I disabled the AutoPost="true" now it retains the selected text but the SelectedIndex_changed() is not fired – Shrivatsan May 07 '14 at 07:05
  • I hope your problem is the datasource which you have set for the dropdown..Instead of that you can bind your dropdown in code behind..In your case after each pageload when selectedindexchanging the datasource is again reset..So the dropdown selects the first value..You can bind dropdown in your code.. – Jameem May 07 '14 at 07:20
  • Remove datasourceID from your dropdown and bind dropdown in pageload as I shown..Editted the answer.. – Jameem May 07 '14 at 07:28
  • Made the changes. I need to fire the Selected_Index_changed() to call createdynamicwebcontrols() with Autopost="False" for the DropDownlist. How to proceed. – Shrivatsan May 07 '14 at 07:50
  • You cannot fire selectedindexchanged with autpostback="false"..Make it true and check?You must bind dropdown as I shown? – Jameem May 07 '14 at 07:54
  • If i make it Autopostback = "true" and BindDropDown the problem still exists. Any solution ? – Shrivatsan May 07 '14 at 08:04
  • How about JQuery/Javascript to fire the selected_Index_Changed event in client side method – Shrivatsan May 07 '14 at 08:06
  • Editted answer with javascript code..Please check,Regards. – Jameem May 07 '14 at 08:11
  • Noted. I am new to JavaScript. Could you please help me how to check for the Datatype of selectedvalue of DDL is decimal or int or varchar and if satisfied i should call my function() written in .cs file. – Shrivatsan May 07 '14 at 08:37
  • If you want to access a function you must use WEBMETHOD technique..Refer http://forums.asp.net/t/1895757.aspx?how+to+add+a+webmethod+attribute+into+a+function+in+an+aspx+file+of+vb+net – Jameem May 07 '14 at 08:42
  • I have made changes but i am not getting them right. Could u please help. – Shrivatsan May 07 '14 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52177/discussion-between-jameem-and-shrivatsan) – Jameem May 07 '14 at 09:15
0

try this might help you

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

or

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

Sunny_Sid
  • 401
  • 4
  • 13
0

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();

     }
Community
  • 1
  • 1
Shrivatsan
  • 105
  • 1
  • 18