点击按钮后,RowCommand不触发。

7
我已经找到了解决方案,我只是想发布它,以便对某些人有用。
这是使用命令的按钮。
<dxwgv:ASPxGridView ID="gdvxUsers" runat="server" AutoGenerateColumns="False" Width="100%" KeyFieldName="UserName" onrowcommand="gdvxUsers_RowCommand">
        <Columns>
        <dxwgv:GridViewDataTextColumn Caption="Edit" VisibleIndex="0" Width="0px">
            <DataItemTemplate>
                <asp:ImageButton ID="imbEdit" runat="server"
                    CommandName = "Edit"
                    ImageUrl="~/images/icon/Edit-icon.png" ClientIDMode="Static" />
            </DataItemTemplate>
        </dxwgv:GridViewDataTextColumn>
</dxwgv:ASPxGridView>

    protected void gdvxUsers_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        switch (e.CommandArgs.CommandName)
        {
            case "Edit":



            break;
        }
    }

按钮被点击时,行命令未触发。
2个回答

10
问题在于在Page_Load中我使用Databind()命令来绑定我的GridView,而我又在rowcommand事件中使用它。看起来在DataBind()之后,rowcommand被取消了。
    protected void Page_Load(object sender, EventArgs e)
    {
            gdvxUsers.DataSource = GetAllUserAndRole();
            gdvxUsers.DataBind();            
    }

所以我通过只在第一次加载时绑定数据来解决了这个问题。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gdvxUsers.DataSource = GetAllUserAndRole();
            gdvxUsers.DataBind();
        }
    }

1

你可能在表格中设置了EnableViewState="false"

如果是这种情况,Rowcommand事件也不会触发。

你在页面上设置了EnableEventValidation="true"

如果是这种情况,RowCommand事件将不会触发,请将其设置为false。

解决方案是将其设置为true。


启用视图状态对我来说是关键,非常感谢。 - KE50
我六年前发布了这个帖子,我不再从事ASP.NET Webform的工作,但感谢您的回复。这对其他人会很有用 :) - Sarawut Positwinyu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接