0

I have a Repeater displaying rows from an SQL Server database.

I have an Edit and Delete button appearing in each row, but I can't successfully write code behind these buttons.

Here is the code to display the repeater:

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                        <ItemTemplate>
                            <div>
                                <table>                                    
                                    <tr><th><%#Eval("Event_Title")%></th><td><asp:Button ID="LinkButton1" runat="server" CommandName="Edit" Text="Edittttt"></asp:Button></td><td><button>Delete</button></td></tr>
                                    <tr><td>Event Group ID</td><td><%#Eval("Event_Group_Id") %></td></tr>
                                    <tr><td>Event Type</td><td><%#Eval("Event_Type")  %></td></tr>
                                    <tr><td>Event ID</td><td><%#Eval("Event_Id")  %></td></tr>
                                    <br />
                                </table>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>

And here is the UI.

I have tried to use the below code to see if the Edit button is working:

  protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Edit":
            Response.Write("Edit button clicked");
            break;

        default: break;
    }
}

But I am getting this error message:

Invalid postback or callback argument. Event validation is enabled using 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.

Below is my Page_Load code: I understand that a lot of it is duplicated, but I will tidy this up in time. Can anyone advise me what I need to do?

        //Trying to display events using repeater
    SqlConnection connR;
    string connectionStringR = ConfigurationManager.ConnectionStrings[
        "BallinoraDBConnectionString1"].ConnectionString;
    connR = new SqlConnection(connectionStringR);
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Events", connR);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    Repeater1.DataSource = dt;
    Repeater1.DataBind();


    Response.Write("Hello");
    SqlConnection conn;
    SqlCommand comm;
    string connectionString = ConfigurationManager.ConnectionStrings[
        "BallinoraDBConnectionString1"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand(                                  //where Event_Group = 'Upcoming
        "SELECT Event_Id, Event_Group_Id, Event_Type, Event_Title FROM Events WHERE Event_Group_Id = 1",
        conn);
    conn.Open();
    SqlDataReader reader = comm.ExecuteReader();

        if (lblGroupOneEvents.Text == "")
        {
             while (reader.Read())
            {

                lblGroupOneEvents.Text += "<div> <table> <tr><td>" + reader["Event_Title"] + "</td>" + "</tr><tr><td><button>Delete</button></td></tr></table> </div>";
             }
            // lblTest.Text += reader["Event_Id"] +"<br />";
        }
        else
        {
           ;
        }

    reader.Close();
    conn.Close();


    SqlConnection conn1;
    SqlCommand comm1;
    string connectionString1 = ConfigurationManager.ConnectionStrings[
        "BallinoraDBConnectionString1"].ConnectionString;
    conn1 = new SqlConnection(connectionString);
    comm1 = new SqlCommand(                                  //where Event_Type = 'Upcoming
        "SELECT Event_Id, Event_Group_Id, Event_Type, Event_Title FROM Events WHERE Event_Group_Id = 2",
        conn);
    conn.Open();
    SqlDataReader reader1 = comm1.ExecuteReader();
    while (reader1.Read())
    {

        lblGroupTwoEvents.Text += reader1["Event_Title"] + "<br />";
       // lblGroupTwoEventsType.Text += reader1["Event_Id"] + "<br />";

    }
    reader.Close();
    conn.Close();
user2911539
  • 87
  • 2
  • 10
  • Possible duplicate of [Invalid postback or callback argument. Event validation is enabled using ''](http://stackoverflow.com/questions/228969/invalid-postback-or-callback-argument-event-validation-is-enabled-using-page) – Rick S Nov 16 '16 at 21:06
  • @RickS Hi, can you explain how that solves my problem? I'm not saying it doesn't but I don't think I understand it – user2911539 Nov 16 '16 at 21:24
  • @RickS I have added my Page_Load code above, I have looked at the suggested duplication you have mentioned and noticed that the second highest answer uses a if (!Page.IsPostBack) { //do something }. I have tried to add this in my own Page_Load but I'm not sure what code I should include inside it, and which code I should keep outside. Could you advise me on how to move forward please? – user2911539 Nov 16 '16 at 21:57

0 Answers0