Sreenish

Sreenish

  • NA
  • 249
  • 33.3k

how to add image to a grid using file upload??

Sep 15 2015 4:48 AM
Hello,
 
 
I want to display three columns in a gridview date name and image. here am not using any database dynamically have to enter.
for date i have used ajax extender in one textbox inside template field.and image field and another textbox for name...
here am posting what i have tried.
 
Source Code::
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Addtxt.aspx.cs" Inherits="GV_with_image.Image_Gv.Addtxt" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

</asp:ToolkitScriptManager>
<div>
<asp:GridView ID="Gridview1" runat="server" ShowFooter="True"
AutoGenerateColumns="False"
>
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" Format="dd/MM/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" HeaderText="Image" />

<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />

</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />

</div>
</form>
</body>
</html>

 Code Behind::
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Specialized;
using System.IO;

namespace GV_with_image.Image_Gv
{
public partial class Addtxt : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetInitialRow();
filup();
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add("Value");
dr = dt.NewRow();
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();

ViewState["CurrentTable"] = dt;

Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex =0;

if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[0].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox2");
Image img = (Image)Gridview1.Rows[rowIndex].Cells[2].FindControl("Value");
drCurrentRow = dtCurrentTable.NewRow();

dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Value"] = ViewState["path"];

rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;

Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}

//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[0].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox2");
Image img = (Image)Gridview1.Rows[rowIndex].Cells[2].FindControl("Value");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();

rowIndex++;
}


}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void filup()
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Img/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
files.Add(new ListItem(fileName, "~/Img/" + fileName));
}
Gridview1.DataSource = files;
Gridview1.DataBind();
}

protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Img/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
ViewState["path"] = fileName;
}
}




}
}
 
 
 
 
 
 
 
 
 
 

Answers (1)