Populate DataList control inside Repeater using C# in ASP.Net

RichardSa
 
on Aug 24, 2021 02:10 AM
471 Views

Id

AdminName

ElectionName

BallotQuestion

CandidateName

Shortbio

1

Richard

2021 Youth Elections

President

Peter Num

He has a vision

2

Richard

2021 Youth Elections

President

Stanton Victor

He is cool

3

Richard

2021 Youth Elections

President

Francis Duru

Very brilliant

4

Richard

2021 Youth Elections

Secretary

Linda Mike

She will be a leader

5

Richard

2021 Youth Elections

Secretary

Queen Donald

Awesome person

6

Richard

2021 Youth Elections

Secretary

Vivian Charles

Unique choice

7

Richard

2021 Youth Elections

Secretary

Mariam Peace

Good persona

8

Richard

P.R.O Elections

President

Dorathy Etim

Visionary

9

Richard

P.R.O Elections

President

Helen William

Excellent candidate

10

Richard

P.R.O Elections

President

Godwin Freeman

Perfect choice

11

Richard

P.R.O Elections

Vice President

King Anthony

Very intelligent

12

Richard

P.R.O Elections

Vice President

Xavier Chuck

Vote him

13

Richard

P.R.O Elections

Vice President

Jordan Frank

Good Listener

 

I am trying to create election software. I have table above which has Ballot records of Candidates’ names contesting for different elections, Election Name, their short bio, the positions contesting for (Ballot Question), as well as the Admin Names who created the Ballot.

I have searched all over for solutions to this but seem not to find any solution. IT IS VERY DIFFICULT FOR ME as I don’t know much about coding (I am still learning).

Please I’m asking for help. How can I create such that when an Admin adds new Ballot Question, it will display on web page; and when a candidate is added to that Ballot question, it will also display accordingly and under that Ballot Question.

For example: If Admin “Richard” creates an election and adds a Ballot Question (“President”); Richard will then go on to add candidate, with the candidate’s Name, short bio and maybe image. Then the candidates’ details will fall under that Ballot Question that the candidate is registered for.

Election Name: 2021 Youth Elections

Ballot Question: President

Candidate Name: Francis Duru

Short bio: Very Brilliant

Election Name: 2021 Youth Elections

Ballot Question: Secretary

Candidate Name: Queen Donald

Short bio: Awesome person

This is my HTML and code. 

<div>
    <asp:Label ID="Label1" runat="server" Text="Richard"></asp:Label>
    <asp:Label ID="Label2" runat="server" Text="2021 Youth Elections"></asp:Label>
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <div class="container-fluid shadow p-3 mb5 bg-white rounded">
                <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:RadioButton ID="RadioButton1" runat="server" />
                                    <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>
            </div>
        </ItemTemplate>
    </asp:Repeater>
</div>

 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1.DataSource = GetData("SELECT * FROM [Questions] WHERE AdminName = '" + Label1.Text + "' AND ElectionName = '" + Label2.Text + "'");
            Repeater1.DataBind();
        }
    }
    private static DataTable GetData(string query)
    {
        using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                using (SqlDataAdapter da = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    da.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
    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.DataSource = GetData("SELECT * FROM [Questions] WHERE AdminName = '" + Label1.Text + "' AND ElectionName = '" + Label2.Text + "'");
            DataList1.DataBind();
        }
    }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download