Gridview行编辑无效

时间:2017-08-16 05:39:47

标签: c#

任何人都可以告诉我我错过了为什么我的行编辑事件没有解雇。 我试图在同一网格中提供编辑工具以及添加新数据。 我能够添加新数据(新行),但无法编辑现有行以进行更新。 我的行编辑事件没有触发。



<asp:GridView ID="grdHierarchy" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="tableGridBorder" GridLines="None" ShowFooter="True" ShowHeaderWhenEmpty="True" Width="100%" OnRowCommand="grdHierarchy_RowCommand" OnRowDataBound="grdHierarchy_RowDataBound"
  OnRowEditing="grdHierarchy_RowEditing">
  <AlternatingRowStyle CssClass="AlternateRow" />
  <Columns>
    <asp:TemplateField HeaderText="Sr.#">
      <ItemTemplate>
        <asp:Label ID="lblSrNo" runat="server" Text="<%# Container.DataItemIndex + 1 %>"></asp:Label>
      </ItemTemplate>
      <ItemStyle CssClass="textAlignCenter" />
      <HeaderStyle CssClass="textAlignCenter" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
      <ItemTemplate>
        <asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:RequiredFieldValidator ID="rfvHierarchyName" runat="server" SetFocusOnError="True" ControlToValidate="txtHierarchyName" ErrorMessage="Enter Reference." ValidationGroup="grpHierarchy">
          <asp:TextBox ID="txtHierarchyName" CssClass="textBoxSmall" Style="visibility: visible" runat="server" Text='<%# Bind("Name") %>' MaxLength="50"></asp:TextBox>
        </asp:RequiredFieldValidator>
        <span style="color: Red;">*</span>
        <asp:RegularExpressionValidator ID="regexValiator3" runat="server" ControlToValidate="txtAPReference" ErrorMessage='Invalid Reference.' Display="None" ValidationGroup="grpHierarchy" ValidationExpression='([^<>\"\^])*'>
        </asp:RegularExpressionValidator>
      </EditItemTemplate>
      <FooterTemplate>
        <asp:RequiredFieldValidator ID="rfvHierarchyName" runat="server" SetFocusOnError="True" ControlToValidate="txtHierarchyName" ErrorMessage="Enter Reference." ValidationGroup="grpfHierarchy">
          <asp:TextBox ID="txtHierarchyName" CssClass="textBoxSmall" Style="visibility: visible" runat="server" Text='<%# Bind("Name") %>' MaxLength="50"></asp:TextBox>
        </asp:RequiredFieldValidator>
        <span style="color: Red;">*</span>
        <asp:RegularExpressionValidator ID="regexValiator4" Display="None" runat="server" ControlToValidate="txtHierarchyName" ErrorMessage='Invalid Reference.' ValidationGroup="grpfHierarchy" ValidationExpression='([^<>\"\^])*'>
        </asp:RegularExpressionValidator>
      </FooterTemplate>
      <ItemStyle CssClass="textAlignLeft" />
      <HeaderStyle CssClass="textAlignLeft" />
      <FooterStyle CssClass="textAlignLeft" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="LevelName">
      <ItemTemplate>
        <asp:Label ID="lblName" runat="server" Text='<%# Bind("Level.Name") %>'></asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:RequiredFieldValidator ID="rfvLevelName" runat="server" SetFocusOnError="True" ControlToValidate="ddlLevelName" ErrorMessage="Select Level Number." Width="100px" CssClass="redborderlarge" ValidationGroup="grpHierarchy" InitialValue="--Select--">
          <asp:DropDownList ID="ddlLevelName" Width="200px" Style="visibility: visible" runat="server">
          </asp:DropDownList>
        </asp:RequiredFieldValidator><span style="color: Red; margin-top: 4px;">*</span>
      </EditItemTemplate>
      <FooterTemplate>
        <asp:RequiredFieldValidator ID="rfvCustomerName" runat="server" SetFocusOnError="True" ControlToValidate="ddlLevelName" ErrorMessage="Select Customer." ValidationGroup="grpfHierarchy" InitialValue="--Select--">
          <asp:DropDownList ID="ddlLevelName" Width="200px" Style="visibility: visible" runat="server">
          </asp:DropDownList>
        </asp:RequiredFieldValidator><span style="color: Red;">*</span>
      </FooterTemplate>
      <ItemStyle CssClass="textAlignLeft" />
      <HeaderStyle CssClass="textAlignLeft" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Action" ShowHeader="False">
      <ItemTemplate>
        <asp:LinkButton ID="btnHierarchyEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit"></asp:LinkButton>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:LinkButton ID="btnHierarchyUpdate" runat="server" CommandName="Select" Text="Update" CausesValidation="true" ValidationGroup="grpfHierarchy" OnClick="btnHierarchyUpdate_Click" OnClientClick="return ToggleCursor2(1,'grpAP',true);"></asp:LinkButton>
        <asp:LinkButton ID="btnHierarchyCancel" runat="server" CausesValidation="false" CommandName="Select" Text="Cancel" OnClick="btnHierarchyCancel_Click"></asp:LinkButton>
      </EditItemTemplate>
      <FooterTemplate>
        <asp:LinkButton ID="btnHierarchyAdd" runat="server" CausesValidation="true" ValidationGroup="grpfHierarchy" CommandName="New" Text="Add" OnClientClick="return true;"></asp:LinkButton>
      </FooterTemplate>
      <ItemStyle CssClass="textAlignCenter" />
      <HeaderStyle CssClass="textAlignCenter" />
      <FooterStyle CssClass="textAlignCenter" />
    </asp:TemplateField>
  </Columns>
  <HeaderStyle CssClass="Header" />
  <RowStyle CssClass="Row" />
  <FooterStyle CssClass="Header borderBttm" />
</asp:GridView>
&#13;
&#13;
&#13;

我代码背后的代码就是这个事件没有触发。我错了

   protected void grdHierarchy_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdHierarchy.EditIndex = e.NewEditIndex;
        grdHierarchy.ShowFooter = false;
    }

   protected void grdHierarchy_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("New"))
        {
            Hierarchy Hierarchy = new Hierarchy();
            TextBox txtHierarchyName = (TextBox)grdHierarchy.FooterRow.FindControl("txtHierarchyName");
            DropDownList ddlLevelName = (DropDownList)grdHierarchy.FooterRow.FindControl("ddlLevelName");
            string HierarchyName = txtHierarchyName.Text.Trim();
            bool IsNameExist = Hierarchy.IsNameExist(Session, HierarchyName);
            if (IsNameExist == false)
            {
                Hierarchy.Id = 0;
                Hierarchy.Name = HierarchyName;
                Hierarchy.LevelId = Convert.ToInt64(ddlLevelName.SelectedValue);
                Hierarchy.CreatedDtTm = DateTime.Now;
                Hierarchy.ModifiedDtTm = DateTime.Now;
                Hierarchy.Save(Session);
                fillHierarchyGrid();
            }
            else
            {
                cGlobalUI.showPopupMsg(Page, "Hierarchy Name is already Exist", cGlobalUI.MessageType.mtInformation);
            }
        }
    }

0 个答案:

没有答案