Remove list of items in listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yookify
    New Member
    • Mar 2010
    • 14

    Remove list of items in listbox

    Im Using asp .net with Vb Coding.
    I have to remove the list of selected items from listbox .
    Code:
    Dim i As Integer
            For i = 0 To listbox1.Items.Count - 1
                listbox1.Items.Remove(listbox1.SelectedValue.ToString())
            Next
    My doubt is If i run the page again the listbox shows the items which i deleted already.
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Hello yookify,

    You are correct when you stated that the items will reappear if the page is revisited. Unless, you pull the values from a data source. When you remove the item from the listbox then remove it from the data source too.

    Hope that helps,
    CroCrew~

    Comment

    • yookify
      New Member
      • Mar 2010
      • 14

      #3
      Thanks For Ur Reply...
      But there is no other way ah?
      items should not be deleted in table but remove from the listbox after selection completes....
      if u can please tell...
      Thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Please post the code where you add the items to the ListBox.
        At what stage in the page life cycle are you adding the items?
        At what stage in the page life cycle are you removing the items?
        Are you always adding items when you should only be doing so the first time the page loads?

        Comment

        • yookify
          New Member
          • Mar 2010
          • 14

          #5
          Originally posted by Frinavale
          Please post the code where you add the items to the ListBox.
          At what stage in the page life cycle are you adding the items?
          At what stage in the page life cycle are you removing the items?
          Are you always adding items when you should only be doing so the first time the page loads?
          From Database i have to take a list of items.using datasource connections i added the items in the listbox.

          here the admin is assigning work to agent its a problem they given to me.

          admin is used to select the names from listbox and used to assign to a particular agent.after admin select a items for agent ,it item i selected should be removed from the listbox not from the table.

          if i use this coding it removing the items from listbox but again when i run the from its shows the items which i previously removed...
          Code:
          Dim i As Integer 
                  For i = 0 To listbox1.Items.Count - 1 
                      listbox1.Items.Remove(listbox1.SelectedValue.ToString()) 
                  Next

          can u please help me...
          Last edited by Frinavale; Apr 13 '10, 03:51 PM. Reason: Please post code in [code]... [/code] tags. Added code tags.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I think you misunderstood what I was trying to get at.

            In ASP.NET there's something called the Page Life Cycle.

            Basically this is what happens when ever the browser makes a request to the server for your page (whether it be a postback or not):
            • The server figures out what resource is being requested and forwards it to ASP.NET
            • ASP.NET checks if the user is authorized to access the resource/page requested...and if they are allowed it creates the Request Object etc
            • Your web page's Init Event is fired
            • Your pages Load event is fired
            • Any code in your page that handles any events that may have been raised is executed
            • The page's PreRender event is fired
            • The page's Render event is fired which converts all of the ASP.NET controls into HTML and it is sent to the browser
            • The page is unloaded, all objects used for the page are destroyed
            • The server waits for the next request


            This happens every time the browser makes a request to the server for your page.

            If you have code in the Page Load event that populates the list...and this population happens every time the page loads because you haven't checked whether or not it IsPostback....t hen everything you have removed from the list will be added again!

            Likewise if you have code that populates the list in your PreRender event...same effect: it will appear that you have not removed items.

            If you are using a DataSource to populate your ListBox with items then you must modify the object you are using as the datasource to remove the items from the list...you cannot use the Items.Remove method as you are.


            So, again, I ask you to please post the code that populates the ListBox with items...and please specify the Event that this code is in.

            -Frinny

            Comment

            • yookify
              New Member
              • Mar 2010
              • 14

              #7
              Originally posted by Frinavale
              I think you misunderstood what I was trying to get at.

              In ASP.NET there's something called the Page Life Cycle.

              Basically this is what happens when ever the browser makes a request to the server for your page (whether it be a postback or not):
              • The server figures out what resource is being requested and forwards it to ASP.NET
              • ASP.NET checks if the user is authorized to access the resource/page requested...and if they are allowed it creates the Request Object etc
              • Your web page's Init Event is fired
              • Your pages Load event is fired
              • Any code in your page that handles any events that may have been raised is executed
              • The page's PreRender event is fired
              • The page's Render event is fired which converts all of the ASP.NET controls into HTML and it is sent to the browser
              • The page is unloaded, all objects used for the page are destroyed
              • The server waits for the next request


              This happens every time the browser makes a request to the server for your page.

              If you have code in the Page Load event that populates the list...and this population happens every time the page loads because you haven't checked whether or not it IsPostback....t hen everything you have removed from the list will be added again!

              Likewise if you have code that populates the list in your PreRender event...same effect: it will appear that you have not removed items.

              If you are using a DataSource to populate your ListBox with items then you must modify the object you are using as the datasource to remove the items from the list...you cannot use the Items.Remove method as you are.


              So, again, I ask you to please post the code that populates the ListBox with items...and please specify the Event that this code is in.

              -Frinny

              Thanks For ur Reply

              This the Code i Used to add and remove items

              Code:
              Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim i As Integer
                For i = 0 To ListBox1.Items.Count - 1
                  If ListBox1.Items(i).Selected Then
                    Try
                      mycon.Open()
                      mycmd.CommandText = "Update main Set Agent_name='" & DropDownList1.SelectedValue & "' where Lastname ='" & ListBox1.Items(i).Text & "' "
                      mycmd.Connection = mycon
                      x = mycmd.ExecuteNonQuery() 
                    Catch ex As Exception
                      Label1.Text = ex.Message
                    Finally
                      mycon.Close()
                    End Try
                  End If
                Next
                MsgBox("Work Is Assigned")
                Dim j As Integer
                For j = 0 To ListBox1.Items.Count - 1
                  ListBox1.Items.Remove(ListBox1.SelectedValue.ToString())
                Next
              End Sub


              For Dropdown Listbox AND for listbox i used enable postback option...
              For Both i used DATASOURCE ID.manual connection not written any coding for connections.


              This is the thing i have done ....

              Can u please reply me....
              Last edited by Frinavale; Apr 14 '10, 06:28 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Yookify,

                This is not the code I was looking for.

                I want to see the code that populates the list...not the code that removes the items from the list.

                I don't think you understand what I'm looking for so I'll make it simple for you to show me what I need to see in order to help you...

                Please post your Page Load code and your Page PreRender code as well if you have any.

                Comment

                • yookify
                  New Member
                  • Mar 2010
                  • 14

                  #9
                  Originally posted by Frinavale
                  Yookify,

                  This is not the code I was looking for.

                  I don't think you understand what I'm looking for so I'll make it simple for you to show me what I need to see in order to help you...

                  Please post your Page Load code and your Page PreRender code as well if you have any.
                  Thank you....

                  I Didt wrote any codings in page load....

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Then where and how are you populating the ListBox with items???

                    Comment

                    • MrMancunian
                      Recognized Expert Contributor
                      • Jul 2008
                      • 569

                      #11
                      I think that when he doesn't do it in runtime, it's hard coded and then you can remove as much as you want, but as your code gets read again, the items will return...

                      Steven

                      Comment

                      • yookify
                        New Member
                        • Mar 2010
                        • 14

                        #12
                        Originally posted by Frinavale
                        Then where and how are you populating the ListBox with items???
                        I Set manual connections.cli ck on the listbox and choose configure data source ,select the connections ,table name and selected the fields i want and click ok .....
                        this the thing i done in listbox and dropdownlistbox .....

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          Well that explains it.

                          If you use this technique, than when you remove the items from your ListBox you also have to remove them from the Data Source. This means that in order for this to work, you have to remove the items from the database as well as from your ListBox. You may not want to do this since it could mess things up for other people using the webpage.

                          I recommend that you change your technique.

                          Instead of relying on the "magical powers" that you are right now, let's do this manually.

                          In your Page Load event, the first time the page is loaded (!IsPostback) retrieve the items from the database for your ListBox. Store this table in Session or something so that you can use it again later. If it is not the first time the page is loaded (IsPostback) then retrieve the list of items from Session. In the Page PreRender event, set the ListBox.DataSou rce to the table with the ListItems in it and call the DataBind() method to bind the two together.

                          When you want to remove items from the ListBox, remove the items from the table stored in Session...

                          Does this make sense?

                          -Frinny

                          Comment

                          • yookify
                            New Member
                            • Mar 2010
                            • 14

                            #14
                            I cant find answ for this Can u Please guide me so only im repeating,sorry if it is wrong....

                            I know how to do Connections...

                            I need help....

                            This coding works well...
                            Code:
                            While y.Read() 
                            Response.Write("Name i " & y("LastName")) 
                            Response.Write(y("Firstname")) 
                            Response.Write(y("id")) 
                            End While 
                            y.Close()
                            I want get data from table and display the data

                            If there are three aaaa,Then all should be displayed.
                            Name:aaaa
                            ssss
                            1234
                            Name:aaaa
                            eeee
                            23456
                            Name:aaaa
                            dddd
                            3456

                            if use response.write it works well
                            I want to display it on the Textbox

                            I Set textbox mode as multiple line and write the following coding:

                            Code:
                            While y.Read() 
                            textbox9.Text = ("Name is " & y("Lastname") & y("id") & y("Firstname")) 
                            End While
                            its showing only one data like this ...
                            name:aaa
                            dddd
                            3456

                            how can i do it

                            can u please help me....

                            Thanks in advance..

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              I'm not sure how this has anything to do with a ListBox....

                              But anyways, you are overwriting the TextBox.Text property with a new String value every time you loop.
                              Code:
                              While y.Read() 
                               textbox9.Text = ("Name is " & y("Lastname") & y("id") & y("Firstname")) 
                              End While
                              What you want to do is concatenate the string to the existing text instead...that way you don't over write the text in the text box.

                              You could use a variable (a StringBuilder) to create the String to display in the TextBox and then set the TextBox.Text value after you've done this.
                              Code:
                              Dim strBuilder As New StringBuilder
                              While y.Read() 
                               strBuilder.Append(" Name is ")
                               strBuilder.Append(y("Lastname"))
                               strBuilder.Append(y("id"))
                               strBuilder.Append(y("Firstname")) 
                              End While
                              textbox9.Text = strBuilder.ToString()
                              Or you could just concatenate what is in the TextBox with the next string:
                              Code:
                              While y.Read() 
                               textbox9.Text =  textbox9.Text+ (" Name is " & y("Lastname") & y("id") & y("Firstname")) 
                              End While

                              -Frinny

                              Comment

                              Working...