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")
In your example posted code, if we are to ONLY PASS one selected value, then what you have is a good start.
However, a different problem is now we allow "many" selections in the check box list. So with a new kind of question and problem?
Then we will as a result need a VERY different solution. The big 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 possible choices, then I suggest we do NOT use the URL parameters (query parameters in the URL).
What we need here is to build up some "list" of choices from the check box list, and pass that to the second page. Now there are more ways to do this then there are flavors of ice cream, and we have a LOT of choices.
I am going to suggest creating a list of choices, and we pass that to the second 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, the above is quite nice - not complex.
Now, for our button code? We need to build up, get the list of selected values - that's what we will pass to the second page.
So, above looks like this:
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 messy parameters"?
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 second web page with the Response.Redirect
OK, so now, on the target web page, we have to get that list we passed.
Now, on the second page, the code ONLY needs that list.
but, let's assume that we have the same check box list on that second 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 second 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 second 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 second target page. Now, I doubt the check box list would be the same and duplicated on that second page, but this code example shows how we could display the check box list "again" on the second 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 second 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:
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 "parameters". And those URL parameters 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 parameters would be OK to use. But now since we need to pass "many" values then using session() to pass that information to the second target page becomes a whole lot less work and efforts on your part.