DataBind 下拉列表和来自数据库的转发器内的文本框
DataBind dropdownlist and the textbox inside the repeater from the database
我在 Repeater 中有一个文本框和一个 DropDownList。我想在 TextBox 和 DropDownList 中填充数据库中的值。在 PageLoad 中,我有 CategoryList.GetList
即:
SELECT ID, Name, ActiveName, ActiveID
FROM category
我有 10 个值 table 和 CategoryList.GetList
returns 一个数据table 的值类似于以下内容:
1, 'A', 'Active', 1
2, 'B', 'Active', 1
3, 'C', 'Inactive', 0
4, 'D', 'Active', 1
现在我需要将值绑定到文本框和 DropDownList。意思是 'A' 需要在 TextBox 中,而 'Active' 需要在 DropDownList 中。我希望 DropDownList 的值仅为 'Active' 或 'Inactive'。当我运行下面的代码时,我看到 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);
}
我想当我收到你的问题时:
好的,我将使用 sql 查询进行解释:
"SELECT id, name, activeid,activename FROM categorylist"
所以不用数据绑定填充下拉列表只需添加:
foreach(var item in CategoryList.GetList(1, 100))
{
dd1.Items.Add(new ListItem("text","Value");
}
从项目中获取文本和值。
或者您可以根据需要使用数据集。
如果您需要有关数据集的任何帮助,请将数据库类型发送给我。
希望对你有帮助
如果您想要 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.
}
}
我在 Repeater 中有一个文本框和一个 DropDownList。我想在 TextBox 和 DropDownList 中填充数据库中的值。在 PageLoad 中,我有 CategoryList.GetList
即:
SELECT ID, Name, ActiveName, ActiveID
FROM category
我有 10 个值 table 和 CategoryList.GetList
returns 一个数据table 的值类似于以下内容:
1, 'A', 'Active', 1
2, 'B', 'Active', 1
3, 'C', 'Inactive', 0
4, 'D', 'Active', 1
现在我需要将值绑定到文本框和 DropDownList。意思是 'A' 需要在 TextBox 中,而 'Active' 需要在 DropDownList 中。我希望 DropDownList 的值仅为 'Active' 或 'Inactive'。当我运行下面的代码时,我看到 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);
}
我想当我收到你的问题时:
好的,我将使用 sql 查询进行解释:
"SELECT id, name, activeid,activename FROM categorylist"
所以不用数据绑定填充下拉列表只需添加:
foreach(var item in CategoryList.GetList(1, 100))
{
dd1.Items.Add(new ListItem("text","Value");
}
从项目中获取文本和值。
或者您可以根据需要使用数据集。
如果您需要有关数据集的任何帮助,请将数据库类型发送给我。
希望对你有帮助
如果您想要 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.
}
}