handling buttons within repeater control

Discussion in 'ASP .Net' started by Jorge Ayala, Nov 18, 2003.

  1. Jorge Ayala

    Jorge Ayala Guest

    Well I'm trying to catch and act upon a button event that is placed within
    the item template of a repeater control. Yet the code I'm using isn't
    working. What I've seen out there to explain how to do this is way
    complicated and assumes a familiarity with dot net that I don't have yet.
    I'll get it eventually but this is where I'll get it. Here's my code.
    First part is the user control and second is the code behind page


    Use control

    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="SiteList.ascx.vb" Inherits="GSIComputerServices.SiteList"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:Repeater id="SiteRepeater" runat="server"
    OnItemCommand="SiteRepeater_ItemCommand">
    <ItemTemplate>
    <tr>
    <td>
    <asp:Button Text='<%#
    CType(Databinder.Eval(Container.DataItem,"SiteName"),String).Trim %>'
    Runat="server" Font-Bold=True CommandArgument='<%#
    CType(Databinder.Eval(Container.DataItem,"SiteID"),String).Trim()%>'
    CommandName='SiteIDButton' Width="100px">
    </asp:Button>
    </td>
    </tr>
    </ItemTemplate>
    <HeaderTemplate>
    <table border="0" cellpadding="0" cellspacing="0">
    </HeaderTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    <SeparatorTemplate>
    </SeparatorTemplate>
    </asp:Repeater>

    code behind
    Imports System.Data.SqlClient

    Imports System.Data

    Imports GSIComputerServices





    Public Class SiteList

    Inherits System.Web.UI.UserControl

    Dim useparent As Master

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents SiteRepeater As System.Web.UI.WebControls.Repeater

    Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init

    'CODEGEN: This method call is required by the Web Form Designer

    'Do not modify it using the code editor.

    InitializeComponent()

    End Sub

    #End Region



    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    Dim sql As String

    Dim db As SqlConnection

    Dim comm As SqlCommand

    Dim dread As SqlDataReader

    sql = "Select * From _Site where MasterID = " &
    CType(Session.Contents("MasterID"), Integer) & ""

    db = New SqlConnection("")

    db.Open()

    comm = New SqlCommand(sql, db)

    dread = comm.ExecuteReader

    Me.SiteRepeater.DataSource = dread

    Me.SiteRepeater.DataBind()

    db.Close()

    'Put user code to initialize the page here

    End Sub

    Public Sub ChooseSite(ByVal siteID As String)

    Me.useparent.ChooseSite(siteID)

    End Sub

    Public Property componentParent() As Master

    Get

    Return Me.useparent

    End Get

    Set(ByVal Value As Master)

    Me.useparent = Value

    End Set

    End Property

    Public Sub SiteRepeater_ItemCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles
    SiteRepeater.ItemCommand

    'ChooseSite("all")

    ChooseSite(CType(e.CommandArgument(), String))

    End Sub

    End Class
    Jorge Ayala, Nov 18, 2003
    #1
    1. Advertising

  2. Jorge Ayala

    Teemu Keiski Guest

    Hi,

    you seem to be databinding the grid on every request and that's why
    ItemCommand event wouldn't be raised on postback (because it is cleared on
    rebinding when rebinding happens "too early").

    Put the code that bind the grid in Page_Load inside:

    If Not Page.IsPostBack Then
    'code to bind here
    End If

    and ItemCommand event starts to raise.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "Jorge Ayala" <> wrote in message
    news:...
    > Well I'm trying to catch and act upon a button event that is placed within
    > the item template of a repeater control. Yet the code I'm using isn't
    > working. What I've seen out there to explain how to do this is way
    > complicated and assumes a familiarity with dot net that I don't have yet.
    > I'll get it eventually but this is where I'll get it. Here's my code.
    > First part is the user control and second is the code behind page
    >
    >
    > Use control
    >
    > <%@ Control Language="vb" AutoEventWireup="false"
    > Codebehind="SiteList.ascx.vb" Inherits="GSIComputerServices.SiteList"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    > <asp:Repeater id="SiteRepeater" runat="server"
    > OnItemCommand="SiteRepeater_ItemCommand">
    > <ItemTemplate>
    > <tr>
    > <td>
    > <asp:Button Text='<%#
    > CType(Databinder.Eval(Container.DataItem,"SiteName"),String).Trim %>'
    > Runat="server" Font-Bold=True CommandArgument='<%#
    > CType(Databinder.Eval(Container.DataItem,"SiteID"),String).Trim()%>'
    > CommandName='SiteIDButton' Width="100px">
    > </asp:Button>
    > </td>
    > </tr>
    > </ItemTemplate>
    > <HeaderTemplate>
    > <table border="0" cellpadding="0" cellspacing="0">
    > </HeaderTemplate>
    > <FooterTemplate>
    > </table>
    > </FooterTemplate>
    > <SeparatorTemplate>
    > </SeparatorTemplate>
    > </asp:Repeater>
    >
    > code behind
    > Imports System.Data.SqlClient
    >
    > Imports System.Data
    >
    > Imports GSIComputerServices
    >
    >
    >
    >
    >
    > Public Class SiteList
    >
    > Inherits System.Web.UI.UserControl
    >
    > Dim useparent As Master
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    >
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub

    InitializeComponent()
    >
    > End Sub
    >
    > Protected WithEvents SiteRepeater As System.Web.UI.WebControls.Repeater
    >
    > Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
    >
    > 'NOTE: The following placeholder declaration is required by the Web Form
    > Designer.
    >
    > 'Do not delete or move it.
    >
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    >
    > 'CODEGEN: This method call is required by the Web Form Designer
    >
    > 'Do not modify it using the code editor.
    >
    > InitializeComponent()
    >
    > End Sub
    >
    > #End Region
    >
    >
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > Dim sql As String
    >
    > Dim db As SqlConnection
    >
    > Dim comm As SqlCommand
    >
    > Dim dread As SqlDataReader
    >
    > sql = "Select * From _Site where MasterID = " &
    > CType(Session.Contents("MasterID"), Integer) & ""
    >
    > db = New SqlConnection("")
    >
    > db.Open()
    >
    > comm = New SqlCommand(sql, db)
    >
    > dread = comm.ExecuteReader
    >
    > Me.SiteRepeater.DataSource = dread
    >
    > Me.SiteRepeater.DataBind()
    >
    > db.Close()
    >
    > 'Put user code to initialize the page here
    >
    > End Sub
    >
    > Public Sub ChooseSite(ByVal siteID As String)
    >
    > Me.useparent.ChooseSite(siteID)
    >
    > End Sub
    >
    > Public Property componentParent() As Master
    >
    > Get
    >
    > Return Me.useparent
    >
    > End Get
    >
    > Set(ByVal Value As Master)
    >
    > Me.useparent = Value
    >
    > End Set
    >
    > End Property
    >
    > Public Sub SiteRepeater_ItemCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles
    > SiteRepeater.ItemCommand
    >
    > 'ChooseSite("all")
    >
    > ChooseSite(CType(e.CommandArgument(), String))
    >
    > End Sub
    >
    > End Class
    >
    >
    Teemu Keiski, Nov 18, 2003
    #2
    1. Advertising

  3. Actually I databind() repeaters on each Page_Load, I never
    check for IsPostBack and it works just fine... Infact if
    you have an OnItemDataBound event that dynamically adds
    buttons into your Repeater Items you HAVE TO databind() on
    each Page_Load or the dynamically created button will
    disappear at post back, so you have to recreate it at each
    page load so the ItemCommand triggers...

    the problem here which i noticed also is actually a bug.
    Button ItemCommand events are NOT bubbled to their parent
    Repeater. LinkButtons are. It is quite frustrating because
    in help it clearly states Buttons are supposed to work
    this way and they don't, they never fire the ItemCommand
    event.. LinkButtons do... Maybe it has something to do
    with Buttons generating input type=submit and LinkButtons
    generate javascript:do_submit or something like that..

    anyway, something is definitely wrong here... try it out..




    >-----Original Message-----
    >Hi,
    >
    >you seem to be databinding the grid on every request and

    that's why
    >ItemCommand event wouldn't be raised on postback (because

    it is cleared on
    >rebinding when rebinding happens "too early").
    >
    >Put the code that bind the grid in Page_Load inside:
    >
    >If Not Page.IsPostBack Then
    >'code to bind here
    >End If
    >
    >and ItemCommand event starts to raise.
    >
    >--
    >Teemu Keiski
    >MCP, Microsoft MVP (ASP.NET), AspInsiders member
    >ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >
    >"Jorge Ayala" <> wrote in

    message
    >news:...
    >> Well I'm trying to catch and act upon a button event

    that is placed within
    >> the item template of a repeater control. Yet the code

    I'm using isn't
    >> working. What I've seen out there to explain how to do

    this is way
    >> complicated and assumes a familiarity with dot net

    that I don't have yet.
    >> I'll get it eventually but this is where I'll get it.

    Here's my code.
    >> First part is the user control and second is the code

    behind page
    >>
    >>
    >> Use control
    >>
    >> <%@ Control Language="vb" AutoEventWireup="false"
    >> Codebehind="SiteList.ascx.vb"

    Inherits="GSIComputerServices.SiteList"
    >>

    TargetSchema="http://schemas.microsoft.com/intellisense/ie5
    " %>
    >> <asp:Repeater id="SiteRepeater" runat="server"
    >> OnItemCommand="SiteRepeater_ItemCommand">
    >> <ItemTemplate>
    >> <tr>
    >> <td>
    >> <asp:Button Text='<%#
    >> CType(Databinder.Eval

    (Container.DataItem,"SiteName"),String).Trim %>'
    >> Runat="server" Font-Bold=True CommandArgument='<%#
    >> CType(Databinder.Eval

    (Container.DataItem,"SiteID"),String).Trim()%>'
    >> CommandName='SiteIDButton' Width="100px">
    >> </asp:Button>
    >> </td>
    >> </tr>
    >> </ItemTemplate>
    >> <HeaderTemplate>
    >> <table border="0" cellpadding="0" cellspacing="0">
    >> </HeaderTemplate>
    >> <FooterTemplate>
    >> </table>
    >> </FooterTemplate>
    >> <SeparatorTemplate>
    >> </SeparatorTemplate>
    >> </asp:Repeater>
    >>
    >> code behind
    >> Imports System.Data.SqlClient
    >>
    >> Imports System.Data
    >>
    >> Imports GSIComputerServices
    >>
    >>
    >>
    >>
    >>
    >> Public Class SiteList
    >>
    >> Inherits System.Web.UI.UserControl
    >>
    >> Dim useparent As Master
    >>
    >> #Region " Web Form Designer Generated Code "
    >>
    >> 'This call is required by the Web Form Designer.
    >>
    >> <System.Diagnostics.DebuggerStepThrough()> Private Sub

    >InitializeComponent()
    >>
    >> End Sub
    >>
    >> Protected WithEvents SiteRepeater As

    System.Web.UI.WebControls.Repeater
    >>
    >> Protected WithEvents LinkButton1 As

    System.Web.UI.WebControls.LinkButton
    >>
    >> 'NOTE: The following placeholder declaration is

    required by the Web Form
    >> Designer.
    >>
    >> 'Do not delete or move it.
    >>
    >> Private designerPlaceholderDeclaration As System.Object
    >>
    >> Private Sub Page_Init(ByVal sender As System.Object,

    ByVal e As
    >> System.EventArgs) Handles MyBase.Init
    >>
    >> 'CODEGEN: This method call is required by the Web Form

    Designer
    >>
    >> 'Do not modify it using the code editor.
    >>
    >> InitializeComponent()
    >>
    >> End Sub
    >>
    >> #End Region
    >>
    >>
    >>
    >> Private Sub Page_Load(ByVal sender As System.Object,

    ByVal e As
    >> System.EventArgs) Handles MyBase.Load
    >>
    >> Dim sql As String
    >>
    >> Dim db As SqlConnection
    >>
    >> Dim comm As SqlCommand
    >>
    >> Dim dread As SqlDataReader
    >>
    >> sql = "Select * From _Site where MasterID = " &
    >> CType(Session.Contents("MasterID"), Integer) & ""
    >>
    >> db = New SqlConnection("")
    >>
    >> db.Open()
    >>
    >> comm = New SqlCommand(sql, db)
    >>
    >> dread = comm.ExecuteReader
    >>
    >> Me.SiteRepeater.DataSource = dread
    >>
    >> Me.SiteRepeater.DataBind()
    >>
    >> db.Close()
    >>
    >> 'Put user code to initialize the page here
    >>
    >> End Sub
    >>
    >> Public Sub ChooseSite(ByVal siteID As String)
    >>
    >> Me.useparent.ChooseSite(siteID)
    >>
    >> End Sub
    >>
    >> Public Property componentParent() As Master
    >>
    >> Get
    >>
    >> Return Me.useparent
    >>
    >> End Get
    >>
    >> Set(ByVal Value As Master)
    >>
    >> Me.useparent = Value
    >>
    >> End Set
    >>
    >> End Property
    >>
    >> Public Sub SiteRepeater_ItemCommand(ByVal source As

    Object, ByVal e As
    >> System.Web.UI.WebControls.RepeaterCommandEventArgs)

    Handles
    >> SiteRepeater.ItemCommand
    >>
    >> 'ChooseSite("all")
    >>
    >> ChooseSite(CType(e.CommandArgument(), String))
    >>
    >> End Sub
    >>
    >> End Class
    >>
    >>

    >
    >
    >.
    >
    Adrijan Josic, Nov 19, 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. Stan
    Replies:
    2
    Views:
    11,335
    Shawn Mehaffie
    Jan 22, 2004
  2. Jeroen Klooster

    Re: Radio Buttons in a Repeater Control

    Jeroen Klooster, Apr 7, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    2,916
    Jeroen Klooster
    Apr 13, 2004
  3. Darren Smith
    Replies:
    5
    Views:
    5,508
    dsmith
    May 10, 2004
  4. Brian Miller
    Replies:
    6
    Views:
    671
    Brian Miller
    Nov 18, 2004
  5. Jaime Stuardo

    How to access a server control within a repeater control

    Jaime Stuardo, Aug 19, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    161
    Jaime Stuardo
    Aug 19, 2006
Loading...

Share This Page