“数据绑定”下拉列表和数据库中转发器内的文本框
本文关键字:数据绑定 转发器 文本 数据库 下拉列表 | 更新日期: 2023-09-27 18:36:07
我在中继器中有一个文本框和一个DropDownList。我想在文本框和下拉列表列表中填充数据库中的值。在PageLoad中,我有CategoryList.GetList
是:
SELECT ID, Name, ActiveName, ActiveID
FROM category
我在该表中有 10 个值,CategoryList.GetList
返回一个数据表,其值类似于以下内容:
1, 'A', 'Active', 1
2, 'B', 'Active', 1
3, 'C', 'Inactive', 0
4, 'D', 'Active', 1
现在我需要将值绑定到文本框和下拉列表。这意味着"A"需要在文本框中,"活动"需要在下拉列表中。我希望下拉列表的值仅为"活动"或"非活动"。当我运行下面的代码时,我看到 DropDownList 多次包含活动和非活动。你能帮我避免这种情况,并且在下拉列表中只有唯一值吗?谢谢
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="ca_temDataBound">
<ItemTemplate>
<tr>
<td style="width: 35%; text-align: left">
<asp:TextBox ID="NameTextBox" runat="server" Width="230px" Text='<%#Eval("CategoryName")%>' />
</td>
<td style="width: 35%; text-align: left">
<asp:DropDownList ID="Active" runat="server" Width="80px" />
</td>
<asp:HiddenField runat="server" Value='<%# Eval("ID") %>' ID="IDHiddenField" />
</tr>
</ItemTemplate>
</asp:Repeater>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rpt.ItemDataBound += new RepeaterItemEventHandler(ca_ItemDataBound);
rpt.DataSource = CategoryList.GetList(1, 100); // Gets id, name, activeid and activename (activeid and active name are the text and value fields for dropdownlist )
rpt.DataBind();
}
}
protected void Category_ItemDataBound(object source, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ActiveDropDown");
ddl.DataSource = CategoryList.GetList(1, 100); // Gets id, name, activeid and activename for the dropdownlist
ddl.DataBind();
}
}
public static ReadOnlyCollection<Category> GetList(int pageIndex, int pageSize)
{
DataTable dataTable = CategoryDB.GetData(pageIndex, pageSize);
List<Category> list = new List<Category>();
foreach (DataRow dataRow in dataTable.Rows)
{
list.Add(new Category(
dataRow["categoryID"].ToString(),
dataRow["categoryName"].ToString(),
dataRow["activeID"].ToString(),
dataRow["activeName"].ToString()));
}
return new ReadOnlyCollection<Category>(list);
}
如果你想在DropDownList
内静态项目,你不必绑定它。所以把它改成这样:
<asp:DropDownList ID="Active" runat="server" Width="80px">
<asp:ListItem Value="1" Text="Active" />
<asp:ListItem Value="0" Text="Inactive" />
</asp:DropDownList>
在代码隐藏中,像这样更改Category_ItemDataBound
方法:
protected void Category_ItemDataBound(object source, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
Category ct = (Category)e.Item.DataItem;
DropDownList ddl = (DropDownList)e.Item.FindControl("Active");
ddl.SelectedValue = ct.ActiveID.ToString(); // I don't know if this is the right property.
}
}
我想因为我得到了你的问题:
好的,我将使用 SQL 查询进行解释:
"SELECT id, name, activeid,activename FROM categorylist"
因此,与其用 Databind 填充下拉列表,只需添加:
foreach(var item in CategoryList.GetList(1, 100))
{
dd1.Items.Add(new ListItem("text","Value");
}
从项目中获取文本和值。
或者,如果您愿意,可以使用数据集。
如果您需要有关数据集的任何帮助,只需将数据库类型发送给我即可。
希望它有帮助