我現在有多個選擇的清單,用戶將在按下預覽按鈕后在另一個網路表單中預覽表單 這里的問題是清單未填充在其他表單中,所有選擇都只選擇了專案
在第一種形式中,我撰寫了以下代碼:
Dim Fruit as String = ChkFruit.SelectedValue
Redirect.response(home.aspx?"fruit=" Fruit)
在第二種形式中,我撰寫了以下代碼:
ChkFruit1.Selectedvalue = QueryString("Fruit")
uj5u.com熱心網友回復:
那么在您發布的示例代碼中,如果我們只傳遞一個選定的值,那么您擁有的是一個好的開始。
然而,VERY NEW BIG LARGE MASSIVE 不同的問題是現在我們允許在復選框串列中進行“許多”選擇。那么有一個新的巨大的不同型別的問題嗎?
那么我們將因此需要一個非常不同的解決方案。這里的大新大問題是如何傳遞 1 個值,或 15 個選項?如前所述,這當然是一個更困難的問題和挑戰。
為了傳遞一大堆可能的選擇的“串列”或“陣列”,我建議我們不要使用 URL 引數(URL 中的查詢引數)。
所以,我們在這里需要的是從復選框串列中建立一些選擇的“串列”,并將其傳遞給第二頁。現在有更多的方法可以做到這一點,然后有冰淇淋口味,我們有很多選擇。
但是,我建議創建一個選擇串列,然后我們將其傳遞到我們跳轉到的第二頁。
所以,我們在第一頁中的標記和按鈕看起來像這樣:
<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" />
好的,上面很好 - 不復雜。
現在,對于我們的按鈕代碼?我們需要建立、獲取/獲取/獲取選定值的串列——這就是我們將傳遞給第二頁的內容。
所以,上面看起來像這樣:

所以,現在我們在這個網頁上的按鈕/提交代碼:
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
那么,代替“url messay parmaters”?
我們創建了一個 List 型別的變數(它們有點像一個陣列()
然后我們將所有選定的專案添加到該新串列中
然后我們將新串列填充/放入 Session()
然后我們使用 Reponse.Redirect 跳轉到第二個網頁
好的,現在,在目標網頁上,我們必須獲取我們通過的串列。
現在,在第二頁上,代碼只需要該串列。
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:

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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352603.html
