动态创建的单选按钮列表

发布于 2024-08-16 22:45:15 字数 1158 浏览 5 评论 0原文

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

第一个问题:当我到达新页面时,我使用其中一个变量来确定是否将单选按钮列表添加到页面上的占位符中。我尝试在 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

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

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?

Thanks....JB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-08-23 22:45:15

如果您有内容占位符,您可以将单选按钮列表控件添加到那里吗?

在主页上:

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

单选按钮列表.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

希望您能在这里找到有用的内容。

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

On Master Page:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文