Hi Sofia,
Please refer below sample.
HTML
Default
<table>
<tr>
<td colspan="2">
<asp:FileUpload runat="server" ID="fuUserImage" /></td>
</tr>
<tr>
<td>User Name:</td>
<td><asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td>Designation:</td>
<td><asp:TextBox runat="server" ID="txtDesignation" /></td>
</tr>
<tr>
<td><asp:Button Text="Save" ID="btnSave" runat="server" OnClick="Save" /></td>
</tr>
</table>
<hr />
<asp:GridView runat="server" ID="gvUsers" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img src="<%# Eval("Image") %>" height="100" width="100" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="Designation" HeaderText="Designation" />
</Columns>
</asp:GridView>
<hr />
<asp:Button Text="Send" ID="btnSend" runat="server" OnClick="Send" />
Home
<asp:Repeater runat="server" ID="rptUsers">
<ItemTemplate>
<table>
<tr>
<td><img src="<%# Eval("Image") %>" height="100" width="100" /></td>
<td><asp:Label runat="server" Text='<%# Eval("UserName") %>'></asp:Label></td>
</tr>
<tr>
<td><asp:Label runat="server" Text='<%# Eval("Designation") %>'></asp:Label></td>
</tr>
</table>
<hr />
</ItemTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("UserName"),
new DataColumn("Designation"),
new DataColumn("Image")
});
ViewState["Users"] = dt;
this.BindGrid();
}
}
protected void Save(object sender, EventArgs e)
{
if (!Directory.Exists(Server.MapPath("~/Images")))
{
Directory.CreateDirectory(Server.MapPath("~/Images"));
}
string filePath = "Images/" + Path.GetFileName(fuUserImage.PostedFile.FileName);
fuUserImage.SaveAs(Server.MapPath("~/" + filePath));
DataTable dt = (DataTable)ViewState["Users"];
dt.Rows.Add(txtName.Text.Trim(), txtDesignation.Text.Trim(), filePath);
ViewState["Users"] = dt;
BindGrid();
txtName.Text = string.Empty;
txtDesignation.Text = string.Empty;
}
protected void Send(object sender, EventArgs e)
{
Session["Users"] = ViewState["Users"];
Response.Redirect("~/Home.aspx");
}
protected void BindGrid()
{
gvUsers.DataSource = (DataTable)ViewState["Users"];
gvUsers.DataBind();
}
Home
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = (DataTable)Session["Users"];
rptUsers.DataSource = dt;
rptUsers.DataBind();
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("UserName"), New DataColumn("Designation"), New DataColumn("Image")})
ViewState("Users") = dt
Me.BindGrid()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
If Not Directory.Exists(Server.MapPath("~/Images")) Then
Directory.CreateDirectory(Server.MapPath("~/Images"))
End If
Dim filePath As String = "Images/" & Path.GetFileName(fuUserImage.PostedFile.FileName)
fuUserImage.SaveAs(Server.MapPath("~/" & filePath))
Dim dt As DataTable = CType(ViewState("Users"), DataTable)
dt.Rows.Add(txtName.Text.Trim(), txtDesignation.Text.Trim(), filePath)
ViewState("Users") = dt
BindGrid()
txtName.Text = String.Empty
txtDesignation.Text = String.Empty
End Sub
Protected Sub Send(ByVal sender As Object, ByVal e As EventArgs)
Session("Users") = ViewState("Users")
Response.Redirect("~/Home.aspx")
End Sub
Protected Sub BindGrid()
gvUsers.DataSource = CType(ViewState("Users"), DataTable)
gvUsers.DataBind()
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = CType(Session("Users"), DataTable)
rptUsers.DataSource = dt
rptUsers.DataBind()
End Sub
Screenshot