I want to create a dynamic Website. I created a RadioButton List with four radio buttons. I also created a loop where I created 11 textboxes and then I made an "If" function. I did the same loop with 6 other textboxes. My goal is to display the 11 textboxes when I click on one radio button and the 6 other textboxes when I click on an other radio button. But instead, everything appears when I click on each radio button. My question is : what should I do when I click on one radio button the 11 textboxes appears, and when I click on an other radio button the 6 other textboxes appears ?
The code below:
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
The if condition to check the selected value of your round button doesnt affect the creation of the textboxes, all you're doing with the roundbuttons its modifying the behaviour and propoerties of the textboxes. This is what you have
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
As you see, you're not conditioning the creation to anything, so you need to place your for loop inside your if statement.
For example
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
Related
I have in my Page_Load event the following code:
For number As Integer = 1 To 5
Dim btn As Button = New Button With {
.Text = "Test" & number.ToString(),
.ID = "Test" & number.ToString()
}
AddHandler btn.Click, AddressOf btn_Click
buttons.Controls.Add(btn)
Next
And the event handler right below the Page_Load:
Private Sub btn_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Debug.WriteLine($"Button {btn.Text} was pressed")
End Sub
I am not doing something right with the AddHandler because it doesn't fire when I click on any of the five buttons. Any help will be greatly appreciated.
i have slightly edited your code as i am working in VB.NET but this works for me i had to change the position of each button according to the integer as VB.NET lays each button in the top left corner of the form. let me know how you get on
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For number As Integer = 1 To 5
Dim btn As Button = New Button With {
.Text = "Test" & number.ToString(),
.Name = "Test" & number.ToString(),
.Top = number.ToString * 20
}
AddHandler btn.Click, AddressOf btn_Click
Me.Controls.Add(btn)
Next
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Debug.WriteLine($"Button {btn.Text} was pressed")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
End Sub
End Class
I am having a problem with validation. I created a dynamic textboxes with corresponding dynamic numeric validator but it's not firing....
Here is my code
code behind on_load
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
For i = 0 To AssetByDimension(oVal).Rows.Count - 1
Dim lt As New Literal()
Dim label As New Label()
Dim count As Integer = pnlDimension.Controls.OfType(Of Label)().ToList().Count
label.Text = "<b>" + AssetByDimension(oVal).Rows(i)("Dimension_Type") + " (" + AssetByDimension(oVal).Rows(i)("Unit_Of_Measure_Code") + "): </b>"
pnlDimension.Controls.Add(label)
Dim textbox As New TextBox()
count = pnlDimension.Controls.OfType(Of TextBox)().ToList().Count
textbox.ID = "txtDimension" & i
pnlDimension.Controls.Add(textbox)
Dim vdr As New RegularExpressionValidator
vdr.ControlToValidate = "txtDimension" & i
vdr.ValidationExpression = "\d+"
vdr.ErrorMessage = "Numbers only"
vdr.BorderColor = Drawing.Color.Red
vdr.ForeColor = Drawing.Color.Red
vdr.SetFocusOnError = True
pnlDimension.Controls.Add(vdr)
lt.Text = "<br /><br />"
pnlDimension.Controls.Add(lt)
Next
End Sub
and here's the button event handler
Protected Sub btnSaveDim_Click(sender As Object, e As EventArgs)
if Page.isvalid then
Response.Redirect("AddAsset.aspx")
else
'do nothing...
end
End Sub
What I have tried:
I tried to use Page.isValid but is not working...
Hoping for your kind
I have text box create like that:
Dim Result1 As New TextBox
Result1.ID = "BOX_Result" & a & "_" & i
I want when i click on that textbox to write "OK" and when i double click in cell to put NOT/OK
Important! The TextBox is created Dynamically if i try Result.Click doesn't work, get ne that error: "Result1.Click display error: "Click is not an event of 'System.Web.UI.WebControls.TextBox' "
I try like that but doesn't work:
AddHandler Result1.Click, AddressOf Me.Result1_Click
Private Sub Result1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Result1.Text = "OK"
End Sub
I want when a person click on that textbox created dynamically but doesn't work click. Thanks for help
You can add these lines to your TextBox definition:
Result1.Attributes.Add("onclick", "this.value = 'OK';")
Result1.Attributes.Add("ondblclick", "this.value = 'NOT/OK';")
In this code, the text "NOT/OK" is displayed when the user double-clicks in the TextBox. In your question, you talk about a double-click "in cell". If that "cell" is not the TextBox, please give some indication of what kind of control it is.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tb As New TextBox 'Create the new TextBox
AddHandler tb.DoubleClick, AddressOf TB_DoubleClick 'Add a handler to the textbox`s DoubleClick event
AddHandler tb.Click, AddressOf TB_Click
'Set any other properties of textbox you want here....
Me.Controls.Add(tb) 'Add the textbox to the forms controls
End Sub
'This is the textbox Click event handler sub
Private Sub TB_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties
Result1.Text = "OK"
End Sub
'This is the textbox DoubleClick event handler sub
Private Sub TB_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties
Result1.Text = "NOT OK"
End Sub
I have create a simple exemple for you :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Result1 As New TextBox
Result1.Text = "BOX_Result"
Dim loc As New Point With {.Y = 117, .X = 111}
Result1.Location = loc
Me.Controls.Add(Result1)
AddHandler Result1.Click, AddressOf Me.Result1_Click
End Sub
Private Sub Result1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txt As TextBox = sender
sender.Text = "OK"
End Sub
End Class
Hope that you want
I have a button which is not created dynamically. When I click on that button the number of lines in a textbox are counted. I store that in the variable called count. Depending on value of count I create buttons in panel.
Up to now it is working properly.
Now the click event of those buttons is not firing.
I have tried to google my question. But everywhere I got the same answer. Create the controls in Page_Init event. But how is it possible? I am getting the value of count from a textfile's lines, and that value of count decides how many button will be created in the panel.
Dim btnAllow As New Button
btnAllow.Text = "Allow"
btnAllow.ID = "btA" & x
AddHandler btnAllow.Click, AddressOf btnAllow_Click
btnAllowList.Add(btnAllow)
tdAllow.Controls.Add(btnAllow)
The code for btnAllow_Click
Protected Sub btnAllow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For x As Integer = 0 To btnAllowList.Count - 1
If sender.ID.SubString(3) = btnAllowList(x).ID.Substring(3) Then
Dim lineCount = IO.File.ReadAllLines(PendingRequestsPath).Length
Dim reader As New System.IO.StreamReader(DocumentViewersPath)
Dim allLines As New List(Of String)
Do While Not reader.EndOfStream
allLines.Add(reader.ReadLine())
Loop
reader.Close()
Dim DocumentViewerAlreadyExists As Boolean = False
For i As Integer = 0 To lineCount - 1
If ReadLine(i, allLines) = lblRequestorsList(x).Text Then
DocumentViewerAlreadyExists = True
Exit For
End If
Next
If DocumentViewerAlreadyExists = False Then
Dim Writer As System.IO.StreamWriter = IO.File.AppendText(DocumentViewersPath)
Writer.WriteLine(lblRequestorsList(x).Text)
End If
Dim line As String = ""
Dim r As IO.StreamReader = IO.File.OpenText(PendingRequestsPath)
Dim strFile As New ArrayList
While r.Peek <> -1 ' Loop through the file
line = r.ReadLine 'Read the next available line
' Check to see if the line contains what you're looking for.
' If not, add it to an ArrayList
If Not line.Contains(lblRequestorsList(x).Text) Then
strFile.Add(line)
End If
r.Close()
' Because we want to replace the content of the file, we first
' need to delete it.
If IO.File.Exists(PendingRequestsPath) Then
IO.File.Delete(PendingRequestsPath)
End If
' Create a StreamWriter object with the same filename
Dim objWriter As New IO.StreamWriter(PendingRequestsPath, True)
' Iterate through the ArrayList
For Each item As ArrayList In strFile
objWriter.WriteLine(item) ' Write the current item in the ArrayList to the file.
Next
objWriter.Flush()
objWriter.Close()
End While
End If
Next
End Sub
This worked for me:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim NumberOfControls As Integer = 10
Session("CreateControls") = NumberOfControls
End Sub
Protected Sub btnAllow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'This will be executed when clicking on the newly created buttons.
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Session("CreateControls") IsNot Nothing Then
For x As Integer = 0 To Convert.ToInt32(Session("CreateControls"))
Dim btnAllow As New Button
btnAllow.Text = "Allow"
btnAllow.ID = "btA" & x
AddHandler btnAllow.Click, AddressOf btnAllow_Click
Panel1.Controls.Add(btnAllow)
Next
End If
End Sub
I'm new to asp.net (after programming for years in classic asp). I'm trying to build a page which adds something to a string.
My code is following:
default.aspx
<body>
<form id="form1" runat="server">
<div>
<p><asp:textbox id="tb" runat="server"></asp:textbox></p>
<asp:Panel ID="tbPanel" runat="server"></asp:Panel>
</div>
</form>
</body>
Code behind:
Partial Class demo_Default
Inherits System.Web.UI.Page
Public Property gesStr As String
Set(value As String)
ViewState("gesStr") = value
End Set
Get
Dim o As Object = ViewState("gesStr")
If o Is Nothing Then
Return ""
Else
Return o
End If
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim anzeigeStr As String = ""
If Page.IsPostBack Then
Else
gesStr = "1;"
End If
tb.Text = gesStr
Dim iButton As New Button
iButton.Text = "add"
iButton.CommandArgument = "1;"
AddHandler iButton.Click, AddressOf add
tbPanel.Controls.Add(iButton)
Me.anzeige()
End Sub
Private Sub add(ByVal sender As Object, ByVal e As EventArgs)
Dim myButton As Button = DirectCast(sender, Button)
Dim addString As String = myButton.CommandArgument
gesStr += addString
End Sub
Private Sub anzeige()
Dim gesArray As Array = Split(gesStr, ";")
For xLauf As Integer = 0 To UBound(gesArray) - 1
Dim anzLabel As New Label
anzLabel.Text = "<p>" & gesArray(xLauf) & "</p>"
tbPanel.Controls.Add(anzLabel)
Next
End Sub
End Class
The problem:
Pressing the button will cause a postBack, but the result of adding won't appear until the button is pressed a second time. The desired result is that the sub displays the correct array within the loop after the first time pressing the button.
Thank you so much for any help!
You need to call the function anzeige() and bind gesStr value to the textbox control each time the button is click. See the code below:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
gesStr = "1;"
End If
tb.Text = gesStr
Dim iButton As New Button
iButton.Text = "add"
iButton.CommandArgument = "1;"
iButton.CommandName = "1;"
AddHandler iButton.Click, AddressOf add
tbPanel.Controls.Add(iButton)
End Sub
Private Sub add(ByVal sender As Object, ByVal e As EventArgs)
Dim myButton As Button = DirectCast(sender, Button)
Dim addString As String = myButton.CommandArgument
gesStr += addString
anzeige()
End Sub
Private Sub anzeige()
Dim gesArray As Array = Split(gesStr, ";")
For xLauf As Integer = 0 To UBound(gesArray) - 1
Dim anzLabel As New Label
anzLabel.Text = "<p>" & gesArray(xLauf) & "</p>"
tbPanel.Controls.Add(anzLabel)
Next
'Bind gesStr value to the textbox control
tb.Text = gesStr
End Sub