【问题标题】:ASP.NET Gridview RowUpdating event not firingASP.NET Gridview RowUpdating 事件未触发
【发布时间】:2010-12-17 09:05:43
【问题描述】:

HTML 页面:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
            <ContentTemplate>
                <asp:GridView ID="Grd_Threshold" runat="server" CellPadding="4" ForeColor="#333333"
                    GridLines="Both" AutoGenerateColumns="false" EmptyDataText="No Records." PageSize="8"
                    AllowPaging="true" OnPageIndexChanging="Grd_Threshold_PageIndexChanging" OnRowCommand="Grd_Threshold_RowCommand"
                    DataKeyNames="ID" AutoGenerateEditButton="true" OnRowCancelingEdit="Grd_Threshold_RowCancelingEdit" OnRowEditing="Grd_Threshold_RowEditing"
                    OnRowUpdating="Grd_Threshold_RowUpdating">
                    <Columns>                        
                        <asp:TemplateField HeaderText="No.">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name" ItemStyle-Width="150px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("CLNAME") + ", " + Eval("CFNAME") +" " +Eval("CMNAME")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Doctor" ItemStyle-Width="150px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("DOCTORNAME")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Item Name" ItemStyle-Width="150px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("INAME")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Item Form" ItemStyle-Width="100px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("IFORM")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Item Strength" ItemStyle-Width="100px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ISTRG")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Order Date" ItemStyle-Width="100px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ORDERDATE")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Price" ItemStyle-Width="80px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("PRICE")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="80px">
                            <EditItemTemplate>
                                <asp:TextBox ID="Txt_Qty" runat="server" Width="50px" Text='<%# Eval("QTY")%>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("QTY")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="User" ItemStyle-Width="100px">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("USERID")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <RowStyle BackColor="#E3EAEB" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#7C6F57" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>

CS 代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Title = "Dashboard";
                if (!Master.Page.IsPostBackEventControlRegistered)
                {
                    BindGrid();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog(ex);
            }
        }

        private void BindGrid()
        {
            try
            {
                if (SessionUtility.DashboardRecords == null || SessionUtility.DashboardRecords.Rows.Count == 0)
                {
                    string WebServiceUrl = SessionUtility.WebServiceUrl + "/GetAllOrders/";
                    JsonArrayCollection ReqResponse = Common.GetWebServiceResponse_MultipleValues(WebServiceUrl, "GET");
                    DataTable Patients = Common.ConvertJsonArrayObjectCollectionToDataTable(ReqResponse);

                    SessionUtility.DashboardRecords = Patients;
                }

                if (SessionUtility.UserRight != "1")
                {
                    //Grd_Threshold.Columns[0].Visible = false;
                    Grd_Threshold.AutoGenerateEditButton = false;
                }
                else if (SessionUtility.UserRight == "1")
                {
                    Grd_Threshold.AutoGenerateEditButton = true;
                }
                Grd_Threshold.DataSource = SessionUtility.DashboardRecords;
                Grd_Threshold.DataBind();
                UpdatePanel1.Update();
            }
            catch (Exception ex)
            {
                Common.WriteLog(ex);
            }
        }

大家好,

在上面编写的代码中,只有 EDIT 事件被触发,并且在 UPDATE 之后只有一次,CANCEL 没有事件被触发。我做错了什么?我想做的就是根据在 QTY 字段中输入的值更改 GridRow 颜色。

【问题讨论】:

    标签: c# asp.net events gridview


    【解决方案1】:

    我发现了问题所在。由于我给每一行中的标签提供了相同的 ID,该事件没有触发。希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      不确定您是否使用了IsPostBackEventControlRegistered - 我以前没有见过它使用过,但似乎您每次在Page_Load() 中都通过DataBind() 清除了您的更新事件。

      Page_Load 每次页面加载时都会触发,然后通过重置网格来更改页面正在执行的操作。

      尝试将控件包装在:

      if(!IsPostBack)
      {
        // bind
      }
      

      所以它只在第一个页面加载时执行此操作。

      【讨论】:

      • 另外,我也不例外捕捉专家,看来你的 try catch 块有点笼统。这可能导致异常被吞没(即使它们已被记录),然后程序可以在未知状态下继续。您可以使用像这样的一般异常处理来暴露安全漏洞或破坏您自己的数据。
      • 我使用了 IsPostBack 属性,但同样的事情发生了。
      • 当我点击网格中的更新链接时,Page_Load 事件每次都会触发,但不会触发 RowUpdating 事件。
      • 我也尝试了这里写的所有东西forums.asp.net/t/1177561.aspx,但仍然没有运气。
      • 我还注意到一件事,如果我自动生成列,所有事件都会被触发而没有任何问题,但如果我使用 TemplateField,只会触发一次编辑事件。抱歉,我知道我在自己的问题上发布了很多 cmets。
      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多