Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 480
PrateekSaluja Avatar asked Mar 15 '11 09:03

PrateekSaluja


2 Answers

<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();

        }
    }
like image 136
Meysam Chegini Avatar answered Oct 12 '22 16:10

Meysam Chegini


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;
    }

}
like image 44
Somnath Avatar answered Oct 12 '22 15:10

Somnath