【发布时间】:2019-03-29 20:17:55
【问题描述】:
我想创建一个动态网站。我创建了一个带有四个单选按钮的单选按钮列表。我还创建了一个循环,在其中创建了 11 个文本框,然后创建了一个“If”函数。我对其他 6 个文本框进行了相同的循环。我的目标是在单击一个单选按钮时显示 11 个文本框,在单击另一个单选按钮时显示其他 6 个文本框。但是,当我单击每个单选按钮时,一切都会出现。 我的问题是: 当我单击一个单选按钮时会出现 11 个文本框,当我单击另一个单选按钮时会出现其他 6 个文本框时,我该怎么办?
以下代码:
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
【问题讨论】: