简体   繁体   English

转发器控件中的单选按钮列表

[英]radiobuttonlist in repeater control

i have been working on a project as i got stucked in one of the problem as,i have a repeater and radiobuttonlist inside it, i want to fill radiobuttonlist from my database but i am getting an error as object reference not set to the instance of an object. 我一直在从事一个项目,因为我陷入了一个问题,因为我里面有一个中继器和radiobuttonlist,我想从数据库中填充radiobuttonlist,但是由于对象引用未设置为实例而出现错误一个东西。

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

Add the null check for radio button list and you will not get error of object reference. 为单选按钮列表添加空检查,您将不会得到对象引用错误。 Please see the below code 请看下面的代码

c# code 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);
            }
        }
    }
}

I tried the code and it worked for me 我尝试了代码,它对我有用

调试您的代码,找出哪一行给出空值或未正确实例化,您将得到罪魁祸首代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM