I have a DropDownList bounded with List of Items from the SqlDatasource. The List of Items in the DropDownList are bounded based on the following query
SELECT COLUMN_NAMES FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT')
My Requirement: How to assign Multiple Datatypes for those DropDownList Items i.e. Few indexes in the List of Items shall be grouped as Decimal Datatype and Few Indexes shall be grouped as Varchar , DateTime Dataypes.
I would like to generate Dynamic Web controls based on the Datatype of DDL input and the correspoinding DropDownList_SelectedIndexChanged(). My SQL query:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='RESULT' group by DATA_TYPE,COLUMN_NAME
Output:
COLUMN_NAME DATA_TYPE
a decimal
b decimal
c decimal
d int
e int
f varchar
g varchar
h varchar
i varchar
j varchar
If a or b or c are selected from DDL i need to generate dynamic web controls uniquely for Decimal datatype SelectedIndexValue.
How to Proceed?
My aspx code:
<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# code:
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
}
}
Equals method will do object level comparison. Which means it will check if both objects are same or not. In your case DropDownList5.SelectedIndex
will give int
object, which you are comparing with typeof
other object, which will always return false, unless the typeof
is int
.
Since you are getting the result with the column names as COLUMN_NAME
and DATA_TYPE
, change your DropDownList
tag to include these column names. As,
<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="COLUMN_NAME" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
Then you will get the DataType
of the column in DropDownList5.SelectedValue
, which is a string
. Then you can add the controls based on the DropDownList
selection by comparing SelectedValue
with the string
name of the DataType
. As,
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);
}
}