freej17
freej17

Reputation: 371

Generate TextBox or DropDownList based on column value

I have column in my table called ColumnType. ColumnType should have TextBox or DropDownlist based on the values from the column TypeID. If the value in column TypeID is 0,2,3 then empty text box should appear, and if the value is 1 the empty DDL should be shown on the page.

This is how the table looks when it generated from the code: https://jsfiddle.net/769825dz/7/

This is C# code where I just take the values from procedure and send it to be displayed to aspx column by column:

LogicTableAdapters.getCharacteristicTableAdapter getObChar = new LogicTableAdapters.getCharacteristicTableAdapter();

DataTable dtObChar = getObChar.getCharacteristicTableAdapter(Convert.ToInt32("1"));

DataTable dtCh = new DataTable();
dtCh.Columns.AddRange(new DataColumn[4]{ new DataColumn("CharacteristicID", typeof(string)), new DataColumn("CharacteristicName", typeof(string)), new DataColumn("ColumnType", typeof(string)), new DataColumn("TypeID", typeof(int)),});

foreach (DataRow dr in dtObChar.Rows)
{
    dtCh.Rows.Add(dr["CharacteristicID"].ToString(), dr["CharacteristicName"].ToString(), dr["ColumnType"] == DBNull.Value ? null : dr["TypeID"].ToString());
}

gvObjCharacteristic.DataSource = dtCh;
gvObjCharacteristic.DataBind();

This is the aspx part where the gridview is generated from the procedure:

 <asp:GridView ID="gvObjCharacteristic" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
    <AlternatingRowStyle BackColor="White" />
    <Columns>

        <asp:TemplateField HeaderText="CharacteristicID">
            <ItemTemplate>
                <asp:Label ID="CharacteristicID" runat="server" class="ObjekatID" Width="118px" Height="26px" Style="text-align: center" Font-Names="Georgia" margin-Left="100px" Text='<%# Bind("CharacteristicID") %>'></asp:Label>


            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CharacteristicName">
            <ItemTemplate>

                <asp:Label ID="CharacteristicName" runat="server" Width="118px" Height="26px" Style="text-align: center" Font-Names="Georgia" margin-Left="100px" Text='<%# Bind("CharacteristicName") %>'></asp:Label>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ColumnType">
            <ItemTemplate>

                <asp:Label ID="ColumnType" runat="server" Width="118px" Height="26px" Style="text-align: center" Font-Names="Georgia" margin-Left="100px" Text='<%# Bind("ColumnType") %>'></asp:Label>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="TypeID">
            <ItemTemplate>

                <asp:Label ID="TypeID" runat="server" Width="118px" Height="26px" Font-Names="Georgia" margin-Left="100px" Text='<%# Bind("TypeID") %>'></asp:Label>

            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

I think I need some code in the c# part.

Can someone please help me with this ?

Upvotes: 0

Views: 62

Answers (1)

VDWWD
VDWWD

Reputation: 35554

You can set the Visible property of the TextBox or DropDownList based on the Column value.

<asp:TemplateField>
    <ItemTemplate>

        <asp:TextBox ID="TextBox1" runat="server" Visible='<%# Convert.ToInt32(Eval("TypeID")) != 1 %>'></asp:TextBox>

        <asp:DropDownList ID="DropDownList1" runat="server" Visible='<%# Convert.ToInt32(Eval("TypeID")) == 1 %>'>
            <asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
            <asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
            <asp:ListItem Text="Value 3" Value="3"></asp:ListItem>
        </asp:DropDownList>

    </ItemTemplate>
</asp:TemplateField>

And you don't need to create a new DataTable, you can do this

gvObjCharacteristic.DataSource = getObChar.getCharacteristicTableAdapter(Convert.ToInt32("1"));;

Upvotes: 1

Related Questions