unable to read fileupload1.filename when placed in UpdatePanel - c#

I have a form where users upload their picture to the registration form.. everything worked fine until i added an updatepanel and an Image control.
I placed an update panel, inside it i place a table with Fileupload control and an image control. once the button is clicked the image is uploaded to the images folder and the preview is displayed in image control.
Before making the modification it worked fine.. but now im unable to read the FileUpload1.Filename .. below is the code
Reg.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table> <tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload1" runat="server" onclick="btnUpload1_Click"
Text="Upload" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<Td>
<asp:Image ID="Image1" runat="server" Width="147px" /></td></tr>
</table>
Reg.aspx.cs
protected void btnUpload1_Click(object sender, EventArgs e)
{
Label1.Text = FileUpload1.FileName;
try
{
var folder = Server.MapPath("~/UploadImages/");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
Label1.Text = Label1.Text + " Directory Created <br>";
}
var picUrl = Server.MapPath("~/UploadImages/"+ FileUpload1.FileName.ToString());
if (CheckFileName(picUrl) == true)
{
FileUpload1.SaveAs(picUrl);
Image1.ImageUrl = picUrl.ToString();
Label1.Text = Label1.Text + "Image Uploaded";
}
else
Label1.Text = Label1.Text +"File already exists";
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
private bool CheckFileName(string picUrl) {
if (File.Exists(picUrl))
return false;
return (true);
}
Now im not able to read the filename from fileupload1 control..
Please help

Add Postback Trigger in your update panel. Have a look at below code sample
ASPX:
<asp:UpdatePanel runat="server" ID="updatePanel1">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fileUploader"/>
<asp:Button runat="server" ID="buttonSubmit" OnClick="buttonSubmit_OnClick" Text="Submit"/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="buttonSubmit"/>
</Triggers>
</asp:UpdatePanel>
ASPX.cs
protected void buttonSubmit_OnClick(object sender, EventArgs e)
{
var fileName = this.fileUploader.FileName;
}

Related

ASP.NET FileUpload with precondtions (empty after PostBack)

I want to create a C# WebForm with a ASP.NET FileUpload Control and some Textboxes and a submit button. Idea: user selects a file, enters some data, on submit the form checks the data and if valid, it saves the file on the server otherwise an error message is displayed. There are so many posts about UpdatePanel Triggers etc. but not a working solution.
Here my code behind:
protected void Page_Load(object sender, EventArgs e) {
// for FileUpload-Control outside UpdatePanel
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
protected void Button1_Click(object sender, EventArgs e) {
bool valid = true;
string errorMessage = DateTime.Now.ToLongTimeString() + ": ";
if (this.TextBox1.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox1<br/>";
}
if (this.TextBox2.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox2<br/>";
}
if (this.TextBox3.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox3<br/>";
}
if (!this.FileUpload3.HasFile) {
// is alway false!
errorMessage += "Missing FileUpload3<br/>";
}
if (valid) {
// never fires, because .HasFile is always false
this.Label1.Text = "valid!";
// do upload stuff
this.FileUpload3.SaveAs("foobar");
} else {
this.Label1.Text = errorMessage;
}
}
And here is my sample ASPX:
<form id="Upload" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Does anyone see, why my FileUpload is always empty, although it's outside the UpdatePanel and I do have the required line in the Page_Load() event? If, could you adjust the code?
Thank you
SiS
<form id="Upload" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>

Flieupload is not working on ascx page

I am facing a problem. I place a control ascx at aspx page. At aspx page i am using UpdatePanel. And at ascx page i use a Formview. In Formview <InsertItemTemplate> i use a control asp:FileUpload for File upload. After Selecting file, when i check FileUpload.HasFile it gives me false always. I try to fire <Triggers> but not
success because file upload is on my ascx page. In below example i am showing my problem parts.
code
FileUpload _fileUpload = FormView1.FindControl("FileUpload1") as FileUpload;
if (_fileUpload != null && _fileUpload.HasFile)
{
/// some code i write here
}
ASPX
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
<asp:PostBackTrigger ControlID="imagAddNew" />
<asp:AsyncPostBackTrigger ControlID="EditProduct1" />
<asp:PostBackTrigger ControlID="ImageButton1" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnl_grid" Style="width: 100%; overflow: auto;" runat="server">
<uc1:EditProduct ID="EditProduct1" runat="server" />
</asp:Panel>
ASCX
<asp:FormView ID="FormView1" runat="server" Width="100%" ondatabinding="FormView1_DataBinding">
<InsertItemTemplate>
<table>
<tr>
<td class="label-col">
Image
</td>
<td class="data-col">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True" CommandName="Insert"
ImageUrl="~/images/save.gif" ValidationGroup="Inser" />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FromView>
try this 100% working
Here Your FileUpload Control is Nested Inside the FormView so i find that control ImageButton and then added PostBackTrigger to that control. So you can Apply Same logic to GridView,Repeater,DataList,etc and your can use either ToolScriptManager
or ScriptManager to Register a PostBackControl
use Image Button like this
<asp:ImageButton ID="ImageButton1" runat="server"
OnClick="ImageButton1_OnClick" CausesValidation="True" CommandName="Insert"
ImageUrl="~/images/save.gif" ValidationGroup="Inser" />
in Your .aspx page where your are importing user control on page load use this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FormView ProductsFormView = (FormView)EditProduct1.FindControl("ProductsFormView");
FindAllTextBoxes(ProductsFormView);
}
}
private void FindAllTextBoxes(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType().ToString() == "System.Web.UI.WebControls.ImageButton")
{
ImageButton ImageButton1 = (ImageButton)c.FindControl("ImageButton1");
if (ImageButton1 != null)
{
ToolScriptManager1.RegisterPostBackControl(ImageButton1);
//or ScriptManager.RegisterPostBackControl(ImageButton1);
}
}
if (c.Controls.Count > 0)
{
FindAllTextBoxes(c);
}
}
}
protected void ImageButton1_OnClick(object sender, EventArgs e)
{
ImageButton ImageButton1 = (ImageButton)sender;
FormViewRow row = (FormViewRow)ImageButton1.Parent.Parent;
FileUpload FileUpload1 = (FileUpload)row.FindControl("FileUpload1");
if (FileUpload1.HasFile)
{
}
}
if You are using ContentPlace Holder with Master page then
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
ToolkitScriptManager ToolScriptManager1 = (ToolkitScriptManager)ContentPlaceHolder1.FindControl("ToolScriptManager1");
ToolScriptManager1.RegisterPostBackControl(ImageButton1);

ASP dropdownlist selecedvalue is blank on button press

Hi I'm having a problem with my dropdownlist. I have goolged but nobody seems to have had the same problem. I have an updatepanel with a panel of controls within it. On button click I want to get the values from the controls the problem is on button click the selectedvalue is blank always.
here is the html
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
<ContentTemplate>
<asp:Panel runat="server" ID="pnlLSP" CssClass="textBox" Width="85%" Visible="False">
<div>
<asp:DropDownList ID="ddlLSPHours" runat="server" ValidationGroup="LSP"
EnableViewState="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="ddlLSPHours" ErrorMessage="RequiredFieldValidator"
ValidationGroup="LSP">*</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlLSPMins" runat="server" ValidationGroup="LSP" EnableViewState="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="ddlLSPMins" ErrorMessage="RequiredFieldValidator"
ValidationGroup="LSP">*</asp:RequiredFieldValidator>
</div>
<div style="text-align: center">
<asp:Button ID="btnLSPDone" runat="server" CssClass="button" Text="Done" Style="margin-top: 10px"
Width="100px" ValidationGroup="LSP" OnClick="btnLSPDone_Click" />
</div>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnLSPDone"></asp:PostBackTrigger>
</Triggers>
</asp:UpdatePanel>
c# code for populating dropdownlists
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//populate hours ddl
for (int i = 0; i < 25; i++)
{
string number = i.ToString();
if (i < 10)
{
number = "0" + i.ToString();
}
ddlLSPHours.Items.Add(number)
}
//populate mins ddl
for (int i = 0; i < 61; i++)
{
string number = i.ToString();
if (i < 10)
{
number = "0" + i.ToString();
}
ddlLSPMins.Items.Add(number);
}
}
}
protected void btnLSPDone_Click(object sender, EventArgs e)
{
string timeSelected = ddlLSPHours.SelectedValue + ":" + ddlLSPMins.SelectedValue;
}
The only problem is with your
EnableViewState="False"
in
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
Any reason why you are using it?
If you remove it, will give you the selected values for the dropdowns. Something like this :
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1">
Hope this helps. Happy Coding..!!
just make EnableViewState="False" of your UpdatePanel
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
it will work.

Submit TextBox From Within A Repeater

I want to do something simple. I have a text box in a repeater Item that will allow people to add a note to the item. My code is not working, it doesn't seem like anything is happening at all.
ASPX:
<asp:Repeater ID="rptList" runat="server" ViewStateMode="Enabled">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="NoteTextBox" runat="server"></asp:TextBox>
<asp:Button ID="SubmitNote" runat="server" Text="Button" OnClick="lnkClient_Click" CommandName="AddNote" CommandArgument='<%# Eval("UID")%>'/>
<asp:Label ID="ShowNotes" runat="server" Text='<%# getNotes(Eval("UID").ToString())%>'></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
CODEBEHIND - This is what should be executed on click. I replaced my SQL code with Response.Write:
public void lnkClient_Click(object sender, EventArgs e)
{
Button btn = (Button)(sender);
string FID = btn.CommandArgument.ToString();
string note = ((TextBox)rptList.Items[0].FindControl("NoteTextBox")).Text;
Response.Write(FID + " " + note);
}
UPDATE: Changed some settings and now the only problem I am having is that the text entered client side is not passed to the command.
Try this
protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("AddNote"))
{
string FID =e.CommandArgument.ToString();
TextBox txtNote=e.Item.FindControl("NoteTextBox") as TextBox;
string note=txtNote.Text;
Response.Write(FID + " " + note);
}
}
and in Mark up
<asp:Repeater ID="rptList" runat="server" OnItemCommand="Repeater_OnItemCommand" ViewStateMode="Enabled">

Upload file in asp, AJAX

where error? if i dont use ajax, then form is valid, and file is uploading, but now page reload and...file not load in path, why?
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<p>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Upload" Text="downdoad..."
BackColor="#0099CC" Font-Size="Small" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
downdoad...
</ProgressTemplate>
</asp:UpdateProgress>
Fileupload doesn't work in an updatepanel,
you have a workaround on this url
http://www.codeproject.com/useritems/simpleajaxupload.asp
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/Uploads" + FileUpload1.FileName));
Label1.Text = "Saved Successfully";
}
else
{
Label1.Text = "File Not found";
}
}
In the above code file does not Add in Upload Folder

Categories