1

我有一个简单的gridview,里面有一个简单实体的列表

  public class Order

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

在 Page_Load 方法中,我执行以下操作

if (!IsPostBack)
{

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

我的页面是这样定义的

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

和这样的javascript函数{

        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>

我遇到的问题是,当我单击按钮时,所选行的值会更改为零,但随后会自动将网格重新加载为其原始值。

我错过了什么?

谢谢

4

1 回答 1

2

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.

于 2013-11-05T20:55:36.090 回答