简体   繁体   中英

Asp.net client side editing of gridview gets overwritten

I have a simple gridview filled with a list of simple entities

  public class Order

    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }
        public int Quantity { get; set; }
    }

in the Page_Load method i do the following

if (!IsPostBack)
{

   this.magrid.DataSource = list;
   magrid.DataBind();
}

my page is defined as so

    <asp:panel runat="server" ID="MyOrders" Visible="true" Width="900px">
          <asp:UpdatePanel runat="server" ID="AjaxPanel">
              <ContentTemplate>
                  <asp:GridView runat="server" ID="magrid" AutoGenerateColumns="False" OnRowDataBound="RowDataBound">
                      <Columns>
                          <asp:BoundField  DataField="Id" HeaderText="Id"/>
                            <asp:BoundField DataField="Code" HeaderText="Code"/>
                            <asp:BoundField DataField="Description" HeaderText="Description"/>
                            <asp:BoundField DataField="Quantity" HeaderText="Quantity"/>
                              <asp:TemplateField
                        HeaderText="Action"
                        ItemStyle-CssClass="gviCnt gviCntProductid"
                        HeaderStyle-CssClass="gviModifyOrder">
                            <ItemTemplate>
                                    <asp:LinkButton 
                                        OnClientClick='<%# "MettreAZero(" + Eval("Id") + ");" %>'
                                        Visible='true'
                                        Text='Clear'  runat="server"
                                        ID="btnRemoveLine"></asp:LinkButton>
                              </ItemTemplate>
                    </asp:TemplateField>
                      </Columns>

                  </asp:GridView>
              </ContentTemplate>
          </asp:UpdatePanel>
   </asp:panel>
</div>
</form>

and the javascript function as such {

        function MettreAZero(id) {
            var table = document.getElementById("magrid");
            for (var i = 0, row; row = table.rows[i]; i++) {
                var cellid = row.cells[0].innerHTML;

                if (cellid == id) {
                    row.cells[3].innerHTML = "0";
                    return;
                }
            }
        }
    }
</script>

the problem I have is that when I click the button, the value of the selected line is changed to zero, but then automatically, the grid is reloaded to its original value.

What am I missing ?

thanks

In your code you setup a click event handler:

OnClientClick=<%# "MettreAZero(" + Eval("Id") + ");" %>

You should stop the submit event from posting back because you will lose the state you just modified. You just need to Modify MettreAZero() to return false; . Another option would be to use AJAX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM