动态控制与单选按钮

时间:2016-06-03 14:08:54

标签: asp.net vb.net

我想创建一个动态网站。我用四个单选按钮创建了一个RadioButton List。我还创建了一个循环,我创建了11个文本框,然后我创建了一个“If”函数。我用其他6个文本框做了同样的循环。我的目标是当我点击一个单选按钮时显示11个文本框,当我点击另一个单选按钮时显示其他6个文本框。但相反,当我点击每个单选按钮时,一切都会出现。 我的问题是: 当我点击一个单选按钮时,我应该怎么做才会出现11个文本框,当我点击另一个单选按钮时,会出现另外6个文本框? < / p>

以下代码:

Public Class WebForm1
Inherits System.Web.UI.Page


Protected MonRadioButton As New System.Web.UI.WebControls.RadioButtonList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    MonRadioButton.RepeatDirection = RepeatDirection.Horizontal
    MonRadioButton.Width = Unit.Pixel(400)
    MonRadioButton.DataSource = Split("Click,OnChanged,Clicked,Changed", ",")
    MonRadioButton.DataBind()
    MonRadioButton.SelectedIndex = 0

    MonRadioButton.AutoPostBack = True

    PlaceHolder1.Controls.Add(MonRadioButton)

    Dim MonTextBox As TextBox

    For i As Integer = 0 To 10

        MonTextBox = New TextBox
        MonTextBox.ID = "TonTextbox" & i
        MonTextBox.Text = MonTextBox.ID

        If MonRadioButton.SelectedValue = "OnChanged" Then

            MonTextBox.AutoPostBack = True

            AddHandler MonTextBox.TextChanged, AddressOf MonTextBox_TextChanged
        End If

        PlaceHolder1.Controls.Add(MonTextBox)
        PlaceHolder1.Controls.Add(New LiteralControl("<br>"))

    Next

    MonTextBox.Dispose()

    If MonRadioButton.SelectedValue = "Click" Then
        Dim LeBouton As New Button
        LeBouton.Text = "valider"

        AddHandler LeBouton.Click, AddressOf LeBouton_Click
        PlaceHolder1.Controls.Add(LeBouton)

    End If
    PlaceHolder1.Controls.Add(New LiteralControl("<br>"))
    PlaceHolder1.Controls.Add(New LiteralControl("<br>"))

    Dim MonTextBox1 As TextBox
    For i As Integer = 0 To 5

        MonTextBox1 = New TextBox
        MonTextBox1.ID = "TxtBox" & i
        MonTextBox1.Text = MonTextBox1.ID

        If MonRadioButton.SelectedValue = "Clicked" Then
            MonTextBox1.AutoPostBack = True
            AddHandler MonTextBox1.TextChanged, AddressOf MonTextBox1_TextChanged
        End If

        PlaceHolder1.Controls.Add(MonTextBox1)
        PlaceHolder1.Controls.Add(New LiteralControl("<br>"))

    Next

    MonTextBox1.Dispose()

    If MonRadioButton.SelectedValue = "Changed" Then

    End If
End Sub

Private Sub MonTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim LeTextBox As New TextBox
    LeTextBox = CType(sender, TextBox)

    Response.Write("Vous venez de modifié : " & LeTextBox.ID & " avec la valeure : " & LeTextBox.Text)

End Sub

Private Sub MonTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim Txtbox As New TextBox
    Txtbox = CType(sender, TextBox)
    Response.Write("Vous venez de modifié : " & Txtbox.ID & " avec la valeure : " & Txtbox.Text())
End Sub

Private Sub LeBouton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim LeTextBox As TextBox
    For i As Integer = 0 To 10
        Try

            LeTextBox = New TextBox
            LeTextBox = CType(Page.FindControl("TonTextbox" & i), TextBox)

            Response.Write("Texbox N°" & i & " : " & LeTextBox.Text & "<br>")
        Catch ex As Exception

        End Try
    Next

End Sub

End Class

1 个答案:

答案 0 :(得分:0)

检查圆形按钮的选定值的if条件不会影响文本框的创建,您只需使用圆形按钮修改文本框的行为和属性即可。这就是你拥有的

If roundbutton=something then
  'Here you change the behaviour of the textbox(postback, text, etc.)
Else  

For i = 0 to 5
   'Here you add the textboxes
Next

如您所见,您并未将创建调整为任何内容,因此您需要将for循环放在if语句中。 例如

    If MonRadioButton.SelectedValue = "Clicked" Then
        MonTextBox1.AutoPostBack = True
        AddHandler MonTextBox1.TextChanged, AddressOf MonTextBox1_TextChanged

        For i As Integer = 0 To 5

          MonTextBox1 = New TextBox
          MonTextBox1.ID = "TxtBox" & i
          MonTextBox1.Text = MonTextBox1.ID

          PlaceHolder1.Controls.Add(MonTextBox1)
          PlaceHolder1.Controls.Add(New LiteralControl("<br>"))
        Next
    End If