[Solved] ASP.Net DataList Error: does not contain a definition for ItemDataBound and no extension method ItemDataBound accepting a first argument of type

RichardSa
 
on Aug 23, 2021 01:10 AM
432 Views

I have record in my database with three Columns as shown below:

SchoolName

ExamName   

ExamQuestion 

Options

Top School

1st Terminal Exam

How many continents are there in the world?       

    7

Top School

1st Terminal Exam

How many continents are there in the world?       

    5

Top School

1st Terminal Exam

How many continents are there in the world?       

    6

Top School

1st Terminal Exam

Which part of the body is used for talking?

Nose

Top School

1st Terminal Exam

Which part of the body is used for talking?

Mouth

Top School

1st Terminal Exam

Which part of the body is used for talking?

Eyes

I tried to create a nested Repetear control in another repeater control to display data (with images for each option) in this manner using DataList control inside Repeater control

*The Option button inside is will be used to add more options (if required) to the question. When the Admin clicks on the “Add Option” button, it display a pop-up where admin will type option into a textbox and the option will be inserted into the database to be displayed inside the control.

I added ItemDataBound event to the repeater and I got this error:

CS1061:'_1' does not contain a definition for 'ItemDataBound' and no extension method 'ItemDataBound' accepting a first argument of type '_1' could be found

Please what should I do in this code below?

HTML

    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <table>
                <tr>
                    <asp:Label ID="Lblone" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "BallotQuestion") %>'></asp:Label>
                    <br />
                    <asp:DataList ID="DataList1" runat="server">
                        <ItemTemplate>
                            <asp:Label ID="lbl" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "candidateName") %>'></asp:Label>
                            <asp:Label ID="Labl1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "shortBIO") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:DataList>
                     <asp:Button ID="Button1" runat="server" Text="Add Option" />
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           CreateDataTable();
        }
    }
    private void CreateDataTable()
    {
        using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;"))
        {
            using (SqlCommand _cmd = new SqlCommand("SELECT * FROM [Table]", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
                {

                    con.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    Repeater1.DataSource = ds;
                    Repeater1.DataBind();
                    con.Close();
                }
            }
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataList DataList1 = (DataList)e.Item.FindControl("DataList1");
            DataList1.ItemDataBound += new DataListItemEventHandler(this.ItemDataBound);
            DataList.DataSource = CreateDataList();
            DataList1.DataBind();
        }
    }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 23, 2021 01:21 AM

Use below code.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataList DataList1 = (DataList)e.Item.FindControl("DataList1");        
        DataList.DataSource = CreateDataList();
        DataList1.DataBind();
    }
}

 

RichardSa says:
<asp:DataList ID="DataList1" runat="server">

Define the OnItemDataBound event in the html.