Show Button when user is Logged In using C# and VB.Net in ASP.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

Sir i have a application where users should be able to log in and view/download files(.PDF,.JPG). I want to make it so the logged in user can view/download/whatever, but not be able to be accessed by non-users who have the path to the URL.

If you are logged in, you should be able to see and download the file. If you're not logged in, no one can accessed.  

Thanks & Regards,

Shashaank Sharma

You are viewing reply posted by: pandeyism 5 months ago.
Posted 5 months ago

Hi sha10oct,

Refer below sample.

HTML

Login.aspx

Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
<asp:Button Text="Login" runat="server" OnClick="LoginData" />

CS.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="File Name" />
        <asp:TemplateField ItemStyle-HorizontalAlign="Center" Visible="false">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center" Visible="false">
            <ItemTemplate>
                <asp:LinkButton ID="lnkView" runat="server" Text="View" CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

LoginCS.aspx.cs

protected void LoginData(object sender, EventArgs e)
{
    // Code for Login.
    Session["IsLogin"] = true;
    Response.Redirect("CS.aspx");
}

CS.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();

        bool IsLoggedIn = Session["IsLogin"] != null ? Convert.ToBoolean(Session["IsLogin"]) : false;
        if (IsLoggedIn)
        {
            GridView1.Columns[1].Visible = true;
            GridView1.Columns[2].Visible = true;
        }
        else
        {
            GridView1.Columns[1].Visible = false;
            GridView1.Columns[2].Visible = false;
        }
    }
}

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select Id, Name from tblFiles";
            cmd.Connection = con;
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            con.Close();
        }
    }
}

LoginVB.cs

Protected Sub LoginData(ByVal sender As Object, ByVal e As EventArgs)
    Session("IsLogin") = True
    Response.Redirect("CS.aspx")
End Sub

VB.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindGrid()
        Dim IsLoggedIn As Boolean = If(Session("IsLogin") IsNot Nothing, Convert.ToBoolean(Session("IsLogin")), False)
        If IsLoggedIn Then
            GridView1.Columns(1).Visible = True
            GridView1.Columns(2).Visible = True
        Else
            GridView1.Columns(1).Visible = False
            GridView1.Columns(2).Visible = False
        End If
    End If
End Sub

Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "select Id, Name from tblFiles"
            cmd.Connection = con
            con.Open()
            GridView1.DataSource = cmd.ExecuteReader()
            GridView1.DataBind()
            con.Close()
        End Using
    End Using
End Sub

Screenshot