我在Repeater中有一个文本框和一个DropDownList。我想在TextBox和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
现在我需要将值绑定到文本框和DropDownList。含义' A'需要在TextBox和' Active'在DropDownList中。我希望DropDownList的值只有' Active'或者'无效'。当我运行下面的代码时,我看到DropDownList多次包含Active和Inactive。你可以帮我避免这种情况,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);
}
答案 0 :(得分:0)
我想我得到了你的问题:
好的,我将使用SQL查询解释:
"SELECT id, name, activeid,activename FROM categorylist"
所以用数据绑定填充下拉列表只需添加:
foreach(var item in CategoryList.GetList(1, 100))
{
dd1.Items.Add(new ListItem("text","Value");
}
从项目中获取文本和值 或者,如果您愿意,也可以使用数据集 如果您需要有关数据集的任何帮助,请将数据库类型发送给我 希望它有所帮助
答案 1 :(得分:0)
如果您想要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.
}
}