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
Related
I am loading all images in a folder on page load by an image button from code behind.
The images are added correctly but I want to hook up an onclick event unto the image button added dynamically.
Below is my code
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
For Each strfilename As String In Directory.GetFiles(Server.MapPath("~/glypics/"))
Dim imgbtn As New ImageButton
Dim fileinfo As New FileInfo(strfilename)
imgbtn.ImageUrl = "~/glypics/" + fileinfo.Name
imgbtn.Width = Unit.Pixel(250)
Panel1.Controls.Add(imgbtn)
imgbtn.Style.Add("padding", "3px")
Next
End Sub
I am not sure what an imagebutton is? But assuming it's anything like a button you just have to add a handler, which can be done a number of ways.
Dim imgbtn As New Button
AddHandler imgbtn.Click, Sub()
'do stuff
End Sub
Or
Dim imgbtn As New Button
AddHandler imgbtn.Click, AddressOf DoClick
Private Sub DoClick(sender As Object, e As EventArgs)
'Do Stuff
End Sub
The latter is more easily removed if needed
RemoveHandler imgbtn.Click, AddressOf DoClick
Add Addhandler imgbtn.MouseClick, AddressOf imgbtn_MouseClick in your function then create the called function.
Sub imgbtn_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
'Image Button clicked...
End Sub
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
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 n number of dynamically generated GridViews in my asp.net page. In all the GridView all the cells are linkbuttons which also dynamically generated. And I want to get selected cells/ linkbuttons index.
How do i do it?
Note- I dont have GridView name as it is dynamically generated in loop.
code as below-
Protected Sub Btncalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btncalculate.Click
Call GetFinal()
End Sub
Public Sub GetFinal()
For count As Integer = 0 To dtValue.Rows.Count - 1
Dim GrdView1 As New GridView()
'DataTable filling Code goes here
GrdView1.DataSource = dt1
AddHandler GrdView1.SelectedIndexChanged, AddressOf GrdView1_SelectedIndexChanged
AddHandler GrdView1.RowDataBound, AddressOf GrdView1_RowDataBound
AddHandler GrdView1.RowCommand, AddressOf GrdView1_RowCommand
GrdView1.DataBind()
end sub
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' All cell to LinkButton code goes here
AddHandler lnkShow.Click, AddressOf LinkButton1_Click
end sub
You create linkButtons
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
LinkButton lnkShow= new LinkButton();
lnkShow.Text = "Show";
lnkShow.CommandArgument = "Show";
lnkShow.CommandArgument = e.Row.Cell(0); ' For instance
End IF
End Sub
And then you handle the GridView1.RowCommand event:
Public Sub GrdView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "show" Then
LinkButton lnkShow= (LinkButton)sender;
argument = e.CommandArgument;
(do something)
End IF
End Sub
I thoink you dont need Link1_Click
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