C# 中继器控制中的RadioButton列表
我一直在做一个项目,因为我遇到了一个问题,因为,我有一个中继器和radiobuttonlist在里面,我想从我的数据库填充radiobuttonlist,但我得到了一个错误,因为对象引用没有设置为对象的实例C# 中继器控制中的RadioButton列表,c#,mysql,asp.net,repeater,radiobuttonlist,C#,Mysql,Asp.net,Repeater,Radiobuttonlist,我一直在做一个项目,因为我遇到了一个问题,因为,我有一个中继器和radiobuttonlist在里面,我想从我的数据库填充radiobuttonlist,但我得到了一个错误,因为对象引用没有设置为对象的实例 aspx code <asp:Repeater ID="Repeater1" runat="server"OnItemDataBound="fillRepeater_onitembound"> <HeaderTemplate> &
aspx code
<asp:Repeater ID="Repeater1" runat="server"OnItemDataBound="fillRepeater_onitembound">
<HeaderTemplate>
</HeaderTemplate>
<AlternatingItemTemplate>
</AlternatingItemTemplate>
<ItemTemplate>
<table style="width:1100px">
<tr style="width:1100px">
<asp:Label ID="lbl_teachername" runat="server" Text='<%#Eval("teachername") %>' ></asp:Label>
<asp:Label ID="lbl_teachercode" runat="server" Text='<%#Eval("teachercode") %>' style="display:none;" ></asp:Label>
</tr>
<br />
<tr>
<td style="width:150px">
<asp:Image ID="img_teacher" runat="server" ImageUrl="~/Images/staff.png" Height="100px" Width="100px"/>
</td>
<td >
<asp:RadioButtonList ID="radioatt" runat="server" OnSelectedIndexChanged="radioatt_OnSelectedIndexChanged" AutoPostBack="true" >
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
c# code
protected void fillRepeater_onitembound(object sender,RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
sql = "select Description,AttendanceCode from tblattendancecodes";
ds = obj.openDataset(sql);
ListItem li;
RadioButtonList rbtl = (RadioButtonList)e.Item.FindControl("radioatt");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
li = new ListItem();
li.Text = ds.Tables[0].Rows[i]["Description"].ToString();
li.Value = ds.Tables[0].Rows[i]["AttendanceCode"].ToString();
rbtl.Items.Add(li);
}
}
}
please help me out
调试您的代码,找出哪一行给出的是null或没有正确实例化,您将得到罪魁祸首代码。添加单选按钮列表的null检查,您将不会得到对象引用错误。请参阅下面的代码 c代码
protected void fillRepeater_onitembound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
sql = "select Description,AttendanceCode from tblattendancecodes";
ds = obj.openDataset(sql);
ListItem li;
RadioButtonList rbtl = (RadioButtonList)e.Item.FindControl("radioatt");
if (rbtl != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
li = new ListItem();
li.Text = dt.Rows[i]["ApplicationName"].ToString();
li.Value = dt.Rows[i]["BuildNumber"].ToString();
rbtl.Items.Add(li);
}
}
}
}
我尝试了这段代码,它对我起了作用你在哪里得到了异常?你正确初始化了sql和obj对象了吗?还有一件事。在执行此行RadioButtonList rbtl=RadioButtonListe.Item.FindControlradioatt后,尝试调试解决方案并检查rbtl控件是否具有所有值;