repeater中radiobuttonlist如何绑定数据,请求完整代码块
html中:<asp:RepeaterID="Repeater1"runat="server"OnItemCommand="Repeater1_ItemCommand">...
html中:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td>
<%#Eval("M_Addate", "{0:yyyy-MM-dd}")%>
</td>
<td>
<p style="width: 180px;">
<%#Eval("M_Title")%></p>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
AutoPostBack="true">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
CS中:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//RadioButtonList zj1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1 ");
//HiddenField Mbit = (HiddenField)e.Item.FindControl("M_Bit");
//if (Mbit.Value.ToString() == "True")
//{
// zj1.Items[0].Selected = true;
//}
if (e.Item.ItemType == ListItemType.Item)
{
RadioButtonList rbl1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1 ");
rbl1.DataSource = this.Repeater1.DataSource;
rbl1.DataTextField = "M_Bit";
rbl1.DataValueField = "M_ID";
rbl1.DataBind();
//if (Repeater1.Items[e.Item.ItemIndex].DataItem == "True ")
// rbl1.Items[0].Selected = true;
}
问题是在html中根本没有绑定出来。 展开
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<td>
<%#Eval("M_Addate", "{0:yyyy-MM-dd}")%>
</td>
<td>
<p style="width: 180px;">
<%#Eval("M_Title")%></p>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
AutoPostBack="true">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
CS中:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//RadioButtonList zj1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1 ");
//HiddenField Mbit = (HiddenField)e.Item.FindControl("M_Bit");
//if (Mbit.Value.ToString() == "True")
//{
// zj1.Items[0].Selected = true;
//}
if (e.Item.ItemType == ListItemType.Item)
{
RadioButtonList rbl1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1 ");
rbl1.DataSource = this.Repeater1.DataSource;
rbl1.DataTextField = "M_Bit";
rbl1.DataValueField = "M_ID";
rbl1.DataBind();
//if (Repeater1.Items[e.Item.ItemIndex].DataItem == "True ")
// rbl1.Items[0].Selected = true;
}
问题是在html中根本没有绑定出来。 展开
1个回答
展开全部
事件错误,应该在ItemDataBound事件中处理,类似如下
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<%#Eval("M_Addate", "{0:yyyy-MM-dd}")%>
</td>
<td>
<p style="width: 180px;">
<%#Eval("M_Title")%></p>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
AutoPostBack="true">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rbl1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1 ");
rbl1.DataSource = this.Repeater1.DataSource;
rbl1.DataTextField = "M_Bit";
rbl1.DataValueField = "M_ID";
rbl1.DataBind();
}
}
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<%#Eval("M_Addate", "{0:yyyy-MM-dd}")%>
</td>
<td>
<p style="width: 180px;">
<%#Eval("M_Title")%></p>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
AutoPostBack="true">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rbl1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1 ");
rbl1.DataSource = this.Repeater1.DataSource;
rbl1.DataTextField = "M_Bit";
rbl1.DataValueField = "M_ID";
rbl1.DataBind();
}
}
追问
rbl1.DataSource = this.Repeater1.DataSource;
在这一行是提示“未将对象引用设置到对象的实例。 ”;
追答
rbl1.DataSource = this.Repeater1.DataSource;
==
不能这样赋值,这里应该是
rbl1.DataSource = 你实际的数据源
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询