网格中的OnRowCommand没有触发?

时间:2011-08-10 16:33:23

标签: asp.net

我经过很长一段时间的工作并面临一个问题。

在网格中,有一些TemplateFields具有用于编辑和删除的链接按钮。 OnRowCommand事件未在gird中触发。重要的一点是,如果我自动生成编辑或删除按钮,则会触发其RowCommand事件。

这是我的aspx代码:

 <asp:GridView ID="gvContact" runat="server" AllowPaging="True" AllowSorting="True"
        DataKeyNames="MailId" AutoGenerateColumns="False" CssClass="ContactsGridViewStyle"
        GridLines="None" OnRowEditing="gv_RowEdit" OnRowDataBound="gv_RowDataBound" OnRowCancelingEdit="gv_CancellingEdit"
        OnRowUpdating="gv_RowUpdating" Width="600px" OnPageIndexChanging="gv_PageChanging"
        OnRowCommand="gv_RowCommand" EmptyDataText="No Record found" EmptyDataRowStyle-ForeColor="Red"
        EmptyDataRowStyle-HorizontalAlign="Center">
        <RowStyle CssClass="RowStyle" />
        <EmptyDataRowStyle CssClass="EmptyRowStyle" />
        <PagerStyle CssClass="PagerStyle" />
        <SelectedRowStyle CssClass="SelectedRowStyle" />
        <HeaderStyle CssClass="HeaderStyle" />
        <EditRowStyle CssClass="EditRowStyle" />
        <AlternatingRowStyle CssClass="AltRowStyle" />
        <Columns>
        <asp:TemplateField HeaderText="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox><br />
                    <asp:RequiredFieldValidator runat="server" ID="rfvName" ControlToValidate="txtName"
                        Display="Dynamic" Text="Name is a required field"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email Id">
                <EditItemTemplate>
                    <asp:TextBox ID="txtMessageTo" runat="server" Text='<%# Bind("MessageTo") %>'></asp:TextBox><br />
                    <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" ControlToValidate="txtMessageTo"
                        Display="Dynamic" Text="Email is a required field"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="Dynamic"
                        Text="Email address is Invalid" SetFocusOnError="true" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                        ControlToValidate="txtMessageTo">                    
                    </asp:RegularExpressionValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblMessageTo" runat="server" Text='<%# Bind("MessageTo") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton runat="server" ID="ibtn" CommandArgument='<%#Eval("RowId")%>' CommandName="EEE"
                        ImageUrl="~/Images/cross.png" />
                    <asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" CommandArgument='<%# Eval("RowId")%>'
                        CommandName="EE"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton runat="server" ID="lbtnUpdate" Text="Update" CommandName="UU"></asp:LinkButton>
                    <asp:LinkButton runat="server" ID="lbtnCancel" Text="Cancel" CommandName="CC"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnDelete" runat="server" Text="Delete" CommandArgument='<%# Eval("RowId")%>'
                        CommandName="DD"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

在CS文件中我绑定网格!Page.IspostBack。

请指导。

2 个答案:

答案 0 :(得分:2)

您不能添加!Page.IsPostBack条件并将网格绑定放入

protected override void OnInit() { 
   MyGrid.DataBind(); 
   base.OnInit();
}

如果在“代码”而不是Object / Sql / Entity | Datasource中绑定网格,则必须将其添加到OnInit();因为那时,它所需要的一切都会被注册到ViewState。

注意:对不起我的英语,我通常会说法语。

答案 1 :(得分:2)

我基本上花了一整天时间来解决这个问题;这个问题似乎在我的Google查询中出现了很多,所以希望这个答案会有所帮助,即使问题本身已经很老了。

问题是由页面上的验证器引起的,例如&lt; asp:RequiredFieldValidator&gt;

在网格上触发更新事件时执行这些字段的验证,而不仅仅是在按下“确定”按钮时。据推测,此时验证失败,因此更新事件在到达处理程序之前被取消。

解决方法是使用验证器的ValidationGroup属性和用于触发这些验证的操作按钮,如Scott Guthrie的帖子所述:http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

相关问题