Home > Back-end >  Populate checklist in another webform
Populate checklist in another webform

Time:11-08

I have checklist with multiple selections now the user will preview the form in another webform after press preview button the issue here that the checklist not populated in the other form with all selections and only item selected

In first form I wrote the below code:

Dim Fruit as String = ChkFruit.SelectedValue

Redirect.response(home.aspx?"fruit="   Fruit)

In second form I wrote the below code:

ChkFruit1.Selectedvalue = QueryString("Fruit")

CodePudding user response:

Well in your example posted code, if we are to ONLY PASS one selected value, then what you have is a good start.

However, VERY NEW BIG LARGE MASSIVE different problem is now we allow "many" selections in the check box list. So with a NEW HUGE LARGE DIFFERENT kind of question and problem?

Then we will as a result need a VERY different solution. The large new big massive problem here is how to pass 1 value, or 15 choices? This of course as noted is quite a more difficult problem and challenge.

For passing a "list" or "array" of a whole bunch of possbile choices, then I suggest we do NOT use the URL parmaters (query parms in the URL).

So, what we need here is to build up some "list" of choices from the check box list, and pass that to the 2nd page. Now there are more ways to do this then there are flavors of ice cream, and we have a LOT of choices.

However, I going to suggest creating a List of chocies, and we pass that to the 2nd page we jump to.

So, our markup and button in the first page can look like this:

        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Grapes</asp:ListItem>
            <asp:ListItem>Bananna</asp:ListItem>
            <asp:ListItem>Cherry</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Submit" Width="158px" />

Ok, above is quite nice - not complex.

Now, for our button code? We need to build up, get/grab/obtain the list of selected values - that's what we will pass to the 2nd page.

So, above looks like this:

enter image description here

So, now our button/submit code on this web page:

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim MyCheckList As List(Of String) = New List(Of String)

    For Each MyItem As ListItem In CheckBoxList1.Items
        If MyItem.Selected Then
            MyCheckList.Add(MyItem.Value)
        End If
    Next

    Session("MyCheckList") = MyCheckList
    Response.Redirect("Test2.aspx")

End Sub

So, in place of "url messay parmaters"?

We create a List type variable (they are somewhat the same as an array()

We then add all selected items to that new list

We then stuff/place the new list into Session()

We then jump to the 2nd web page with the Reponse.Redirect

Ok, so now, on the target web page, we have to get that list we passed.

Now, on the 2nd page, the code ONLY needs that list.

but, lets assume that we have the same check box list on that 2nd page - and we want to fill out - show/display/have the Check box list show the same choices.

WHAT IS IMPORTANT here is how we are free to use the "list" we passed to that 2nd page. I can do anything with that nice list , but we have this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        Dim MyCheckList As List(Of String) = Session("MyCheckList")

        ' display the list of passed check box values

        For Each MySelected As String In MyCheckList
            Debug.Print(MySelected)
        Next

        ' or say fill out a check box list we have on this new page - 


        ' process EACH check box list item
        ' check if in our list - if yes, then check box/select it

        For Each MyItem As ListItem In CheckBoxList1.Items
            MyItem.Selected = MyCheckList.Contains(MyItem.Value)
        Next

    End If

End Sub

So, in this 2nd page, we demonstrate two examples.

First part of code example - JUST display the list of previous selections. You are thus now free to for each loop and process the check box list of selections.

Second part of code example. We actually take that list and check box a whole new check box list we have on the 2nd target page. Now, I doubt the check box list would be the same and duplicated on that 2nd page, but this code example shows how we could display the check box list "again" on the 2nd new target page.

And the code also demonstrates how to loop/process the list of selections, which is really at a the end of the day the most valuable part of this example code

So, in our 2nd page, if we have this markup:

     <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Grapes</asp:ListItem>
            <asp:ListItem>Bananna</asp:ListItem>
            <asp:ListItem>Cherry</asp:ListItem>
        </asp:CheckBoxList>
        <br />            

Then we would see this:

enter image description here

So when you have MORE then just one value to pass to the next page?

Then you can use session() in place of messy and difficult to use URL "parmaters". And those URL paramters are not all that great for passing "many" values like this new problem required.

If we were to only pass ONE value, and ONE selection, then the URL parmaters would be ok to use. But now since we need to pass "many" values then using session() to pass that information to the 2nd target page becomes a whole lot less work and efforts on your part.

  • Related