How to use a repeater with two buttons?

G

Guest

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 approve, the other deny. When you press Approve, it
updates the sql table from pending to approved, when you press deny, it
updates the sql table from pending to denied.

The simplest solution looks to be a repeater which will display the three
data fields, and then the two buttons.

That code is below, and works fine -- that is until I click either approve
or deny.

The code-behind page (also shown below) does not fire, and I get a
validation error (also shown below).

I've looked at several items posted about the error message, and I've tried
setting the page directive to turn off validation (the error message goes
away, but the Label doesn't change = no event firing in my mind at least),
and I belive that the causesvalidation = "false" should override that
behavior for the buttons, but apparently that ain't so either, so I am in a
quandry.

Any suggestions or alternative approaches would be greatly appreciated.


<asp:Repeater runat="server" id="Repeater1" >
<HeaderTemplate>
<table cellpadding="5" cellspacing = "2" border="1"><font
size="2" face="Verdana">
<tr bgcolor = "BurlyWood">
<td><b>Name</b></td>
<td><b>From</b></td>
<td><b>To</b></td>
<td><b>&nbsp</b></td>
<td><b>&nbsp</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="AntiqueWhite">
<td><%# DataBinder.Eval(Container.DataItem,
"EmployeeID") %></td>
<td><%# DataBinder.Eval(Container.DataItem,
"StartDate")%></td>
<td><%# DataBinder.Eval(Container.DataItem,
"EndDate")%></td>
<td><asp:Button text="Approve" runat="server"
ID="Approve" CommandName="Approve" CausesValidation="false" /></td>
<td><asp:Button text ="Deny" runat="server" ID="Deny"
CommandName="Deny" CausesValidation="false" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</font>
</table>
</FooterTemplate>
</asp:Repeater>

Code-Behind:

void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label1.Text = e.CommandName.ToString();
}



Error Message:
Invalid postback or callback argument. Event validation is enabled using
<pages enableEventValidation="true"/> in configuration or <%@ Page
EnableEventValidation="true" %> in a page. For security purposes, this
feature verifies that arguments to postback or callback events originate from
the server control that originally rendered them. If the data is valid and
expected, use the ClientScriptManager.RegisterForEventValidation method in
order to register the postback or callback data for validation.
 
S

sloan

I think you need a CommandArgument.

See my code below.

Just substitute my "TitleID" for you "EmployeeID".

I have EditTitle, and DeleteTitle, you can have Accept and Deny.




<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# DataBinder.Eval(Container.DataItem, "TitleId") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</td>

<TD><IMG height="20" src="../images/openitem.gif" width="20"
align="absMiddle" border="0">
<asp:LinkButton id="lnkButtonEdit1" runat="server" CommandName
="EDITTITLE" CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"TitleId") %>'>
<strong>Edit</strong>
</asp:LinkButton>
</TD>
<TD>
<IMG height="20" src="../images/delete_icon.gif" width="20"
align="absMiddle" border="0">
<asp:LinkButton id="lnkButtonDelete1" runat="server" CommandName
="DELETETITLE" CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"TitleId") %>'>
<strong>Delete</strong>
</asp:LinkButton>
</TD>
</tr>
</ItemTemplate>


And then something like this:


private void MyRepeater_ItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if (e.CommandName == "ADDTITLE" || e.CommandName == "EDITTITLE") {
string titleId = e.CommandArgument.ToString;
Response.Redirect("./TitleEdit.aspx?TitleID=" + titleId);
} else if (e.CommandName == "DELETETITLE") {
string titleId = e.CommandArgument.ToString;
//run some delete code
Response.Redirect("./Me.aspx");
}
}
 
G

Guest

Hi Sloan,

Thanks for the quick reply! I wish I could say the problem was solved, but
it isn't.
I have seperated the code out to a fresh web page to simplify (updated code
posted below). I have my breakpoint set on the ItemCommand code line. I see
postback behavior on the web page, but the ItemCommand does not appear to be
firing, as evidenced by no break, and the label text stays the same.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Testapp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" id="MyRepeater"
DataSourceID="SqlDataSource1" >
<HeaderTemplate>
<table cellpadding="5" cellspacing = "2" border="1">
<tr >
<td><b>Name</b></td>
<td><b>From</b></td>
<td><b>To</b></td>
<td><b>Approve</b></td>
<td><b>Deny</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr >
<td><%# DataBinder.Eval(Container.DataItem,
"EmployeeID") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "StartDate")
%></td>
<td><%# DataBinder.Eval (Container.DataItem, "EndDate")
%></td>
<td><asp:LinkButton id="lnkButton1" runat="server"
CommandName = "Approve" CommandArgument='<%#
DataBinder.Eval(Container.DataItem, "PrimaryKey")
%>'><strong>Approve</strong></asp:LinkButton></td>
<td><asp:LinkButton id="LinkButton2" runat="server"
CommandName = "Deny" CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"PrimaryKey") %>'><strong>Deny</strong></asp:LinkButton></td>
</tr>
</ItemTemplate>
<FooterTemplate>

</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TimeDataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [StartDate], [EndDate],
[Status], [PrimaryKey] FROM [LeaveRequest]">
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Not
Set"></asp:Label></div>
</form>
</body>
</html>




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Testapp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

private void MyRepeater_ItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{

Label1.Text = e.CommandName.ToString();
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top