Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a Multi Select Listbox from the Request Object.

Status
Not open for further replies.

MayoorPatel

Programmer
Apr 10, 2006
35
GB
Hi there I have a form using VB.NET which submits data to the database and then posts back to itself and populates the dropdowns with the values I have just added to the database.

I have a multi select dropdown called "drpCategory" which needs to populate the selected items from the request object once the form posts back to itself.

I have the following code for a single select but this now doesnt work as I have multiple values.

Code:
If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
                subcategory.Items.FindByValue(Request(subcategory.ID.ToString).ToString).Selected = True
            End If

So i need to know how to amend this code for my needs.

Thankyou in advance for any help!

Mayoor

 
when you use multiple values the request.form returns values in a CSV format.

try this:
response.write(Request(subcategory.ID.ToString).ToString)

you will note that the values will be in this format:
3,4,6

therefore your FindByValue will NOT work. the workaround is that you will have to split the request string, put a loop and select the values...


Known is handfull, Unknown is worldfull
 
Ahhh right ok.. Right ive got something like this.


Code:
If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
                aryTextFile = Request(subcategory.ID.ToString).ToString().Split(",")
                For Each i In aryTextFile
                    if i = ??????
                        subcategory.Items.FindByValue(i).Selected = True
                Next
            End If

Obviously this is wrong as i have no idea about the syntax, can you help?
 
ok guys i now have something which I think is close

Code:
If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
                arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")
                For Each i In arySelectedSubcategoryItems
                    For Each item As ListItem In subcategory.Items
                        If item.Value = i Then
                            subcategory.Items(i).Selected() = True
                        End If
                    Next
                Next
            End If

but its giving me this error

Code:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error: 


Line 183:                    For Each item As ListItem In subcategory.Items
Line 184:                        If item.Value = i Then
Line 185:                            subcategory.Items(i).Selected() = True
Line 186:                        End If
Line 187:                    Next

 
try this:


If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")
For Each i In arySelectedSubcategoryItems
if not(i.equals(srting.empty)) then
subcategory.Items.FindByValue(i).Selected = True
end if

Next
End If



Known is handfull, Unknown is worldfull
 
Hi there, that brings up the form but it only selects the first option unfortunately. :(
 
hmm, its multi select list right?


try this:


If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")
response.write("Trying to select - " & i &"<br>")
For Each i In arySelectedSubcategoryItems
if not(i.equals(srting.empty)) then
subcategory.Items.FindByValue(i).Selected = True
response.write("Selected - " & i &"<br>")
end if
response.write("<hr>")

Next
End If


that code will give you some kind of statistics at the top of the page...

Known is handfull, Unknown is worldfull
 
Hi there vbkris Im not seeing any output at the top of the page all I see is the postabck form i.e same as usual??
 
which means that it is not even entering the loop,

try a simple:
response.write(Request(subcategory.ID.ToString))

before the loop, does it print any values???


Known is handfull, Unknown is worldfull
 
It is entering the loop because I have put a breakpoint where that piece of code starts and it loops through all the code fine. The problem is with the postback. Let me show you.

the page load calls

Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        decisionId = Utility.ParseInt(Request.QueryString("Id"), 0)
        Me.ConfigurePage()
        If Not IsPostBack Then

            ' NOT Postback
            Me.PopulateDropDowns()
            If Not decisionId = 0 Then
                Me.PopulateForm()
            Else
                Me.PopulalateNew()
            End If
            Me.ListJudgmentFiles()
        Else
            ' IS Postback
            Me.PopulateSubCategoryFromRequest(drpMainCategory, drpMainSubCategory)
        End If

    End Sub

as you can see if there is a postback the follwing call is made

Me.PopulateSubCategoryFromRequest(drpMainCategory, drpMainSubCategory)

to this procedure

Code:
 Private Sub PopulateSubCategoryFromRequest(ByVal category As DropDownList, ByVal subcategory As ListBox)
        If Not category.SelectedItem.Value = -1 Then
            Utility.PopulateSubCategory(subcategory, category.SelectedItem.Value)


            Dim i As Integer
            Dim arySelectedSubcategoryItems() As String

            If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
                arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")
                Response.Write("Trying to select - " & i & "<br>")
                For Each i In arySelectedSubcategoryItems
                    If Not (i.Equals(String.Empty)) Then
                        subcategory.Items.FindByValue(i).Selected = True
                        Response.Write("Selected - " & i & "<br>")
                    End If
                    Response.Write("<hr>")

                Next
            End If
  End If
    End Sub

Can you see anything here which might be causing a problem?

 
>>The problem is with the postback. Let me show you.

where exactly is it happening? when you mean by looping i am assuming the for loop using arySelectedSubcategoryItems.

if the loop is working then i guess we need to shif the response.write stmt outside.

e.g.:
arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")
Response.Write("Trying to select - " & i & "<br>")
For Each i In arySelectedSubcategoryItems
Response.Write("Selected - " & i & "<br>")
If Not (i.Equals(String.Empty)) Then
subcategory.Items.FindByValue(i).Selected = True
End If
Response.Write("<hr>")

Next

what i am trying to check here is whether there really is any value in the postback...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top