asp:listbox postback issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikeh3275
    New Member
    • Mar 2008
    • 15

    asp:listbox postback issue

    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:
    [code=asp]
    <%@ Page Language="C#" ContentType="te xt/html" ResponseEncodin g="utf-8" CodeFile="logop ageAdmin.aspx.c s" Inherits="_Defa ult" 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>Untitl ed Document</title>
    </head>
    <body>
    <form id="attachme" method="post" encType="multip art/form-data" runat="server">
    <div id="fileSelecti on">
    <input type="file" id="fileDialog " name="fileDialo g" />
    <input id="addFile" name="addFile" type="button" value="Add" onclick="addToL ist()" />
    </div>
    <div id="listEdit">
    <asp:ListBox ID="uploadList " runat="server" Rows="10" ValidationGroup ="uploadSelecti on"></asp:ListBox><br/>
    <asp:RequiredFi eldValidator ID="reqUpload" runat="server" ControlToValida te="uploadList " InitialValue="" EnableClientScr ipt="true" Display="Dynami c" ErrorMessage="P lease select files to upload<br/>" ValidationGroup ="uploadSelecti on" />
    <input type="button" id="removeItem " name="removeIte m" value="Remove Selected Files" onclick="remove FromList()" />
    <asp:Button ID="upload" runat="server" Text="Upload Files" OnClick="Upload _ServerClick" ValidationGroup ="uploadSelecti on" />
    </div>
    <asp:TextBox ID="status" runat="server" TextMode="Multi Line" Rows="10" Columns="60" Wrap="true" Enabled="false" />
    ...
    </form>
    </body>
    </html>
    [/code]
    C# code:
    [code=cpp]
    protected void Page_Load(objec t sender, EventArgs e)
    {
    Page.RegisterCl ientScriptBlock ("logopageAdmin Main", "<script type=\"text/javascript\" src=\"logopageA dminScript.js\" ></script>");
    ...
    }


    public void Upload_ServerCl ick(object sender, System.EventArg s e)
    {
    foreach (ListItem i in uploadList.Item s)
    {
    try
    {
    //testing to see if listbox items are read
    Response.Write( i.Value + "<br/>");
    //upload code here

    }
    catch(Exception err)
    {
    //do code
    }
    }
    }
    [/code]
    javascript code:
    [code=javascript]
    var listWidget;

    function addToList()
    {
    var fileWidget = document.getEle mentById("fileD ialog");
    listWidget = document.getEle mentById("uploa dList");

    if (fileWidget.val ue.substring(fi leWidget.value. length - 4) == ".jpg" || fileWidget.valu e.substring(fil eWidget.value.l ength - 4) == ".gif" || fileWidget.valu e.substring(fil eWidget.value.l ength - 4) == ".png")
    {
    var newOption = document.create Element("Option ");
    newOption.text = fileWidget.valu e;
    newOption.value = fileWidget.valu e;
    listWidget.opti ons[listWidget.leng th] = newOption;
    }
    else
    {
    alert("Files must be either .jpg, gif, or .png");
    }
    }

    function removeFromList( )
    {
    listWidget = document.getEle mentById("uploa dList");

    for (var i = listWidget.leng th - 1; i>=0; i--)
    {
    if (listWidget.opt ions[i].selected)
    {
    listWidget.remo ve(i);
    }
    }
    }[/code]
    Last edited by Frinavale; Mar 12 '08, 07:13 PM. Reason: added [code] tags
  • ShahbazAshraf
    New Member
    • Mar 2008
    • 36

    #2
    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 .....

    Comment

    • mikeh3275
      New Member
      • Mar 2008
      • 15

      #3
      Thanks, Shabazz, I'll give it a try! :D

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by mikeh3275
        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.

        Comment

        • mikeh3275
          New Member
          • Mar 2008
          • 15

          #5
          Originally posted by Frinavale
          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.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by mikeh3275
            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).

            Originally posted by mikeh3275
            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

            Comment

            • mikeh3275
              New Member
              • Mar 2008
              • 15

              #7
              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.

              Comment

              • mikeh3275
                New Member
                • Mar 2008
                • 15

                #8
                I found a method that seems to work:

                In our last post , we saw how to add a row dynamically to a gridview. But we still have to see how to persist values added on the client sid...

                Comment

                Working...