根据列值生成 TextBox 或 DropDownList
Generate TextBox or DropDownList based on column value
我的 table 中有一个名为 ColumnType 的列。 ColumnType 应具有基于列 TypeID 的值的 TextBox 或 DropDownlist。
如果 TypeID 列中的值为 0、2、3,则应显示空文本框,如果值为 1,则应在页面上显示空 DDL。
这是 table 从代码生成时的样子:
https://jsfiddle.net/769825dz/7/
这是 C# 代码,我只是从过程中获取值并将其发送到 aspx 以逐列显示:
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();
这是从程序生成 gridview 的 aspx 部分:
<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>
我想我需要一些 C# 部分的代码。
有人可以帮我解决这个问题吗?
您可以根据Column值设置TextBox或DropDownList的Visible
属性。
<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>
而且你不需要创建一个新的DataTable,你可以这样做
gvObjCharacteristic.DataSource = getObChar.getCharacteristicTableAdapter(Convert.ToInt32("1"));;
我的 table 中有一个名为 ColumnType 的列。 ColumnType 应具有基于列 TypeID 的值的 TextBox 或 DropDownlist。 如果 TypeID 列中的值为 0、2、3,则应显示空文本框,如果值为 1,则应在页面上显示空 DDL。
这是 table 从代码生成时的样子: https://jsfiddle.net/769825dz/7/
这是 C# 代码,我只是从过程中获取值并将其发送到 aspx 以逐列显示:
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();
这是从程序生成 gridview 的 aspx 部分:
<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>
我想我需要一些 C# 部分的代码。
有人可以帮我解决这个问题吗?
您可以根据Column值设置TextBox或DropDownList的Visible
属性。
<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>
而且你不需要创建一个新的DataTable,你可以这样做
gvObjCharacteristic.DataSource = getObChar.getCharacteristicTableAdapter(Convert.ToInt32("1"));;