动态创建的单选按钮列表 [英] dynamically created radiobuttonlist

查看:205
本文介绍了动态创建的单选按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个母版页。内容页面有包含请求变量的超链接列表。你点击一个链接转到页包含在RadioButtonList(也许)。

Have a master page. The content page has a list with hyperlinks containing request variables. You click on one of the links to go to the page containing the radiobuttonlist (maybe).

第一个问题:当我到了新的一页,我用的变量之一,以确定是否一个单选按钮列表添加到页面上的占位符。我试图做到这一点在页)_load但无法获得选择的值。当我打得周围preINIT这样做,第一时间的页面是存在的,我不能让页面的控制。 (未将对象引用设置到对象的实例。)我认为这是与母版和页面内容?该控件不实例化,直到后来? (用vb的方式)

First problem: When I get to the new page, I use one of the variables to determine whether to add a radiobuttonlist to a placeholder on the page. I tried to do it in page)_load but then couldn't get the values selected. When I played around doing it in preInit, the first time the page is there, I can't get to the page's controls. (Object reference not set to an instance of an object.) I think it has something to do with the MasterPage and page content? The controls aren't instantiated until later? (using vb by the way)

问题二:说我得到工作,有一次我打了一个按钮,我仍然可以访问所传递的请求变量来确定单选​​按钮列表选择的项目

Second problem: Say I get that to work, once I hit a button, can I still access the passed request variable to determine the selected item in the 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

很抱歉说得这么有需要的 - 但也许有人可以直接我举例的网页?也许看到它会帮助它有意义吗?

Sorry to be so needy - but maybe someone could direct me to a page with examples? Maybe seeing it would help it make sense?

感谢.... JB

推荐答案

如果你有一个内容占位符,可能你只需要添加的单选按钮列表控件到那里?

If you have a content placeholder, could you just add the radio button list control to there?

在母版页:

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

一些链接,接下来的页面上使用含有请求变量。

Some links, containing request variables used on the next page.

<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>

现在的单选按钮,列表页上,将其添加到内容占位符。

Now on the page with the radio button list, add it into the content placeholder.

<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:
code的基础上传递的信息动态填充您的单选按钮列表。

RadioButtonList.aspx: Code to dynamically populate your radio button list based on the information passed in.

 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

希望你会发现一些有用的在这里了。

Hopefully you'll find something helpful in here.

这篇关于动态创建的单选按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆