how to add linkbutton control to table cell dynamically and its event handler .?
Class SurroundingClass
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim tbl As HtmlTable = New HtmlTable()
Dim tr As HtmlTableRow = New HtmlTableRow()
Dim td As HtmlTableCell = New HtmlTableCell()
tr.Cells.Add(td)
tbl.Rows.Add(tr)
Dim lbtn As LinkButton = New LinkButton()
lbtn.Text = "My Link Button"
lbtn.Click += New EventHandler(AddressOf LinkButton1_Click) '<- THIS LINE ERROR
td.Controls.Add(lbtn)
End Sub
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
ERROR message image
Ah, you are using the C# format for attaching the event "+=", in VB use the AddHandler method.
For Example:
AddHandler lbtn.Click, AddressOf LinkButton1_Click
Related
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 added Textbox like
Protected Sub gvReconciliation_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvReconciliation.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtRemarks As New TextBox()
txtRemarks.ID = "txtRemarks"
txtRemarks.Text = TryCast(e.Row.DataItem, DataRowView).Row("Remarks").ToString()
e.Row.Cells(10).Controls.Add(txtRemarks)
End If
End Sub
I've been reading posts and articles and just getting a little confused and consequently burning through time I don't have at the moment
Can someone look at my code and tell me where I've gone wrong?
Partial Class PayerContacts
Inherits System.Web.UI.Page
Dim connStrDRContacts As String = ConfigurationManager.ConnectionStrings("DRContacts_SQL").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
navBuild()
End Sub
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
If IsPostBack Then
LoadContacts(ViewState("objSender"))
End If
End Sub
Private Function navBuild() As Integer
Dim SQLstrDRs As String = "SELECT * FROM DRList"
Dim DbConnDRs As SqlConnection = New SqlConnection(connStrDRContacts)
DbConnDRs.Open()
Dim dtDRsTemp As New DataTable
Dim SQLAdapterDRs As New SqlDataAdapter(SQLstrDRs, DbConnDRs)
SQLAdapterDRs.Fill(dtDRsTemp)
'Loop through each row of the DataView to create HTML table data
Dim NewTableRow As New TableRow
For Each row As DataRow In dtDRsTemp.Rows
'CREATE table with button to display contacts related to client (one to many)
Dim NewTableButton As LinkButton = New LinkButton
NewTableButton.ID = "btnDRName" & NewTableText
NewTableButton.ViewStateMode = UI.ViewStateMode.Enabled
AddHandler NewTableButton.Click, AddressOf LoadContacts
Next
Return 0
End Function
Protected Sub LoadContacts(sender As Object, e As EventArgs)
Dim LoopCount As Integer = 0
Dim SQLstrLoadTable As String = "SELECT * FROM ContactList WHERE DRVendor = '" & sender.Text.ToString & "'"
and so on....
SQLAdapterLoadTable.Fill(dtLoadTableTemp)
Dim NewTableRow As New TableRow
For Each row As DataRow In dtLoadTableTemp.Rows
'CREATE Accordion to display data
NewAccordion.ID = "ContactAccordion" & LoopCount
NewAccordion.Visible = True
blah, blah...
'SET Pane
NewAccordionPane.HeaderContainer.ID = "PaneHeader" & LoopCount
NewAccordionPane.ContentContainer.ID = "PaneContent" & LoopCount
'CREATE button to open ModalPopup to EDIT each record
Dim imgGear As New ImageButton
imgGear.ID = "btnGear" & row!ID.ToString
imgGear.ViewStateMode = UI.ViewStateMode.Enabled
AddHandler imgGear.Click, AddressOf EditRecord
'LOAD Pane
NewAccordionPane.HeaderContainer.Controls.Add(NewHeaderTable)
NewAccordionPane.ContentContainer.Controls.Add(New LiteralControl(NewTableText))
ViewState("objSender") = sender
End Sub
Protected Sub EditRecord(ByVal sender As Object, ByVal e As EventArgs)
'Open ModalPopup to edit record
popup.Show()
pnlAddEdit.Visible = True
End Sub
End Class
The Infinities Loop articles on ViewState and Dynamic Controls should really be read by every Webforms developer: -
http://mocha.mojoskins.com/SharedFiles/Download.aspx?pageid=566&mid=786&fileid=38
http://weblogs.asp.net/infinitiesloop/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_
The examples are in C# but you should be able to figure out what's going on, it's the same base class library after all.
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