DropDownList保留错误的输入文本,而不是其List of Items中的选定输入

时间:2014-05-07 06:03:16

标签: c# jquery asp.net drop-down-menu sqldatasource

我有一个DropDownList控件,其中填充了SqlDataSource中的List列表。 SqlDataSource QueryBuilder从我的数据库表中选择Column_Names。

如果DropDownList提供了DataTextField =" All_Columns" DataValueField =" ALL_COLUMNS"属性,DropDownList_SelectedIndexChanged()保留选定的输入文本。

当前问题: 如果DropDownList提供了DataTextField =" All_Columns" DataValueField =" DATA_TYPE"属性,DropDownList_SelectedIndexChanged()不保留基于所选输入的文本。但它保留了满足索引中相应DATA_TYPE的项列表中的第一个值。

需要解决方案: 如何根据DATA_TYPE属性保留选定的输入文本?我尝试存储Session [" DDLValue"] = DropDownList.SelectedItem.Text,但它始终保留满足索引中相应DATA_TYPE的项目列表中的第一个值。

即。如果我选择" e"从以下DropDownList输入中,DropDownList中保留的值为**" d"

如何保留" e"即选择带有DATA_TYPE属性的文本。**

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

我的aspx代码:

<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#代码:

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

3 个答案:

答案 0 :(得分:1)

试试这个..

如果您在pageload中绑定下拉列表,那么..

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

<强>标记:

<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#代码:

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

     }

答案 1 :(得分:0)

试试这可能会对你有所帮助

Session [“DDLValue”] = DropDownList5.SelectedItem.Value;

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

答案 2 :(得分:0)

正确解决方案:

如果从以下DropDownList输入中选择&#34; e&#34; ,DropDownList中保留的值为&#34; e&#34;

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

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

Aspx代码:

<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#代码:

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

     }