2011-08-10 27 views
0

我試圖通過一堆我在我的aspx頁面上創建的複選框循環。我想獲取所有的值,並將它們解析爲一個字符串,作爲後置變量發送到另一個頁面。這是我到目前爲止有:asp.net通過複選框循環獲取值發送到另一頁

aspx頁面:

<div class="ReportsCustomBox"><asp:CheckBox ID="CheckBox0" runat="server" /> Name</div> 
<div class="ReportsCustomBox"><asp:CheckBox ID="CheckBox1" runat="server" /> Training Year</div> 
<div class="ReportsCustomBox"><asp:CheckBox ID="CheckBox2" runat="server" /> Continuity Clinic</div> 
etc... 

aspx.vb文件:

Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Submit.Click 
    Dim targetURL As String 
    targetURL = "report.aspx?" 

    ' This is the part i can't figure out. 
    ' For Each checkbox on page 
     'if checkbox is checked 
     'targetURL &= checkboxid & "=true&" 

End Sub 

我的目標是建立另一個頁面,然後將使用查詢字符串變量檢查這些值,並從它們中構建一個列表視圖。 (報告頁面,基本上讓用戶選擇他們想要在報告中看到的列的複選框)

任何幫助或方向都非常感謝! 謝謝!

回答

1
直接

不回答你的問題,但除非你有一個具體的原因不是,你應該直接將表單發佈到其他頁面。不要打擾後面的代碼會變得脆弱和容易出錯。

使用Button.PostbackUrl屬性要發佈的頁面切換到

<asp:Button id="Submit" Text="Submit!" PostbackUrl="~/other-page.aspx"/> 

而在其他頁面使用Request.Form訪問貼出值的代碼隱藏。

根據您的情況,您甚至可以強制鍵入上一頁,以便更輕鬆地訪問屬性和值。 http://dotnettipoftheday.org/tips/strongly-typed-access-to-previous-page.aspx

+0

對不起,我知道這不是一個直接的答案,但是當你是像我這樣的菜鳥時,你甚至不知道有時要問什麼。但這是完美的,正是我真正想要的。 :) 謝謝! – russds

+0

讓我知道是否有辦法編輯此問題,以使其對社區更有價值。我知道我將此標記爲解決方案,但這不是我確切問題的確切答案。 (我應該回去編輯問題了嗎?)謝謝傑夫閱讀我的菜單。 :) – russds

+0

不需要爲此道歉。這就是stackoverflow在這裏!我很高興能幫助你。使這種方式更加困難的一個部分是其他頁面的Request.Form集合中的值看起來有點時髦。你應該運行調試器來檢查'Request.Form.AllKeys'來找出你需要如何引用它們。 – Jeff

2

你可以通過頁面控制迴路,並找出(我的vb.net生鏽,但希望這給你的想法):

For Each ctrl As Control In Page.Controls 
    If TypeOf ctrl Is CheckBox AndAlso CType(ctrl, CheckBox).Checked Then 
     targetURL &= CType(ctrl, CheckBox).Id & "=true&" 
    End If 
Next 
1

您可以循環訪問頁面的控件集合,但是您不會找到位於容器控件(如Table或GridView)內的CheckBox。

這將找到所有複選框:

Public Module ExtensionMethods 

    <Runtime.CompilerServices.Extension()> _ 
    Public Sub GetControlsRecursively(ByVal parentControl As System.Web.UI.Control, ByVal type As Type, ByRef controlCollection As List(Of Control)) 
     If parentControl.GetType = type Then 
      controlCollection.Add(parentControl) 
     End If 

     For Each c As System.Web.UI.Control In parentControl.Controls 
      c.GetControlsRecursively(type, controlCollection) 
     Next 
    End Sub 

End Module 

你可以這樣調用它:

Dim allCheckboxes As New List(Of Control) 
Me.Page.GetControlsRecursively(GetType(CheckBox), allCheckboxes) 

和迴路他們以這樣的方式

Dim txt As New System.Text.StringBuilder() 
For Each chk As CheckBox In allCheckboxes 
    If chk.Checked Then txt.Append(chk.ID).Append("=true&") 
Next 
If txt.Length <> 0 Then txt.Length -= 1 
Dim url = txt.ToString 
0

另一種選擇,根據你所說的你想要做的事情,就是跳過這個話題e複選框控件完全,並且用正常的複選框代替。我建議這樣做,因爲它可以更容易處理。

例如,你會把你的HTML以下(最主要的是,你把相同的名稱屬性值對所有的人,並將其值設置爲有意義):

<div class="ReportsCustomBox"><input type="checkbox" name="reportColumns" value="Name" /> Name</div> 
<div class="ReportsCustomBox"><input type="checkbox" name="reportColumns" value="TrainingYear" /> Training Year</div> 
<div class="ReportsCustomBox"><input type="checkbox" name="reportColumns" value="ContinuityClinic" /> Continuity Clinic</div> 

然後,它將在Form集合中作爲一個變量使用逗號分隔的值進行檢查(例如,如果您選中了這兩個框,則Request.Form("reportColumns")將返回"Name,TrainingYear"。然後,您可以直接在查詢字符串中將此值傳遞到下一頁,在那裏您可以將值分成數組並處理每個值。

相關問題