Repeater中的DropDownList绑定默认值
在Repeater绑定数据前,Repeater中的DropDownList已设置了值,在Repeater1.DataSource=ds.Tables[0].Default...
在Repeater绑定数据前,Repeater中的DropDownList已设置了值,
在Repeater1.DataSource=ds.Tables[0].DefaultView;
Repeater1.DataBind();
绑定后,如何DropDownList选中绑定值所对应的项。。。 展开
在Repeater1.DataSource=ds.Tables[0].DefaultView;
Repeater1.DataBind();
绑定后,如何DropDownList选中绑定值所对应的项。。。 展开
展开全部
在Repeater的ItemDataBound事件中处理一下吧,这里同时可以动态为DropDownList绑定下拉项
前台代码:
<asp:Repeater runat="server" ID="repeater1"
onitemdatabound="repeater1_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>
姓名
</td>
<td>
性别
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("XM") %>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="男" ></asp:ListItem>
<asp:ListItem Text="女" ></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("XM");
dt.Columns.Add("XB");
dt.Rows.Add(new object[] { "张三", "男" });
dt.Rows.Add(new object[] { "李四", "女" });
repeater1.DataSource = dt;
repeater1.DataBind();
}
}
protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
(e.Item.FindControl("DropDownList1") as DropDownList ).SelectedValue=((DataRowView)e.Item.DataItem).Row[1].ToString();
}
}
前台代码:
<asp:Repeater runat="server" ID="repeater1"
onitemdatabound="repeater1_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>
姓名
</td>
<td>
性别
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("XM") %>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="男" ></asp:ListItem>
<asp:ListItem Text="女" ></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("XM");
dt.Columns.Add("XB");
dt.Rows.Add(new object[] { "张三", "男" });
dt.Rows.Add(new object[] { "李四", "女" });
repeater1.DataSource = dt;
repeater1.DataBind();
}
}
protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
(e.Item.FindControl("DropDownList1") as DropDownList ).SelectedValue=((DataRowView)e.Item.DataItem).Row[1].ToString();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询