ImageButton Event won't fire inside Template column of DataGrid

Discussion in 'ASP .Net Datagrid Control' started by KJ, Aug 7, 2003.

  1. KJ

    KJ Guest

    I have a custom datagrid control created with CreateChildControls()

    My Image Button Event will not fire so I cannot raise it to the webform.

    The ItemClick Event will not fire.

    My code is below:



    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"
    dgItems.AllowSorting = True

    ' 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("MyStock")
    dgItems.DataBind()
    dgItems.EnableViewState = False
    Controls.Add(dgItems)

    End Sub

    End Class

    Public Class DataGridTemplate
    Implements ITemplate
    Dim ib As New ImageButton()
    Dim templateType As ListItemType
    Dim columnName As String

    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.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)
    End Sub


    End Class
    KJ, Aug 7, 2003
    #1
    1. Advertising

  2. KJ

    KJ Guest

    Re: ImageButton Event won't fire inside Template column of DataGrid - Solution!

    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.
    KJ, Aug 8, 2003
    #2
    1. Advertising

  3. KJ

    Stevie_mac Guest

    Re: ImageButton Event won't fire inside Template column of DataGrid - Solution!

    Did you checkout ItemCommand(...)

    Any imagebutton in a template col should raise this event. In the template
    editor, set the image's CommandName property to "Img" then check
    e.CommandName in the DataGrids ItemCommand event.

    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    DataGrid1.ItemCommand
    If e.CommandName = "Img" Then
    Response.Write("Img Clicked")
    End If
    End Sub

    "KJ" <> wrote in message
    news:...
    > 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.
    Stevie_mac, Aug 16, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad
    Replies:
    4
    Views:
    8,307
  2. Replies:
    4
    Views:
    519
  3. Wenceslao Leon

    ImageButton in template column in edit mode don't fire event

    Wenceslao Leon, Sep 13, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    123
    Wenceslao Leon
    Sep 13, 2003
  4. smay

    User control ImageButton won't fire

    smay, Sep 16, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    95
  5. jef
    Replies:
    2
    Views:
    237
Loading...

Share This Page