I'm creating a .net program that uploads images to the FTP server. A blank listbox is populated dynamically on the client side from the value of the html input file widget. There is also a requiredfieldva lidator on the listbox to make sure there is an item in the listbox. When I click the button to submit, however, the listbox clears itself and a validation error shows. Below is the relevant code in C# plus the Javascript code. Thanks in advance.
.aspx code: -
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" CodeFile="logopageAdmin.aspx.cs" Inherits="_Default" trace=true %>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>Untitled Document</title>
-
</head>
-
<body>
-
<form id="attachme" method="post" encType="multipart/form-data" runat="server">
-
<div id="fileSelection">
-
<input type="file" id="fileDialog" name="fileDialog" />
-
<input id="addFile" name="addFile" type="button" value="Add" onclick="addToList()" />
-
</div>
-
<div id="listEdit">
-
<asp:ListBox ID="uploadList" runat="server" Rows="10" ValidationGroup="uploadSelection"></asp:ListBox><br/>
-
<asp:RequiredFieldValidator ID="reqUpload" runat="server" ControlToValidate="uploadList" InitialValue="" EnableClientScript="true" Display="Dynamic" ErrorMessage="Please select files to upload<br/>" ValidationGroup="uploadSelection" />
-
<input type="button" id="removeItem" name="removeItem" value="Remove Selected Files" onclick="removeFromList()" />
-
<asp:Button ID="upload" runat="server" Text="Upload Files" OnClick="Upload_ServerClick" ValidationGroup="uploadSelection" />
-
</div>
-
<asp:TextBox ID="status" runat="server" TextMode="MultiLine" Rows="10" Columns="60" Wrap="true" Enabled="false" />
-
...
-
</form>
-
</body>
-
</html>
-
C# code: -
protected void Page_Load(object sender, EventArgs e)
-
{
-
Page.RegisterClientScriptBlock("logopageAdminMain", "<script type=\"text/javascript\" src=\"logopageAdminScript.js\"></script>");
-
...
-
}
-
-
-
public void Upload_ServerClick(object sender, System.EventArgs e)
-
{
-
foreach (ListItem i in uploadList.Items)
-
{
-
try
-
{
-
//testing to see if listbox items are read
-
Response.Write(i.Value + "<br/>");
-
//upload code here
-
-
}
-
catch(Exception err)
-
{
-
//do code
-
}
-
}
-
}
-
javascript code: -
var listWidget;
-
-
function addToList()
-
{
-
var fileWidget = document.getElementById("fileDialog");
-
listWidget = document.getElementById("uploadList");
-
-
if (fileWidget.value.substring(fileWidget.value.length - 4) == ".jpg" || fileWidget.value.substring(fileWidget.value.length - 4) == ".gif" || fileWidget.value.substring(fileWidget.value.length - 4) == ".png")
-
{
-
var newOption = document.createElement("Option");
-
newOption.text = fileWidget.value;
-
newOption.value = fileWidget.value;
-
listWidget.options[listWidget.length] = newOption;
-
}
-
else
-
{
-
alert("Files must be either .jpg, gif, or .png");
-
}
-
}
-
-
function removeFromList()
-
{
-
listWidget = document.getElementById("uploadList");
-
-
for (var i = listWidget.length - 1; i>=0; i--)
-
{
-
if (listWidget.options[i].selected)
-
{
-
listWidget.remove(i);
-
}
-
}
-
}
7 3132
What i understand is that u have assigned the element in listbox from client side (i.e. from java script) addToList() doing this .. and when the postback occur server doesnot know the items of listbox and clear it out and after postback listbox contain no element in it ... for this u have to call the this addToList()
function when the postback occur ... bcoz client (browser) is stateless it can not maintain its state so u have to call java script function once again after the post back .....
Thanks, Shabazz, I'll give it a try! :D
Frinavale 9,735
Recognized Expert Moderator Expert
I'm creating a .net program that uploads images to the FTP server. A blank listbox is populated dynamically on the client side from the value of the html input file widget. There is also a requiredfieldva lidator on the listbox to make sure there is an item in the listbox. When I click the button to submit, however, the listbox clears itself and a validation error shows. Below is the relevant code in C# plus the Javascript code. Thanks in advance.
...
What is the validation error?
I think that ShahbazAshraf is right.
The server loads controls based on their ViewState.
When you add items to a listbox, you could be messing this up.
What is the validation error?
I think that ShahbazAshraf is right.
The server loads controls based on their ViewState.
When you add items to a listbox, you could be messing this up.
The validation error is line 16. It basically acts as if there were no items in the listbox at all.
Frinavale 9,735
Recognized Expert Moderator Expert
The validation error is line 16. It basically acts as if there were no items in the listbox at all.
Could you please copy/paste the exact error here.
Again, I'm sure it's because your ListBox is registered into your ViewState, changing the ListBox with JavaScript is going to mess this up and an error will occur....either that or the ListBox (when recreated on the server) will be empty upon postback every time (because it's loaded based on it's ViewState).
I'm creating a .net program that uploads images to the FTP server.
I don't think you can use a ListBox to upload files anyways.....
I'm pretty sure you can only use UploadControls (<input> is the html tag for it)
Have you seen this tutorial?
-Frinny
Below are the filepaths added to the listbox.
After I click "Upload Files", the list box clears itself and I get the validation error.
I did run across a control like the one you linked to. It just seemed easier to plug the item value from the listbox into FTPWebRequest. I'll look into that again, though.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Richard Fennell |
last post by:
I am trying to use asp:listbox on a form, when a user selects a row in the
asp:listbox a panel is displayed on the centre of the screen with a message,
a bit like a model dialog on the form.
The problem I have is that the portion of the panel that intersets the
asp:listbox is behind the asp:listbox, whilst the rest is in front of any
other...
|
by: John Giblin |
last post by:
I have a ListBox within a repeater tag and I am trying to set the selected
value. I tried the following without success. I was also tryng to make a
method for the selectedindex but I do not know how to pass the instance of
this asp:listbox control.
<asp:ListBox id="ListBox1" name="ListBox1" DataSource='<%#
GetDropDownQuantities(...
|
by: Ray Valenti |
last post by:
I have a ASP listbox that I am trying to populate with two fields, one for
display (Category) and one to store (ID) as the selected item. I can
successfully populate and view the list. How ever after specifying the
field to hold the selected value with the Databindings property, the error
below show up.
VS changes the DataSource from...
|
by: Daniel |
last post by:
hi,
I had an asp:listbox, and everytime i click item inside, the bar
automatically go to the top, is there any way to keep the scroll position?
I turn on the smartNavigation, it still doesn't work.
Thanks ahead.
|
by: Dan Nash |
last post by:
Hi again!
Right, okay... im now trying to get the value of an <asp:TextBox> control.
so I have on my ASPX page...
<asp:ListBox id="results" runat="server"></asp:ListBox>
<asp:TextBox id="txtSelected" runat="server"></asp:TextBox>
In my code-behind Page_Load i have..
results.Items.Add("Dan");
| |
by: Simon Prince |
last post by:
Help
I have a ASP:Listbox on a form. My page Adds items to this this via
Client-Side Script only.
Such as...
var vObj_TargetElement =
document.getElementById("_PageTemplate_innerHolder_oASP_Lbx_Invoice");
var vObj_SourceElement = document.getElementById("oHTML_Tbx_Invoice");
var vInt_OptionTotal = vObj_TargetElement.length++;
|
by: Ryan Taylor |
last post by:
Hello.
I have an application where I need the user to be able to add items to a
listbox. I've implemented this via javascript. The listbox is an
<asp:ListBox>. However, when the user submits the form those items do not
get posted. My listbox.Items.Count always returns 0.
I want the user to be able to add items without having to resort to...
|
by: Chris Kettenbach |
last post by:
Good morning, Does anyone happen to know if there's a way to make an array
of the selected items in an asp:ListBox? I know you can loop through and
check the selected property, my question is how do you know how large to
make your array without looping through the list twice.
I had like button_click(object sender, EventArgs e)
{
...
|
by: Mark Rae |
last post by:
Hi,
Looking for some advice again...
Imagine two ListBox controls denoting something like students and team
membership e.g. many students can be members of many teams (e.g. the hockey
team, the football team, the athletics team etc).
Team membership is managed on a standard web page with two ListBox
controls - the one on the left shows...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |