Hi micah,
I have created sample code which full-fill your requirement. So please refer the code carefully and modify as per your requirement.
HTML
Login Page
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
UserName
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" TextMode="Password" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" Text="Login" OnClick="OnLogin" runat="server" />
</td>
</tr>
</table>
</div>
</form>
C#
protected void OnLogin(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE UserName=@UserName AND Password=@Password", con))
{
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
Session["Id"] = idr["Id"].ToString();
Response.Redirect("Default.aspx");
}
con.Close();
}
}
}
ViewPage
HTML
<form id="form1" runat="server">
<div>
<asp:DataList ID="GetMergedAll" runat="server" RepeatColumns="4" OnItemDataBound="GetMergedAll_ItemDataBound">
<HeaderTemplate>
<table class="table" border="1">
<tr>
<td>
<asp:Label ID="Label1" Text="Pics" runat="server" />
</td>
<td>
<asp:Label ID="Label2" Text="Action" runat="server" />
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Id" Text="0" Visible="false" runat="server" />
<asp:Label ID="lblUserId" Text='<%#Eval("UserId") %>' Visible="false" runat="server" />
<asp:ImageButton ID="UserImage" ImageUrl='<%# "Images/" + Eval("Name") %>' ToolTip='<%#Eval("Name")%>'
Width="80px" Height="50px" runat="server" />
</td>
<td>
<asp:LinkButton ID="btnAdd" Visible="false" runat="server">Add</asp:LinkButton>
<asp:LinkButton ID="btnEdit" Visible="false" runat="server">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
<asp:LinkButton ID="linkback" OnClick="OnClick" Text="back" runat="server" />
</form>
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
public string userId
{
get
{
return Session["Id"].ToString() != null ? Convert.ToString(Session["Id"]) : "0";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populatebooks();
}
}
private void Populatebooks()
{
string Populatebooks = "SELECT * FROM [dbo].[Files]";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(Populatebooks, con))
{
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GetMergedAll.DataSource = dt;
GetMergedAll.DataBind();
}
}
}
protected void GetMergedAll_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string dlUserId = (e.Item.FindControl("lblUserId") as Label).Text;
if (dlUserId == userId)
{
(e.Item.FindControl("btnEdit") as LinkButton).Visible = true;
}
else
{
(e.Item.FindControl("btnAdd") as LinkButton).Visible = true;
}
}
}
protected void OnClick(object sender, EventArgs e)
{
Response.Redirect("LoginPage.aspx");
}
Vb.net
Login Page
Protected Sub OnLogin(sender As Object, e As EventArgs)
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM [User] WHERE UserName=@UserName AND Password=@Password", con)
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim())
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim())
con.Open()
Dim idr As SqlDataReader = cmd.ExecuteReader()
If idr.Read() Then
Session("Id") = idr("Id").ToString()
Response.Redirect("Default.aspx")
End If
con.Close()
End Using
End Using
End Sub
ViewPage
Private constring As String = ConfigurationManager.ConnectionStrings("constring").ToString()
Public ReadOnly Property userId() As String
Get
Return If(Session("Id").ToString() IsNot Nothing, Convert.ToString(Session("Id")), "0")
End Get
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Me.Populatebooks()
End If
End Sub
Private Sub Populatebooks()
Dim Populatebooks As String = "SELECT * FROM [dbo].[Files]"
Using con As New SqlConnection(constring)
con.Open()
Using cmd As New SqlCommand(Populatebooks, con)
cmd.CommandType = CommandType.Text
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
GetMergedAll.DataSource = dt
GetMergedAll.DataBind()
End Using
End Using
End Sub
Protected Sub GetMergedAll_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim dlUserId As String = TryCast(e.Item.FindControl("lblUserId"), Label).Text
If dlUserId = userId Then
TryCast(e.Item.FindControl("btnEdit"), LinkButton).Visible = True
Else
TryCast(e.Item.FindControl("btnAdd"), LinkButton).Visible = True
End If
End If
End Sub
Protected Sub OnClick(sender As Object, e As EventArgs)
Response.Redirect("LoginPage.aspx")
End Sub
Screenshot