PrateekSaluja
PrateekSaluja

Reputation: 14926

How to use button in repeater control?

I am using asp.net 3.5 with c#.I want to invoke button click event inside repeater control.

<asp:Repeater ID="rptFriendsList"
    runat="server" 
    onitemcommand="rptFriendsList_ItemCommand">
    <ItemTemplate> 
        <asp:ImageButton ID="btnSave"
                         runat="server" 
                         ImageUrl="~/Contents/Images/save_button.png"
                         CommandName="Schedule"
                         UseSubmitBehavior="False"  />
    </ItemTemplate>
</asp:Repeater>

but when i click to a button its giving an error

"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."

my purpose is to execute some code in button click which is placed inside the repeater.Please help me to solve this issue.Thanks in advance.

Upvotes: 11

Views: 25717

Answers (7)

Meysam Chegini
Meysam Chegini

Reputation: 999

<asp:Repeater ID="Repeater1" runat="server"      OnItemCommand="Repeater1_OnItemCommand"  DataSourceID="SqlDataSource1">


            <ItemTemplate>
                key1:
                <asp:Label ID="key1Label" runat="server" Text='<%# Eval("key1") %>'></asp:Label><br />
                key2:
                <asp:Label ID="key2Label" runat="server" Text='<%# Eval("key2") %>'></asp:Label><br />
                key3:
                <asp:Label ID="key3Label" runat="server" Text='<%# Eval("key3") %>'></asp:Label><br />
                <asp:TextBox ID="col1" runat="server" Text='<%# Eval("col1") %>'></asp:TextBox>
                <asp:TextBox ID="col2" runat="server" Text='<%# Eval("col2") %>'></asp:TextBox>


                <br />


                <asp:linkbutton ID="Linkbutton1" commandname="Update" runat="server" text="Update"  CommandArgument='<%# Eval("key1") +"|"+Eval("key2")+"|"+ Eval("key3") %>' />
               <asp:linkbutton ID="Linkbutton2" commandname="Cancel" runat="server" text="Cancel" />


            </ItemTemplate>



 protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {

            string col1 = ((TextBox)e.Item.FindControl("col1")).Text;
            string col2 = ((TextBox)e.Item.FindControl("col2")).Text;


            string allKeys = Convert.ToString(e.CommandArgument);

            string[] arrKeys = new string[2];
            char[] splitter = { '|' };
            arrKeys = allKeys.Split(splitter);



            SqlDataSource1.UpdateParameters["col1"].DefaultValue = col1;
            SqlDataSource1.UpdateParameters["col2"].DefaultValue = col2;

            SqlDataSource1.UpdateParameters["key1"].DefaultValue = arrKeys[0];
            SqlDataSource1.UpdateParameters["key2"].DefaultValue = arrKeys[1];
            SqlDataSource1.UpdateParameters["key3"].DefaultValue = arrKeys[2];


            SqlDataSource1.Update();          

            Repeater1.DataBind();

        }
    }

Upvotes: 5

mRizvandi
mRizvandi

Reputation: 1093

You can't use button, because button create postback on click and also itemcommand of repeater called!

But, If you want to use asp:button instead of asp:linkbutton, you have to set UseSubmitBehavior property of button to false. its means, button dont make postback.

<asp:Button ID="btnAccept" runat="server" Text="Accept All" CssClass="vb-default vb-green vb-txt-light" CommandName="Accept" CommandArgument='<%# Eval("UserID") %>' UseSubmitBehavior="false" />

Upvotes: 1

Somnath
Somnath

Reputation: 249

I have used this bellow code and runs ok use this bellow code in .aspx page

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
    <HeaderTemplate>
        <table>
            <tr>
                <th>
                    Edit
                </th>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table>
            <tr>
                <td align="center">
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

Use this in .cs Make the event Repeater1_ItemCommand

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{

    switch (e.CommandName)
    {
        case "Edit":
            // Do some stuff when the Edit button is clicked.

            break;

        // Other commands here.

        default:
            break;
    }

}

Upvotes: 1

David
David

Reputation: 657

This also happens when you have assigned the datasource and data bound your repeater in the OnLoad event rather than OnInit

Upvotes: 2

Nitesh
Nitesh

Reputation: 37

Set page EnableEventValidation="false".

Upvotes: -1

Robert
Robert

Reputation: 3302

if you're adding items server side, try to assign unique ID to each ImageButton

Upvotes: -2

ankur
ankur

Reputation: 4733

UseSubmitBehavior="False" this property you have used is not present with the image button have you over ridden imagebutton class and added this property.

Upvotes: 5

Related Questions