如何为与项列表绑定的DropDownList分配数据类型

时间:2014-05-02 07:01:51

标签: c# asp.net sql-server dynamic-web-controls

我有一个DropDownList与SqlDatasource中的List列表绑定。 DropDownList中的项目列表基于以下查询

  

从INFORMATION_SCHEMA.COLUMNS WHERE中选择COLUMN_NAMES(TABLE_NAME   ='结果')

我的要求:如何为那些DropDownList项分配多个数据类型,即项目列表中的少数索引应分组为十进制数据类型,少数索引应分组为Varchar,DateTime Dataypes。

我想基于DDL输入的数据类型和相应的DropDownList_SelectedIndexChanged()生成动态Web控件。     我的SQL查询:

SELECT COLUMN_NAME, DATA_TYPE 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='RESULT' group by DATA_TYPE,COLUMN_NAME

输出:

COLUMN_NAME  DATA_TYPE  

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

如果从DDL中选择了a或b或c,我需要为Decimal数据类型SelectedIndexValue生成唯一的动态Web控件。

如何继续?

我的aspx代码:

<asp:Panel ID="Panel5" runat="server" Height="221px">
        <span style="font-size: 135%; font-family: Verdana; font-weight: bold"> Search Functionalities </span>
        <asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="All_Columns" DataValueField="All_Columns" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged"  AutoPostBack="true">
        </asp:DropDownList>
        <asp:SqlDataSource ID="column_list_for_filter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME 'All_Columns' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='RESULT'  "></asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Small" OnClick="Button1_Click" Text="Search Flow Periods" Width="144px" />
        <asp:Table ID="dynamic_filter_table" runat="server" ToolTip="Results">
        </asp:Table>
</asp:Panel>

C#代码:

  protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
     {
         if(DropDownList5.SelectedIndex.Equals(typeof(Decimal)))
         {
            createdynamiccontrols();
         }
         else if (DropDownList5.SelectedIndex.Equals(typeof(DateTime)))
         {
             //Create Unique Web Controls
         }

         else if (DropDownList5.SelectedIndex.Equals(typeof(Char)))
         {
             //Create Unique Web Controls
         }
     }

3 个答案:

答案 0 :(得分:0)

我以这种方式假设您的场景,您需要根据数据类型对column_name值进行分类,一旦对所选值进行分类,应该在唯一的Web控件中显示吗?如果是这样,请尝试使用dropdwon selectchange事件中的这行代码。

SqlConnection con=new Sqlconnenction("Your Connection String");
con.Open();
Sqlcommand cmd = new SqlCommand("Select * from your database",con); // Your two columns
SqlDateReader dr=cmd.ExecuteReader();
while(dr.Read())
{
  if(dr.GetString(1) == "decimal") // dr.GetString(1) = 'Your Second Column'
  {
    TextBox1.Text = dr.GetString(0);
  }
  else if(dr.GetString(1)=="int")
  {
    Label1.Text = dr.GetString(0);
  }
  else if(dr.GetString(1)=="varchar")
  {
    Button1.Text = dr.GetString(0);
  }
}
con.Close();

如果我困惑你,请告诉我。

答案 1 :(得分:0)

Equals方法将进行对象级别比较。这意味着它将检查两个对象是否相同。在您的情况下,DropDownList5.SelectedIndex将提供int对象,您将与typeof其他对象进行比较,除非typeofint,否则该对象将始终返回false。

由于您收到的列名称为COLUMN_NAMEDATA_TYPE,因此请更改DropDownList标记以包含这些列名称。如,

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

然后,您将获得DataTypeDropDownList5.SelectedValue列的stringDropDownList。然后,您可以通过将SelectedValuestring的{​​{1}}名称进行比较,根据DataType选项添加控件。如,

protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
    if(DropDownList5.SelectedValue == "decimal")
    {
        TextBox1.Text = dr.GetString(0);
    }
    else if(DropDownList5.SelectedValue == "int")
    { 
        Label1.Text = dr.GetString(0);
    }
    else if(DropDownList5.SelectedValue == "varchar")
    {
        Button1.Text = dr.GetString(0);
    }
}

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

     }