1

I am new to the asp.net, and sorry for grammar mistakes.

I have 3 fields in my table, name, age and created_date. I used the grid view to display all the fields on a web form. Later, I added a command column and then converted it to a template field.

I need to disable the edit button based on created_date; i.e if created_date + 5 > sysdate then I need to disable the edit button.

How can I do this?

4

2 回答 2

0

您可以在代码后面添加此代码,以实现网格视图的 RowDataBound 事件:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs  e)
{
    // check if this row is on the edit state
    if (e.Row.RowState == DataControlRowState.Edit)
    {
        // get date_created from bind
        DateTime date_created = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "date_created"));

        // check if from this date more 5 days if greather than now
        if(date_created.AddDays(5) > DateTime.Now)
        {
            // disable button
            ((Button) e.Row.FindControl("btnEdit")).Enabled = false;
        }
    }
}

在 asp.net 服务器标签上,您可以在 gridview 标签上引用此事件:

<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound">
...
</asp:GridView>
于 2012-07-03T20:25:41.643 回答
0

对于像这样的 asp.net 按钮可能会成功

<asp:Button ID="Edit" runat="server" Text="Edit" ... 
    Enabled='<%# ((DateTime)Eval("created_date")).AddDays(5) > DateTime.Now ? false : true %>'/>
于 2012-07-03T20:21:39.117 回答