I'm dynamically adding a link button in the footer of a grid view. The grid view is wrapped in an update panel. I can get an async post back (I can tell by seeing an update progress flash), but I can't get the debug point in my click function to fire.
Private Sub gvParts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
End If
End If
Private Sub clearButton_click(ByVal sender As Object, ByVal e As System.EventArgs)
ClearCart()
End Sub
try this
<dl>
Private Sub gvParts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(clearbutton)
End If
End If
Sorry,It's my mistake I have posted wrong code.Place the above code on OnRowCreated event of gridview
try this
Private Sub gvParts_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(clearbutton)
End If
End If
The controls have to be added to your Controls collection prior to the page_load event. The default databinding (which fires the OnRowCreated, OnRowDataBound events) happens during the OnLoad event. Try moving your databinding code to the Page_Init function. Depending on what your databinding code looks like, this might mean you will have to implement the databinding "manually" (ie. Set the datasource and call .DataBind() in code)
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'm creating a code in Page_PreRender function to dynamically creates some labels and buttons:
Dim btnExcludeDr As New Button()
btnExcludeDr.ID = "btnExcludeDr"
btnExcludeDr.Text = "Rate Driver"
form1.Controls.Add(btnExcludeDr)
AddHandler btnExcludeDr.Click, AddressOf Me.cmdExcludeDrv_Click
And the event which must be fired for each btnExcludeDr button is:
Protected Sub cmdExcludeDrv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("hello")
End Sub
But the event is not fired. Do you have any solution? thank you !
The best place to create your dynamic controls is in the Page_Init function that the page code-behind class provides.
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim btnExcludeDr As New Button()
btnExcludeDr.ID = "btnExcludeDr"
btnExcludeDr.Text = "Rate Driver"
form1.Controls.Add(btnExcludeDr)
AddHandler btnExcludeDr.Click, AddressOf Me.cmdExcludeDrv_Click
End Sub
Protected Sub cmdExcludeDrv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("hello")
End Sub
I have created few buttons and dropdowns dynamically. Code compiles fine but My event handlers are not firing? How should I handle this situation?
Protected Sub CreateAndLoadDropdowns()
Dim ddlBureauDropdowns As New DropDownList
Dim btnGo As New Button
With btnGo
.Text = "Go"
.ID = tempList2(0).MenuID
End With
AddHandler btnGo.Click, AddressOf Me.btnGo_Click
AddHandler ddlBureauDropdowns.SelectedIndexChanged, AddressOf Me.ddlBureauDropdowns_SelectedIndexChanged
phAddDropdnsHere.Controls.Add(ddlBureauDropdowns)
phAddDropdnsHere.Controls.Add(btnGo)
Next
End Sub
Protected Sub btnGo_Click(sender As Object, e As EventArgs)
End Sub
Protected Sub ddlBureauDropdowns_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
I have moved the code to Page_init and added autopostback = true for the dropdown. The Event handlers are firing now.
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
It have a ListView with LayoutTemplate, ItemTemplate and EditTemplate. The List view only ever shows one item.
The ItemTemplate as an EditButton with CommandName = 'Edit' which when clicked triggers...
Protected Sub ListView1_ItemEditing(ByVal sender As Object, ByVal e As ListViewEditEventArgs) Handles ListView1.ItemEditing
ListView1.EditIndex = e.NewEditIndex
'Create SQL and load result in datatable and bind to listview
LoadData(Session("SID"))
End Sub
The EditTemplate has an UpdateButton with CommandName 'Update' which when clicked triggers...
Protected Sub ListView1_Command(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles ListView1.ItemCommand
If e.CommandName = "Update" Then
'Use StringBuilder to build up an UPDATE TSql script
SqlStr = sb.ToString
'Execute Update
ExecuteSQLScript(SqlStr)
End If
The above works great. My question is how do I now get back to ItemTemplate View. I know that I must use the ItemUpdating method similar to the way the above ItemEditing method worked, but I've ran out of ideas... Any help appreciated. All I have at the moment is...
Protected Sub ListView1_Updating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs) Handles ListView1.ItemUpdating
'ListView1.ItemIndex = e.ItemIndex 'This does not work
LoadData(Session("SID"))
End Sub
Thanks
Set the EditIndex to -1.
ListView1.EditIndex = -1