更新GridView控件中的按钮属性

时间:2010-06-22 09:32:29

标签: c# asp.net

我有一个包含表中记录的GridView,可以删除或更新。如果要编辑一行,它应显示更新和取消按钮,以便您可以保留或取消当前编辑(我隐藏更新并取消默认)。我使用以下asp作为GridView的按钮列:

<asp:TemplateField visible ="true" >
<HeaderTemplate>
    <div id = "header">Maintain</div>
</HeaderTemplate>
<ItemTemplate>
        <asp:Button ID="btnDelete"  Text="Delete"   Runat="Server" CommandName="Delete" Visible="true"  CommandArgument='<%#Eval("UNIQUEID")%>'/>
        <asp:Button ID="btnEdit"    Text="Edit"     Runat="Server" CommandName="Edit"   Visible="true"  CommandArgument='<%#Eval("UNIQUEID")%>'/>
        <asp:Button ID="btnCancel"  Text="Cancel"   Runat="Server" CommandName="Cancel" Visible="false" CommandArgument='<%#Eval("UNIQUEID")%>'/>
        <asp:Button ID="btnUpdate"  Text="Update"   Runat="Server" CommandName="Update" Visible="false" CommandArgument='<%#Eval("UNIQUEID")%>'/>
</ItemTemplate>
</asp:TemplateField>

我使用以下代码来处理已按下的命令:

protected void grdResults_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int pk;
    if (e.CommandName == "Edit")
    {
        int index = 0;
        foreach (result r in allSearchResults)  //get the row index with the grid
        {
            if (r.UniqueID == Convert.ToInt32(e.CommandArgument))
                break;
            index++;
        }
        ((Button)(grdResults.Rows[index].FindControl("btnUpdate"))).Visible = true;
        ((Button)(grdResults.Rows[index].FindControl("btnCancel"))).Visible = true;
    }
    if (e.CommandName == "Delete")
    {
        _presenter.modifySearchResults(Convert.ToInt32(e.CommandArgument));  //delete the item from the search results
    }
    if (e.CommandName == "Update")
    {
        //TODO update the search results with the edited text
    }
    if (e.CommandName == "Cancel")
    {
        //TODO cancel the current edit
    }
}

但出于某种原因,当我点击编辑时,它不会显示更新按钮。当我单步执行代码时,它会找到btnUpdate控件并正确更改其可见性,但不会显示在屏幕上。

1 个答案:

答案 0 :(得分:1)

可能它在ASP.NET生命周期中处于错误的位置。尝试将显示/隐藏按钮移动到PreRender