Hi smile,
Please refer the below sample.
In the sample i am making use of dynamic DataTable for the records. You need to fetch the records from Database table.
And in the Label i have set 5 as UserId for verification.
HTML
<asp:Label ID="lblUserId" runat="server" Text="5" />
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="FileName" HeaderText="File Name" />
<asp:BoundField DataField="ContentType" HeaderText="ContentType" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton Text="Download" runat="server" OnClick="OnDownload" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
gvFiles.DataSource = GetFiles();
gvFiles.DataBind();
}
}
protected void OnDownload(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string fileName = row.Cells[1].Text;
string isPaid = (from status in GetPaymentStatus().AsEnumerable()
where status["FileName"].ToString() == fileName && status["FileName"].ToString() == lblUserId.Text
select status["PaymentStatus"].ToString().ToLower()).FirstOrDefault();
if (isPaid == "paid")
{
// Download code goes here.
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please pay before download.');", true);
}
}
private DataTable GetFiles()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id"),
new DataColumn("FileName"),
new DataColumn("ContentType"),
new DataColumn("Price")
});
dt.Rows.Add(1, "ABC", "application/pdf", 500);
dt.Rows.Add(2, "SKY", "application/pdf", 700);
dt.Rows.Add(3, "XYZ", "application/pdf", 800);
return dt;
}
private DataTable GetPaymentStatus()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("UserId"),
new DataColumn("FileName"),
new DataColumn("PaymentStatus")
});
dt.Rows.Add(1, "ABC", "Paid");
dt.Rows.Add(2, "SKY", "Paid");
dt.Rows.Add(3, "XYZ", "Paid");
return dt;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
gvFiles.DataSource = GetFiles()
gvFiles.DataBind()
End If
End Sub
Private Function GetFiles() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("FileName"), New DataColumn("ContentType"), New DataColumn("Price")})
dt.Rows.Add(1, "ABC", "application/pdf", 500)
dt.Rows.Add(2, "SKY", "application/pdf", 700)
dt.Rows.Add(3, "XYZ", "application/pdf", 800)
Return dt
End Function
Private Function GetPaymentStatus() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("UserId"), New DataColumn("FileName"), New DataColumn("PaymentStatus")})
dt.Rows.Add(1, "ABC", "Paid")
dt.Rows.Add(2, "SKY", "Paid")
dt.Rows.Add(3, "XYZ", "Paid")
Return dt
End Function
Protected Sub OnDownload(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
Dim fileName As String = row.Cells(1).Text
Dim isPaid As String = (From status In GetPaymentStatus().AsEnumerable()
Where status("FileName").ToString() = fileName AndAlso status("FileName").ToString() = lblUserId.Text
Select status("PaymentStatus").ToString().ToLower()).FirstOrDefault()
If isPaid = "paid" Then
' Download code goes here.
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Please pay before download.');", True)
End If
End Sub
Screenshot