2

有一個母版頁。內容頁面包含一個帶有包含請求變量的超鏈接的列表。您點擊其中一個鏈接轉到包含單選按鈕列表的頁面(也許)。動態創建單選按鈕列表

第一個問題:當我到達新頁面時,我使用其中一個變量來確定是否將一個單選按鈕列表添加到頁面上的佔位符。我試圖在頁面中執行它)_load,但無法獲得所選的值。當我在preInit中進行播放時,第一次訪問該網頁時,我無法訪問該網頁的控件。 (對象引用未設置爲對象的實例。)我認爲它與MasterPage和頁面內容有關?這些控件直到後來才被實例化? (使用vb的方式)

第二個問題:說我得到這個工作,一旦我點擊一個按鈕,我仍然可以訪問傳遞的請求變量來確定radiobuttonlist中選定的項目?

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit 
    'get sessions for concurrent 

    Dim Master As New MasterPage 
    Master = Me.Master 

    Dim myContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder) 
    If Request("str") = "1" Then 
     Dim myList As dsSql = New dsSql() ''''instantiate the function to get dataset 
     Dim ds As New Data.DataSet 

     ds = myList.dsConSessionTimes(Request("eid")) 
     If ds.Tables("conSessionTimes").Rows.Count > 0 Then 
      Dim conY As Integer = 1 

      CType(myContent.FindControl("lblSidCount"), Label).Text = ds.Tables("conSessionTimes").Rows.Count.ToString 

對不起,如此有需要 - 但也許有人可以指示我到一個頁面的例子?也許看到它會幫助它有意義?

感謝.... JB

回答

2

如果你有一個內容佔位符,可能你只需要添加的單選按鈕列表控件到那裏?

在母版頁:

<asp:ContentPlaceHolder id="ContentPlaceHolderForRadioButtonList" runat="server">  
</asp:ContentPlaceHolder> 

一些鏈接,包含下一個頁面上使用請求變量。

<a href="RadioButtonList.aspx?ref=first" >Link 1</a> 
<a href="RadioButtonList.aspx?ref=second" >Link 2</a><br /> 
<a href="RadioButtonList.aspx?ref=third" >Link 3</a><br /> 
<a href="RadioButtonList.aspx?ref=forth" >Link 4</a><br /> 
<a href="RadioButtonList.aspx?ref=fifth" >Link 5</a><br /> 
<a href="RadioButtonList.aspx?ref=sixth" >Link 6</a> 

現在,在具有單選按鈕列表的頁面上,將其添加到內容佔位符中。

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderForRadioButtonList" Runat="Server"> 
<!-- radio button list to be dynamically populated--> 
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
    </asp:RadioButtonList> 
</asp:Content> 

RadioButtonList.aspx: 代碼以基於傳遞的信息動態填充您的單選按鈕列表

Partial Class RadioButtonList 
    Inherits System.Web.UI.Page 
    Private selection As String = "" 

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init 
     selection = IIf(Request.QueryString("ref") IsNot Nothing, Request.QueryString("ref"), "") 
     If selection = "first" Then 
      RadioButtonList1.Items.Add(New ListItem("first", "1")) 
      RadioButtonList1.Items.Add(New ListItem("third", "3")) 
      RadioButtonList1.Items.Add(New ListItem("fifth", "5")) 
     ElseIf selection = "second" Then 
      RadioButtonList1.Items.Add(New ListItem("second", "2")) 
      RadioButtonList1.Items.Add(New ListItem("forth", "4")) 
      RadioButtonList1.Items.Add(New ListItem("sixth", "6")) 
     Else 
      RadioButtonList1.Items.Add(New ListItem("first", "1")) 
      RadioButtonList1.Items.Add(New ListItem("second", "2")) 
      RadioButtonList1.Items.Add(New ListItem("third", "3")) 
      RadioButtonList1.Items.Add(New ListItem("forth", "4")) 
      RadioButtonList1.Items.Add(New ListItem("fifth", "5")) 
      RadioButtonList1.Items.Add(New ListItem("sixth", "6")) 
     End If 

     'set the selected radio button 
     For i As Integer = 0 To RadioButtonList1.Items.Count - 1 
      If RadioButtonList1.Items(i).Text = selection Then 
       RadioButtonList1.Items(i).Selected = True 
       Exit For 
      End If 
     Next 

    End Sub 

End Class 

希望你能找到有用的東西這裏。