473,246 Members | 1,928 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

handling buttons within repeater control

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
Nov 18 '05 #1
2 3165
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" <jo***@gsicomputerservices.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #2
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 whyItemCommand event wouldn't be raised on postback (because it is cleared onrebinding 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" <jo***@gsicomputerservices.com> wrote in messagenews:uO**************@tk2msftngp13.phx.gbl...
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

.

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stan | last post by:
I cannot make the link buttons fire ItemCommand from repeater control. Here is the code: <asp:repeater id=rptLetters runat="server"> <itemtemplate> <asp:linkbutton id="lnkLetter"...
2
by: luca | last post by:
I'm trying to build a Server Control, it's a calendar to manage sellers appointments (don't answer me to use and custumize Calendar Control because unluckily it's not possible for this specific...
4
by: JezB | last post by:
I am dynamically creating WebControls and creating them on my page. Some of these are buttons, and I attach events to these buttons. The problem is these force a post-back, so I must regenerate all...
0
by: mike | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
2
by: Wayne Sepega | last post by:
I have the following Code: foreach (MyClass d in myClassCollection) { curRadioBtn = new RadioButton(); curRadioBtn.Text = d.ToString(); curRadioBtn.GroupName = "Contact"; curRadioBtn.ID =...
9
by: Jaybuffet | last post by:
my aspx has something like this <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <mycontrol:ctl id="ctlId" obj='<%# Container.DataItem %>' showItem="true"/> </ItemTemplate>...
6
by: Totto | last post by:
Hi, Anyone know the best solution to dynamically add buttons to a asp 2.0 page using data from Sql server? Are there any contols suitable for this or is it best to iterate the dataset and...
3
by: Mark | last post by:
Assume you want to dynamically add one to many link button controls to a web page dynamically at run time. Each link button needs to post back and execute code. As the link buttons are created at...
2
by: =?Utf-8?B?Um9nZXIweDE=?= | last post by:
Language: c#,.net 2 This looks simple in concept: I have a page that needs to display a userid, a date/time to start vacation, a date/time to end the vacation, and two buttons: one says...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.