Hi @Gani_tpt,
If you insist on using session, you can refer to the following example.
One thing to note is that in order to prevent users from forgetting to click the "Upload" button, I have added code in the example to upload the file using the FileUpload control without clicking the "Upload" button.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
$("[id*=gvCustomers]").find("[id*=Button1]").click();
}
}
</script>
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Label ID="ImageErrorLabel" runat="server" />
<asp:Button ID="Button1" Text="Upload" OnClick="Upload" runat="server" Style="display: none" />
protected void Page_Load(object sender, EventArgs e)
{
Control control = null;
DropDownList DropDownList1 = null;
if (!this.IsPostBack)
{
this.BindGrid();
if (gvCustomers.FooterRow != null)
{
control = gvCustomers.FooterRow;
DropDownList1 = (control.FindControl("ddlCustomers") as DropDownList);
}
else
{
control = gvCustomers.Controls[0].Controls[0];
DropDownList1 = (control.FindControl("ddlCustomer") as DropDownList);
}
FileUpload fuUpload = (FileUpload)control.FindControl("fuUpload");
fuUpload.Attributes["onchange"] = "UploadFile(this)";
List<Item> items = new List<Item>();
items.Add(new Item() { Value = "1", Text = "USA" });
items.Add(new Item() { Value = "2", Text = "UK" });
items.Add(new Item() { Value = "3", Text = "AUS" });
items.Add(new Item() { Value = "4", Text = "OTHERS" });
DropDownList1.DataSource = items;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--Select --", "0"));
}
if (this.IsPostBack)
{
if (gvCustomers.FooterRow != null)
{
control = gvCustomers.FooterRow;
}
else
{
control = gvCustomers.Controls[0].Controls[0];
}
Label label = (control.FindControl("ImageErrorLabel") as Label);
FileUpload fuUpload = (FileUpload)control.FindControl("fuUpload");
if (Session["FileUpload1"] == null && fuUpload.HasFile)
{
Session["FileUpload1"] = fuUpload.PostedFile;
label.Text = Path.GetFileName(fuUpload.PostedFile.FileName);
}
else if (Session["FileUpload1"] != null && (!fuUpload.HasFile))
{
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
label.Text = Path.GetFileName(file.FileName);
}
else if (fuUpload.HasFile)
{
Session["FileUpload1"] = fuUpload.PostedFile;
label.Text = Path.GetFileName(fuUpload.PostedFile.FileName);
}
}
}
protected void Upload(object sender, EventArgs e)
{
Control control = null;
if (gvCustomers.FooterRow != null)
{
control = gvCustomers.FooterRow;
}
else
{
control = gvCustomers.Controls[0].Controls[0];
}
Label label = (control.FindControl("ImageErrorLabel") as Label);
FileUpload fuUpload = (FileUpload)control.FindControl("fuUpload");
if (Session["FileUpload1"] == null && fuUpload.HasFile)
{
Session["FileUpload1"] = fuUpload.PostedFile;
label.Text = Path.GetFileName(fuUpload.PostedFile.FileName);
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
SaveFile(file);
}
else if (Session["FileUpload1"] != null && (!fuUpload.HasFile))
{
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
label.Text = Path.GetFileName(file.FileName);
SaveFile(file);
}
else if (fuUpload.HasFile)
{
Session["FileUpload1"] = fuUpload.PostedFile;
label.Text = Path.GetFileName(fuUpload.PostedFile.FileName);
SaveFile(fuUpload.PostedFile);
}
}
private void SaveFile(HttpPostedFile file)
{
string fname = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("~/images/" + fname));
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.