如何使用ASP.NET获取所选动态单选按钮VB.NET的ID [英] How do I get the ID of the selected dynamic radiobutton VB.NET with ASP.NET

查看:76
本文介绍了如何使用ASP.NET获取所选动态单选按钮VB.NET的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经动态生成了radiobuttons,当我点击所选按钮时,如果文本框(textbox2)中的动态单选按钮,我如何获得id。

下面的代码仅用于生成控件(收音机和文本框),我的意思是btnload工作,但btnselected不起作用(动态控件只是消失,textbox2显示没有。)。





I have dynamically generated radiobuttons, How can I get the id if the dynamic radiobuttons in a textbox (textbox2) when I cick on the selected button.
the code below only works for generating the controls (radio and textboxes), I mean the btnload works but the btnselected does not work (the dynamic controls just vanish and textbox2 dispalys nothing.).


<html>
    <head id="Head1" runat="server">
    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">         
                ​<asp:Panel ID="container" runat="server">
                                    
                </asp:Panel>       
            <div>
               <asp:Button ID="btnload" runat="server" Text="Load" />  
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                no of loops</div>
                <asp:Button ID="btnselected" runat="server" Text="selected" />
                <asp:TextBox ID="TextBox2" runat="server" style="margin-top: 26px"></asp:TextBox>
        </form>
    </body>
</html>







Protected Sub getradioId_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnload.Click

        Dim tbl As New Table
        tbl.Width = 500

        Dim i As Integer = 0

        Do Until i = TextBox1.Text
            Dim tblrow As New TableRow
            tblrow.Width = 80%

            Dim tblcellrad As New TableCell
            tblcellrad.Width = 100

            Dim tblcellname As New TableCell
            tblcellname.Width = 300

            Dim cand As New TextBox
            cand.ID = i

            i += 1
            Dim rad As New RadioButton   'generate controls (radiobuttons and textbox)
            rad.GroupName = "one"
            rad.ID = i
            rad.Text = "rad" & i
            cand.Text = "candidate " & i.ToString

            If i Mod 2 = 1 Then      'set row colour
                tblrow.Style.Add("background-color", "#EEEEEE")
            Else
                tblrow.Style.Add("background-color", "#E0E0E0")
            End If

            tblcellrad.Controls.Add(rad)    'add controls to cells
            tblcellname.Controls.Add(cand)

            tblrow.Controls.Add(tblcellname) 'add cells to rows
            tblrow.Controls.Add(tblcellrad)


            tbl.Controls.Add(tblrow) 'add row to table
            container.Controls.Add(tbl)

        Loop
    End Sub

Protected Sub btnselected_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnselected.Click
        Dim radios = Controls.OfType(Of RadioButton).AsQueryable()

        For Each r As RadioButton In radios
            If r.Checked Then
                TextBox2.Text = r.ID.ToString
            End If
        Next
    End Sub

推荐答案

参考 - 保留ASP.NET应用程序中动态创建的控件的状态 [ ^ ]。

Refer - Retaining State for Dynamically Created Controls in ASP.NET applications[^].
Quote:

在运行时动态地向ASP.NET页面添加控件时,对象引用会在回发时丢失,因为它们在代码隐藏中没有句柄。在回发后访问这些对象时,用户输入的值不可用,并且它们看起来是空的。本文介绍如何使用ViewState重新创建并将回发值重新插入到对象中以访问其值。

When dynamically adding controls to an ASP.NET page in runtime the object references are lost at postback because they have no handle in the codebehind. The values entered by the user is not availible when accessing these objects after postback, and they seem empty. This article describes how to use ViewState to recreate and reinsert the postback values into the objects to access their values.

您必须在每个Post Post中重新创建控件。

You have to recreate the controls in each Post Back.


这篇关于如何使用ASP.NET获取所选动态单选按钮VB.NET的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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