C# 基于列值生成TextBox或DropDownList
我的表中有一列名为ColumnType。ColumnType应具有基于列TypeID的值的TextBox或DropDownlist。 如果列TypeID中的值为0,2,3,则应显示空文本框,如果值为1,则应在页面上显示空DDL 这是从代码生成表时的外观: 这是C#代码,我只是从过程中获取值,并将其逐列发送到aspx显示:C# 基于列值生成TextBox或DropDownList,c#,asp.net,gridview,C#,Asp.net,Gridview,我的表中有一列名为ColumnType。ColumnType应具有基于列TypeID的值的TextBox或DropDownlist。 如果列TypeID中的值为0,2,3,则应显示空文本框,如果值为1,则应在页面上显示空DDL 这是从代码生成表时的外观: 这是C#代码,我只是从过程中获取值,并将其逐列发送到aspx显示: LogicTableAdapters.getCharacteristicTableAdapter getObChar = new LogicTableAdapters.get
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();
这是aspx部件,其中gridview是通过以下过程生成的:
<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部分的代码
有人能帮我吗?您可以根据列值设置文本框或下拉列表的
可见属性
<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>
谢谢你的回答,我会立即尝试这个解决方案!
gvObjCharacteristic.DataSource = getObChar.getCharacteristicTableAdapter(Convert.ToInt32("1"));;