Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Image Button inside a template column - Handling The Click Event

Reply
Thread Tools

Image Button inside a template column - Handling The Click Event

 
 
KJ
Guest
Posts: n/a
 
      08-07-2003
I have been searching for 2 days on how to handle a imagebutton click
event inside a template column on a datagrid control. Does anyone know
how to do it?

The ItemClick Event does not fire when I click the image button
therefore the event never gets raised to the webform. Please Help

My code is below





Public Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String

Event ItemImageClick(ByVal source As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
ib = New ImageButton()

ib.ImageUrl = Select.gif"
ib.ToolTip = "Select Item"
ib.ImageAlign = ImageAlign.AbsBottom
ib.CommandName = "Select"
AddHandler ib.Click, AddressOf ItemClick
container.Controls.Add(ib)

End Sub

Public Sub ItemClick(ByVal sender As Object, _
ByVal e As
System.Web.UI.ImageClickEventArgs)
RaiseEvent ItemImageClick(Me, e)

End Sub
 
Reply With Quote
 
 
 
 
Severin
Guest
Posts: n/a
 
      08-07-2003
On an ASPX page, only PAGE level controls have events...

When you place any other <asp:> control inside a datagrid, it is no longer a
PAGE level control, it is now a CHILD of the datagrid.

So... You must use the DataGrid event 'ItemCommand' for all events that can
be raised from internal controls.
Using the ImageButton CommandArguement and CommandName to determine which
imagebutton was cliked.

Usually you would put the primaryKey of the datatable in the CommandArgument
property, and then any string for the CommandName property

example below:
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.ItemCommand

If e.CommandName = [property of ImageButton] Then

Dim primaryKey as Integer = CInt(e.CommandArguement)

' Do logic or database command for image clicked event

End If

End Sub

Severin


"KJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have been searching for 2 days on how to handle a imagebutton click
> event inside a template column on a datagrid control. Does anyone know
> how to do it?
>
> The ItemClick Event does not fire when I click the image button
> therefore the event never gets raised to the webform. Please Help
>
> My code is below
>
>
>
>
>
> Public Class DataGridTemplate
> Implements ITemplate
> Dim templateType As ListItemType
> Dim columnName As String
>
> Event ItemImageClick(ByVal source As Object, ByVal e As
> System.Web.UI.ImageClickEventArgs)
>
> Sub New(ByVal type As ListItemType, ByVal ColName As String)
> templateType = type
> columnName = ColName
> End Sub
>
> Sub InstantiateIn(ByVal container As Control) _
> Implements ITemplate.InstantiateIn
> ib = New ImageButton()
>
> ib.ImageUrl = Select.gif"
> ib.ToolTip = "Select Item"
> ib.ImageAlign = ImageAlign.AbsBottom
> ib.CommandName = "Select"
> AddHandler ib.Click, AddressOf ItemClick
> container.Controls.Add(ib)
>
> End Sub
>
> Public Sub ItemClick(ByVal sender As Object, _
> ByVal e As
> System.Web.UI.ImageClickEventArgs)
> RaiseEvent ItemImageClick(Me, e)
>
> End Sub



 
Reply With Quote
 
 
 
 
Kyle Johnson
Guest
Posts: n/a
 
      08-07-2003

My dgItemClick Event that handles dgItems.ItemCommand is not firing.
Does anyone know why?

Below is the code:



Public Class ABC
Inherits System.Web.UI.WebControls.WebControl

Dim dgItems As New DataGrid()
Dim x As New TemplateColumn()
Dim y As DataGridColumn()
Dim s As New BoundColumn()
Dim k As New BoundColumn()
Dim l As New BoundColumn()
Dim e As New BoundColumn()
Dim strsql As String

Protected Overrides Sub CreateChildControls()

dgItems.CellPadding = 2
dgItems.CellSpacing = 0
'dgItems.Width = 550px
' dgItems.BorderWidth = 1
dgItems.BorderColor = ColorTranslator.FromHtml("Black")
dgItems.AutoGenerateColumns = False
dgItems.ForeColor = ColorTranslator.FromHtml("Black")
' dgItems.Font.Size = 8
dgItems.Font.Name = "Arial"

' x.ItemStyle.BackColor = ColorTranslator.FromHtml("#FFF778")
x.ItemStyle.HorizontalAlign = HorizontalAlign.Center
s.HeaderText = "hasccp"
s.DataField = "hasccp"

k.HeaderText = "Number"
k.DataField = "Number"

l.HeaderText = "Type"
l.DataField = "Type"

e.HeaderText = "Name"
e.DataField = "Name"

x.ItemTemplate = New DataGridTemplate(ListItemType.Item,
"Column1")
dgItems.Columns.AddAt(0, x)
dgItems.Columns.AddAt(1, s)
dgItems.Columns.AddAt(2, k)
dgItems.Columns.AddAt(3, l)
dgItems.Columns.AddAt(4, e)
dgItems.DataSource = RcpDS.Tables("Recipes")
dgItems.DataBind()
dgItems.EnableViewState = True
Controls.Add(dgItems)
'Controls.Add(New LiteralControl("</div>"))
AddHandler dgItems.ItemCommand, AddressOf dgItemClick

End Sub
Public Sub dgItemClick(ByVal sender As Object, _
ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs )
If e.CommandName = "Select" Then
End If
End Sub

End Class

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      08-08-2003
Through much searching.


I found the solution. It was not as clearing spelled out either.

One event that does always fire is the datagrid DataBinding event.


1. Inside the databinding event. You have to call EnsureChildControls
method. Ex.

Private Sub datagrid1_DataBinding(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgItems.DataBinding
EnsureChildControls()
End Sub

2. In the Page_Load of your web form this is where you call your
public method to bind the data to your datagrid. In your control
create a method that just creates the dataset, sets it to the
datasource of the grid and then binds it. Ex.


Public Sub BindDataGrid()
Dim sqlconn As New SqlClient.SqlConnection()
sqlconn.ConnectionString = "data source="
sqlconn.Open()

strsql = "select * from google"

Dim sqlCommand As New SqlClient.SqlCommand(strsql, sqlconn)
sqlCommand.CommandType = CommandType.Text

Dim rcpDA As SqlClient.SqlDataAdapter = New
SqlClient.SqlDataAdapter()
rcpDA.SelectCommand = sqlCommand

Dim RcpDS As DataSet = New DataSet()
rcpDA.Fill(RcpDS, "Searches")
datagrid1.DataSource = RcpDS.Tables("Searches")
datagrid1.DataBind()

End Sub


Call this from the Page_Load of the webform.

All your events should fire now. Hope this helps. Sorry I cannot give
a technical explaination for this but maybe someone will reply with
one.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
image button click event fires before click event of button Purvi T ASP .Net 0 10-19-2004 06:19 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Building Controls 1 05-28-2004 06:23 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net 2 05-25-2004 11:45 PM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Datagrid Control 0 05-25-2004 01:45 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Web Controls 0 05-25-2004 01:45 AM



Advertisments