Is it possible to place a button inside a repeater?
I am using the repeater to display user reviews and I would like to place a button inside the repeater to alert the admin to moderate the review. How can I do this?
Just add the button inside the repeater <ItemTemplate> and if you want to know which repeater item the button was pushed on, then check out the CommandName CommandArgument properties for the button, these are used why placing it inside a repeater.
I don't understand why after you help someone, they don't mark your post as the answer. What's up with that???
If you have event validation turned on (it is on by default), you probably want to set UseSubmitBehavior to false so you don't get an exception.
In your code-behind you can handle the ItemCommand event using a switch statement:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "buy": // Do some stuff when the BUY button is clicked. break;
// Other commands here.
default: break; } }
Finally, you must bind your Repeater to a data source with data if you want to see your button. For initial testing, you can just bind to a string array with a single element:
I have the button placed in the repeater, but how do I know which repeater the button has been pressed in?
This is my code:
Protected Sub userReviews_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles userReviews.ItemCommand
If e.CommandName = "report" Then
Dim aConnection As OleDbConnection
Dim aConnectionString, aQuery As String
Dim Moderate As Integer = "1"
aConnectionString = "Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " & Server.MapPath("App_Data/FYPdb.accdb")
aConnection = New OleDbConnection(aConnectionString)
aConnection.Open()
aQuery = "UPDATE Review SET Moderate = '" & Moderate & "'"
Dim aCommand = New OleDbCommand
aCommand = New OleDbCommand(aQuery, aConnection)
aCommand.ExecuteNonQuery()
End If
End Sub
End Class
When the button has been pressed, I would like the "moderate" value in the corresponding row to change from 0 to 1. With this code, it changes all the moderate values of all the rows to 1, which is obviously not what I want. How can I achieve this? :(
Eithne
Member
26 Points
33 Posts
Create a button inside a repeater?
Jun 11, 2012 11:37 PM|LINK
Is it possible to place a button inside a repeater?
I am using the repeater to display user reviews and I would like to place a button inside the repeater to alert the admin to moderate the review. How can I do this?
javedwahid
Participant
1687 Points
471 Posts
Re: Create a button inside a repeater?
Jun 11, 2012 11:48 PM|LINK
Just add the button inside the repeater <ItemTemplate> and if you want to know which repeater item the button was pushed on, then check out the CommandName CommandArgument properties for the button, these are used why placing it inside a repeater.
JMendez24
Member
70 Points
15 Posts
Re: Create a button inside a repeater?
Jun 11, 2012 11:49 PM|LINK
here is an example:
http://www.w3schools.com/aspnet/aspnet_repeater.asp
perper
Contributor
4454 Points
804 Posts
Re: Create a button inside a repeater?
Jun 12, 2012 12:05 AM|LINK
try this
<asp:Repeater id="rpt" runat="server"> <ItemTemplate> <asp:Button runat="server" ID="btn" Text="Test Button" /> </ItemTemplate> </asp:Repeater>hope this helps
mmsuci
Member
43 Points
89 Posts
Re: Create a button inside a repeater?
Jun 12, 2012 12:46 AM|LINK
When the button is inside a Repeater template, you don't want to use an OnClick event, but the ItemCommand event of the Repeater control.
Your .aspx code will look something like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div class="box">
<asp:Button ID="buy" runat="server" Text="BUY" UseSubmitBehavior="False" CommandName="buy" />
</div>
</ItemTemplate>
</asp:Repeater>
If you have event validation turned on (it is on by default), you probably want to set UseSubmitBehavior to false so you don't get an exception.
In your code-behind you can handle the ItemCommand event using a switch statement:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "buy":
// Do some stuff when the BUY button is clicked.
break;
// Other commands here.
default:
break;
}
}
Finally, you must bind your Repeater to a data source with data if you want to see your button. For initial testing, you can just bind to a string array with a single element:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new string[] { string.Empty };
Repeater1.DataBind();
}
Eithne
Member
26 Points
33 Posts
Re: Create a button inside a repeater?
Jun 12, 2012 11:03 AM|LINK
Ok thanks everyone for the help!
I have the button placed in the repeater, but how do I know which repeater the button has been pressed in?
This is my code:
Protected Sub userReviews_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles userReviews.ItemCommand If e.CommandName = "report" Then Dim aConnection As OleDbConnection Dim aConnectionString, aQuery As String Dim Moderate As Integer = "1" aConnectionString = "Provider= Microsoft.ACE.OLEDB.12.0; Data Source = " & Server.MapPath("App_Data/FYPdb.accdb") aConnection = New OleDbConnection(aConnectionString) aConnection.Open() aQuery = "UPDATE Review SET Moderate = '" & Moderate & "'" Dim aCommand = New OleDbCommand aCommand = New OleDbCommand(aQuery, aConnection) aCommand.ExecuteNonQuery() End If End Sub End ClassAnd this:
<asp:Repeater ID="userReviews" Runat="Server"> <HeaderTemplate> <table id="tblreviews" cellpadding=2 cellspacing=2 bordercolor="Black" width="80%" border="0" style="font-family: Arial, Helvetica, sans-serif; font-size: 0.8em;"> </HeaderTemplate> <ItemTemplate> <td> <br /> <table style="border: 3px solid #000000; font-family: Arial, Helvetica, sans-serif" bgcolor="#D6E3C0" align="left" width="100%"> <tr bgcolor="#0F2B1B" height=25 bordercolor="Black" style="border-width: 3px; border-style: solid; color: #F2B1B;"> <td width=75% colspan ="2" style="font-family: 'lucida handwriting'; font-size: 1em; color: Black" bgcolor="#F2B1BD"><%# Container.DataItem("Nickname")%> </td> <td width=25% style="font-family: 'lucida handwriting'; font-size: 1em; color: Black" bgcolor="#F2B1BD"><%# Container.DataItem("DateSubmitted")%> <asp:Button ID="btnReport" runat="server" BackColor="White" Text="X" BorderColor="Red" BorderStyle="Solid" BorderWidth="1px" UseSubmitBehavior="False" CommandName="report"/></td> </tr> <tr> <td colspan ="3" width="100%" align="center"> <table bgcolor="White" width="80%"> <tr> <td> <%# Container.DataItem("ReviewText")%> </td> </tr> </table> </td></tr> </tr> <tr style="left: inherit"> <td align= "left" style="font-size: 0.8em; font-family: arial, Helvetica, sans-serif">Residents treated with respect: <font color="#E1516A"> <%# Container.DataItem("Rating1")%></font></td> <td align= "left" style="font-size: 0.8em; font-family: arial, Helvetica, sans-serif">Food Quality: <font color="#E1516A"><%# Container.DataItem("Rating2")%></font></td> <td align= "left" style="font-size: 0.8em; font-family: arial, Helvetica, sans-serif">Friendly Staff: <font color="#E1516A"><%# Container.DataItem("Rating3")%></font></td> </tr> <tr> <td align= "left" style="font-size: 0.8em; font-family: arial, Helvetica, sans-serif">"Homey" Atmosphere: <font color="#E1516A"><%# Container.DataItem("Rating4")%></font></td> <td align= "left" style="font-size: 0.8em; font-family: arial, Helvetica, sans-serif">Cleanliness: <font color="#E1516A"><%# Container.DataItem("Rating5")%></font></td> <td align= "left" style="font-size: 0.8em; font-family: arial, Helvetica, sans-serif">Activities: <font color="#E1516A"><%# Container.DataItem("Rating6")%></font></td> <br /> </tr> </table> <br /> </td> </ItemTemplate> <SeparatorTemplate> </tr> </SeparatorTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>When the button has been pressed, I would like the "moderate" value in the corresponding row to change from 0 to 1. With this code, it changes all the moderate values of all the rows to 1, which is obviously not what I want. How can I achieve this? :(
Asmita.Singh
Member
110 Points
20 Posts
Re: Create a button inside a repeater?
Jun 12, 2012 01:06 PM|LINK
hi...
as i understand from your problem.. you have a review table with field moderate. In review table you must be having a
primary key to differentiate between all review comments. You just have to save that key to button using CommandArgument.
eg:
<asp:Button ID="btnReport" runat="server" BackColor="White" Text="X" BorderColor="Red"
BorderStyle="Solid" BorderWidth="1px" UseSubmitBehavior="False" CommandName="report" CommandArgument = '<%# Container.DataItem("Yourkey")%>' />
you can access this key in userReviews_ItemCommand method,by e.CommandArgument. use this to update ur table.
Eithne
Member
26 Points
33 Posts
Re: Create a button inside a repeater?
Jun 12, 2012 03:28 PM|LINK
Thank you SO much for that :)
It works perfectly when I use any other value in my table OTHER than the key. They key is an auto generated number so maybe this is the problem?
It is telling me that there is an IndexOutOfRangeException when I use the key as CommandArguement.