Hi CodeLava,
You need to save the answered data in session. Then from the session check the index of datapager in prerender event and apply the css.
Refer below sample.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.PagerNormal {
font: 9pt Verdana;
border: solid 1px #F90000;
padding: 1px 4px;
background-color: #F90000;
border-radius: 5px;
}
.PagerAnswered {
font: 9pt Verdana;
border: solid 1px #008000;
padding: 1px 4px;
background-color: #008000;
border-radius: 5px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<tr>
<td colspan="3">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="1" OnPreRender="DataPager1_PreRender">
<Fields>
<asp:NumericPagerField ButtonType="Button"
NextPreviousButtonCssClass="PagerNormal" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:HiddenField ID="hfQuestion" runat="server" Value='<%# Eval("QuestionId") %>' />
<asp:Label ID="lblQuestion" Text='<%# Eval("QuestionText") %>' runat="server" />
<div>
<asp:RadioButtonList runat="server">
<asp:ListItem Text="Option 1" />
<asp:ListItem Text="Option 2" />
<asp:ListItem Text="Option 3" />
<asp:ListItem Text="Option 4" />
</asp:RadioButtonList>
<br />
<asp:Button Text="Previous" runat="server" />
<asp:Button Text="Next" runat="server" />
<br />
</div>
<br />
</td>
</ItemTemplate>
</asp:ListView>
</form>
</body>
</html>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindListView();
}
}
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.BindListView();
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
DataPager dataPager = (DataPager)sender;
if (dataPager != null)
{
DataTable dt = GetAnswered();
for (int i = 0; i < dataPager.Controls[0].Controls.Count / 2; i++)
{
if (!string.IsNullOrEmpty(dt.Rows[i]["Option"].ToString()))
{
Control control = dataPager.Controls[0].Controls[i * 2];
if (control.GetType() == typeof(Button))
{
(control as Button).Attributes.Add("class", "PagerAnswered");
}
else if (control.GetType() == typeof(Label))
{
(control as Label).Attributes.Add("class", "PagerAnswered");
}
}
}
}
}
private void BindListView()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM Questions";
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
lvCustomers.DataSource = dt;
lvCustomers.DataBind();
}
}
}
}
private DataTable GetAnswered()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("QId"),
new DataColumn("Option")
});
dt.Rows.Add(1, "Option 1");
dt.Rows.Add(2, "");
dt.Rows.Add(3, "Option2");
return dt;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindListView()
End If
End Sub
Protected Sub OnPagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
TryCast(lvCustomers.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
Me.BindListView()
End Sub
Protected Sub DataPager1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
Dim dataPager As DataPager = CType(sender, DataPager)
If dataPager IsNot Nothing Then
Dim dt As DataTable = GetAnswered()
For i As Integer = 0 To dataPager.Controls(0).Controls.Count / 2 - 1
If Not String.IsNullOrEmpty(dt.Rows(i)("Option").ToString()) Then
Dim control As Control = dataPager.Controls(0).Controls(i * 2)
If control.GetType() = GetType(Button) Then
TryCast(control, Button).Attributes.Add("class", "PagerCurrent")
ElseIf control.GetType() = GetType(Label) Then
TryCast(control, Label).Attributes.Add("class", "PagerCurrent")
End If
End If
Next
End If
End Sub
Private Sub BindListView()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT * FROM Questions"
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
lvCustomers.DataSource = dt
lvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Private Function GetAnswered() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("QId"),
New DataColumn("Option")})
dt.Rows.Add(1, "option 1")
dt.Rows.Add(2, "")
dt.Rows.Add(3, "Option2")
Return dt
End Function