locked
gridview "Invalid postback or callback argument" problem RRS feed

  • Question

  • User836525179 posted

    hi guys

    i'm using gridview control to show user basket in my website, i add delete button in my gridview control,but when i click on delete button it show me "Invalid postback or callback argument." error , i searched this problem in google and find some resolves like add enableEventValidation="false" on top page but just error not displayed and delete button does not worked , i add delete codes in RowCommand i tried to trace my codes but i undrestand row command does not run when i click on delete button

    plz help if you know what is problem

    this is my codes:

                                <asp:GridView ID="grdBasket" CssClass="GridViewStyle" DataKeyNames="id" PageSize="5" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowCommand="grdBasket_RowCommand">
                                <Columns>
                                    <asp:TemplateField>                    
                                        <ItemTemplate>
                                            <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("smallimg") %>' Height="77" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="شرح کالا">                    
                                        <ItemTemplate>
                                            <a href='../../product/<%#Eval("articleID") %>/<%#Eval("postTitle") %>' target="_blank"><asp:Label ID="Label1" style="float:right;direction:rtl;text-align:right" runat="server" Text='<%#Eval("postTitle") %>'></asp:Label></a>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="قیمت کالا">                    
                                        <ItemTemplate>
                                            <asp:Label ID="lblPrice" runat="server" ForeColor="Green" Text='<%#Eval("price").ToString()+ " تومان" %>'></asp:Label> 
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="datetime" HeaderText="تاریخ"/>
                                    <asp:TemplateField>                    
                                        <ItemTemplate>
                                            <asp:ImageButton ID="btnDelete" runat="server" style="cursor:pointer;" CommandName="delRow" CommandArgument='<%#Eval("id") %>' ToolTip="حذف این رکورد" 
                                            OnClientClick="return confirm('آیا مطمعن هستید؟')" ImageUrl="~/ucp_Content/img/Trash.png" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <RowStyle CssClass="RowStyle" />
                                <EmptyDataRowStyle CssClass="EmptyRowStyle" />
                                <PagerStyle CssClass="PagerStyle" />
                                <SelectedRowStyle CssClass="SelectedRowStyle" />
                                <HeaderStyle CssClass="HeaderStyle" />
                                <EditRowStyle CssClass="EditRowStyle" />
                                <AlternatingRowStyle CssClass="AltRowStyle" />
                                </asp:GridView>

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                check_user();
                add_to_basket();
                get_user_basket();
            }
        }
    
        protected void grdBasket_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var md = new MYDBMS();
            var dt = new DataTable();
            var clsp = new CLS_Basket();
    
            long PID = 0;
    
            try
            {
                if (e.CommandName == "delRow")
                {
                    PID = Convert.ToInt64(e.CommandArgument);
                    clsp.RecID = PID;
                    md.ExecuteSQL(clsp.CartPage_Delete_Basket());
                    grdBasket.DataBind();
                    ScriptManager.RegisterStartupScript(this, GetType(), "Alert", "alert('کالای مورد نظر از سبد خرید حذف شد.');", true);
                }
            }
            catch
            {
    
            }
        }

    Friday, February 2, 2018 7:34 PM

All replies

  • User2103319870 posted

    but just error not displayed and delete button does not worked , i add delete codes in RowCommand i tried to trace my codes but i undrestand row command does not run when i click on delete button

    Instead of using rowcommand to attach the click event of button, IMHO a better option to use click event of button directly like below

      <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="btnDelete" runat="server" Style="cursor: pointer;" CommandName="delRow" CommandArgument='<%#Eval("ID") %>' ToolTip="حذف این رکورد"
                                OnClientClick="return confirm('آیا مطمعن هستید؟')" ImageUrl="~/ucp_Content/img/Trash.png" OnClick="btnDelete_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>

    Now in button click you can access the ID like below

      protected void btnDelete_Click(object sender, ImageClickEventArgs e)
            {
                //Get the image button
                ImageButton imgbtn = sender as ImageButton;
                long PID = 0;
                try
                {
                    //Access the command argument from ImageButton
                    PID = Convert.ToInt64(imgbtn.CommandArgument);
                    clsp.RecID = PID;
                    md.ExecuteSQL(clsp.CartPage_Delete_Basket());
                    //You might need to fetch the data from database and rebind the grid again
                    grdBasket.DataBind();
                    ScriptManager.RegisterStartupScript(this, GetType(), "Alert", "alert('کالای مورد نظر از سبد خرید حذف شد.');", true);
                }
                catch
                {
                    throw;
                }
            }

    Friday, February 2, 2018 7:46 PM
  • User836525179 posted

    ty problem resolved but when i two times click on delete button, it work

    first time click did not work

    Friday, February 2, 2018 7:59 PM
  • User1400794712 posted

    Hi uniservice3,

    ty problem resolved but when i two times click on delete button, it work

    first time click did not work

    You can add a breakpoint for the Click method to check if this method is executed correctly for the first time clicking.

    Best Regards,

    Daisy

    Saturday, February 24, 2018 9:56 AM