473,569 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

asp:listbox postback issue

15 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" CodeFile="logopageAdmin.aspx.cs" Inherits="_Default" trace=true %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.     <head>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.         <title>Untitled Document</title>
  7.     </head>
  8.     <body>
  9.          <form id="attachme" method="post" encType="multipart/form-data" runat="server">
  10.              <div id="fileSelection">
  11.                 <input type="file" id="fileDialog" name="fileDialog" />
  12.                 <input id="addFile" name="addFile" type="button" value="Add" onclick="addToList()" />
  13.              </div>
  14.              <div id="listEdit">
  15.                  <asp:ListBox ID="uploadList" runat="server" Rows="10" ValidationGroup="uploadSelection"></asp:ListBox><br/>
  16.                 <asp:RequiredFieldValidator ID="reqUpload" runat="server" ControlToValidate="uploadList" InitialValue="" EnableClientScript="true" Display="Dynamic" ErrorMessage="Please select files to upload<br/>" ValidationGroup="uploadSelection" />
  17.                 <input type="button" id="removeItem" name="removeItem" value="Remove Selected Files" onclick="removeFromList()" />
  18.                 <asp:Button ID="upload" runat="server" Text="Upload Files" OnClick="Upload_ServerClick" ValidationGroup="uploadSelection" />
  19.              </div>
  20.              <asp:TextBox ID="status" runat="server" TextMode="MultiLine" Rows="10" Columns="60" Wrap="true" Enabled="false" />
  21.             ...
  22.          </form>
  23.     </body>
  24. </html>
  25.  
C# code:
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     Page.RegisterClientScriptBlock("logopageAdminMain", "<script type=\"text/javascript\" src=\"logopageAdminScript.js\"></script>"); 
  4.         ...
  5. }
  6.  
  7.  
  8. public void Upload_ServerClick(object sender, System.EventArgs e)
  9. {
  10.     foreach (ListItem i in uploadList.Items)
  11.     {
  12.         try
  13.         {
  14.             //testing to see if listbox items are read
  15.             Response.Write(i.Value + "<br/>");
  16.             //upload code here    
  17.  
  18.         }
  19.         catch(Exception err)
  20.         {
  21.                         //do code
  22.             }
  23.         }
  24. }
  25.  
javascript code:
Expand|Select|Wrap|Line Numbers
  1. var listWidget; 
  2.  
  3. function addToList()
  4. {
  5.     var fileWidget = document.getElementById("fileDialog");
  6.     listWidget = document.getElementById("uploadList");
  7.  
  8.     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")
  9.     {
  10.         var newOption = document.createElement("Option");
  11.         newOption.text = fileWidget.value;
  12.         newOption.value = fileWidget.value;
  13.         listWidget.options[listWidget.length] = newOption;
  14.     }
  15.     else
  16.     {
  17.         alert("Files must be either .jpg, gif, or .png");    
  18.     }
  19. }
  20.  
  21. function removeFromList()
  22. {
  23.     listWidget = document.getElementById("uploadList");
  24.  
  25.     for (var i = listWidget.length - 1; i>=0; i--)
  26.     {
  27.         if (listWidget.options[i].selected)
  28.         {
  29.             listWidget.remove(i);
  30.         }
  31.     }
  32. }
Mar 10 '08 #1
7 3132
ShahbazAshraf
36 New Member
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 .....
Mar 12 '08 #2
mikeh3275
15 New Member
Thanks, Shabazz, I'll give it a try! :D
Mar 12 '08 #3
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.
Mar 12 '08 #4
mikeh3275
15 New Member
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.
Mar 13 '08 #5
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
Mar 13 '08 #6
mikeh3275
15 New Member
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.
Mar 17 '08 #7
mikeh3275
15 New Member
I found a method that seems to work:

http://dotnetsavvyblog.blogspot.com/...-gridview.html
Mar 20 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1399
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...
0
1175
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(...
1
4000
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...
1
9856
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.
2
2291
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");
2
1438
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++;
3
2714
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...
5
2895
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) { ...
2
3608
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...
0
7703
marktang
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...
0
7618
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...
0
7926
Oralloy
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. ...
0
8132
jinu1996
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...
1
7678
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...
1
5514
isladogs
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
0
944
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.