【问题标题】:dynamically created radiobuttonlist动态创建的单选按钮列表
【发布时间】:2011-01-01 07:35:05
【问题描述】:

有一个母版页。内容页面有一个包含请求变量的超链接列表。您单击其中一个链接以转到包含单选按钮列表的页面(可能)。

第一个问题:当我进入新页面时,我使用其中一个变量来确定是否将单选按钮列表添加到页面上的占位符。我尝试在 page)_load 中执行此操作,但随后无法选择值。当我在 preInit 中进行操作时,页面第一次出现时,我无法访问页面的控件。 (对象引用未设置为对象的实例。)我认为它与 MasterPage 和页面内容有关?控件直到稍后才实例化? (顺便用vb)

第二个问题:假设我让它工作,一旦我点击一个按钮,我仍然可以访问传递的请求变量以确定单选按钮列表中的选定项目吗?

    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

【问题讨论】:

    标签: asp.net dynamic radiobuttonlist dynamically-generated page-init


    【解决方案1】:

    如果你有一个内容占位符,你能把单选按钮列表控件添加到那里吗?

    在母版页上:

    <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
    

    希望你能在这里找到一些有用的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2013-02-03
      • 2019-10-13
      相关资源
      最近更新 更多