Hi RichardSa,
Refer below sample.
HTML
<asp:Label ID="Adminlbl" runat="server" Text="Richard" />
<asp:Label ID="electlbl" runat="server" Text="2021 Youth Elections" />
<asp:DataList runat="server" ID="dlquestion" CssClass="row" CellPadding="4" OnItemDataBound="dlquestion_ItemDataBound" Width="100%">
<ItemTemplate>
<div class="card p-3 mb5 bg-white rounded" style="border: 1px solid #d1d5d4; margin: 0 auto; padding: 10px; width: 100%; border-radius: 10px;">
<asp:Label ID="lblQuestion" Text='<%# Eval("BallotQuestion") %>' runat="server" Font-Bold="true" Font-Names="Varela Round" Font-Size="16pt" />
<asp:Repeater ID="rptOptions" runat="server">
<ItemTemplate>
<div runat="server" id="innerTable">
<div class="" id="middleTable">
<img alt="lock" src="images/home/lock.png" height="80" />
<br />
<div>
<asp:HiddenField ID="hfElectionID" runat="server" Value='<%# Eval("Id") %>' />
<asp:Label ID="Label1" runat="server" Text="NAME: " Font-Bold="true" Font-Size="13pt"></asp:Label>
<asp:Label ID="candlbl" runat="server" Text='<%# Eval("CandidateName") %>' />
</div>
<br />
<div>
<asp:Label ID="Label2" runat="server" Text="BIO: " Font-Bold="true" Font-Size="13pt"></asp:Label>
<asp:Label ID="biolabel" Text='<%# Eval("Shortbio") %>' runat="server" />
</div>
<br />
<br />
</div>
<%--<div>
<br />
<asp:Button ID="Button2" runat="server" Text="Edit" CssClass="btn btn-info" Font-Size="10pt" OnClick="OnEdit" />
<asp:Button ID="Button3" runat="server" Text="Delete" CssClass="btn btn-danger" Font-Size="10pt" />
</div>
<hr />--%>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnAddOption" runat="server" Text="Add Option" CssClass="btn btn-primary" OnClick="OnAddOption" />
</div>
</ItemTemplate>
</asp:DataList>
<div id="dialog" style="display: none">
<asp:HiddenField ID="hfBallotQuestion" runat="server" />
Candidate Name:
<asp:TextBox runat="server" ID="txtCandidateName" />
Shortbio:
<asp:TextBox runat="server" ID="txtShortbio" />
<asp:Button Text="Add" runat="server" OnClick="OnAdd" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script type="text/javascript">
function ShowPopup() {
$("#dialog").dialog({
title: "Update record",
buttons: {
Close: function () {
$(this).dialog('close');
}
},
modal: true,
width: "480"
});
$("#dialog").parent().appendTo($("form:first"));
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "SELECT DISTINCT BallotQuestion FROM Election WHERE AdminName = '" + Adminlbl.Text + "' AND ElectionName = '" + electlbl.Text + "'";
DataTable dt = GetData(query);
dlquestion.RepeatColumns = dt.Rows.Count;
dlquestion.DataSource = dt;
dlquestion.DataBind();
dlquestion.RepeatColumns = 1;
dlquestion.RepeatDirection = RepeatDirection.Vertical;
}
}
protected void dlquestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string BallotQuestion = (e.Item.FindControl("lblQuestion") as Label).Text;
Repeater rptOptions = e.Item.FindControl("rptOptions") as Repeater;
string query = "SELECT Id,CandidateName, Shortbio FROM Election WHERE BallotQuestion = '" + BallotQuestion + "' AND AdminName = '" + Adminlbl.Text + "' AND ElectionName = '" + electlbl.Text + "'";
rptOptions.DataSource = GetData(query);
rptOptions.DataBind();
}
}
private DataTable GetData(string query)
{
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection("Server=.;DataBase=Test;UID=sa;PWD=pass@123;"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void OnAddOption(object sender, EventArgs e)
{
DataListItem item = (sender as Button).NamingContainer as DataListItem;
Label ballotQuestion = item.FindControl("lblQuestion") as Label;
hfBallotQuestion.Value = ballotQuestion.Text;
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
}
protected void OnAdd(object sender, EventArgs e)
{
string adminName = Adminlbl.Text;
string electtionName = electlbl.Text;
string ballotQuestion = hfBallotQuestion.Value;
string name = txtCandidateName.Text;
string shortbio = txtCandidateName.Text;
using (SqlConnection con = new SqlConnection("Server=.;DataBase=Test;UID=sa;PWD=pass@123;"))
{
string query = "INSERT INTO Election (AdminName,ElectionName,BallotQuestion,CandidateName,Shortbio) VALUES (@AdminName,@ElectionName,@BallotQuestion,@CandidateName,@Shortbio)";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@AdminName", adminName);
cmd.Parameters.AddWithValue("@ElectionName", electtionName);
cmd.Parameters.AddWithValue("@BallotQuestion", ballotQuestion);
cmd.Parameters.AddWithValue("@CandidateName", name);
cmd.Parameters.AddWithValue("@Shortbio", shortbio);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect(Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim query As String = "SELECT DISTINCT BallotQuestion FROM Election WHERE AdminName = '" &
Adminlbl.Text & "' AND ElectionName = '" & electlbl.Text & "'"
Dim dt As DataTable = GetData(query)
dlquestion.RepeatColumns = dt.Rows.Count
dlquestion.DataSource = dt
dlquestion.DataBind()
dlquestion.RepeatColumns = 1
dlquestion.RepeatDirection = RepeatDirection.Vertical
End If
End Sub
Protected Sub dlquestion_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim BallotQuestion As String = TryCast(e.Item.FindControl("lblQuestion"), Label).Text
Dim rptOptions As Repeater = TryCast(e.Item.FindControl("rptOptions"), Repeater)
Dim query As String = "SELECT Id,CandidateName, Shortbio FROM Election WHERE BallotQuestion = '" &
BallotQuestion & "' AND AdminName = '" & Adminlbl.Text & "' AND ElectionName = '" & electlbl.Text & "'"
rptOptions.DataSource = GetData(query)
rptOptions.DataBind()
End If
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection("Server=.;DataBase=Test;UID=sa;PWD=pass@123;")
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub OnAddOption(ByVal sender As Object, ByVal e As EventArgs)
Dim item As DataListItem = TryCast(TryCast(sender, Button).NamingContainer, DataListItem)
Dim ballotQuestion As Label = TryCast(item.FindControl("lblQuestion"), Label)
hfBallotQuestion.Value = ballotQuestion.Text
ClientScript.RegisterStartupScript(Me.GetType(), "Popup", "ShowPopup();", True)
End Sub
Protected Sub OnAdd(ByVal sender As Object, ByVal e As EventArgs)
Dim adminName As String = Adminlbl.Text
Dim electtionName As String = electlbl.Text
Dim ballotQuestion As String = hfBallotQuestion.Value
Dim name As String = txtCandidateName.Text
Dim shortbio As String = txtCandidateName.Text
Using con As SqlConnection = New SqlConnection("Server=.;DataBase=Test;UID=sa;PWD=pass@123;")
Dim query As String = "INSERT INTO Election (AdminName,ElectionName,BallotQuestion,CandidateName,Shortbio) VALUES (@AdminName,@ElectionName,@BallotQuestion,@CandidateName,@Shortbio)"
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@AdminName", adminName)
cmd.Parameters.AddWithValue("@ElectionName", electtionName)
cmd.Parameters.AddWithValue("@BallotQuestion", ballotQuestion)
cmd.Parameters.AddWithValue("@CandidateName", name)
cmd.Parameters.AddWithValue("@Shortbio", shortbio)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub