Deepak K
Deepak K

Reputation: 57

How to disable particular commandfield in a gridview?

I have a gridview with a label and a commandfield for edit. I want to disable the commandfield if the label contains today's date.

All commandfields of empty labels should be enabled. How can I achieve that?

Thanks

Aspx -

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Schedule Date">
<ItemTemplate>
<asp:Label ID="schedule" runat="server" Text='<%# Eval("Date","{0:dd-MMM-yy}") %>'></asp:Label>
</ItemTemplate>
<asp:CommandField ShowEditButton="true" EditText="Click here to Schedule/Re-Schedule" UpdateText="Submit" HeaderText="Schedule/Re-Schedule" />
</Columns>
</asp:GridView>

Upvotes: 0

Views: 1355

Answers (1)

Mohammed Alasa&#39;ad
Mohammed Alasa&#39;ad

Reputation: 539

its better if you use command field as linkButton:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
    onrowdatabound="gv_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Schedule Date">
            <ItemTemplate>
                <asp:Label ID="schedule" runat="server" Text='10:25-02-2015'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

then you can use row data bound event:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label schedule = (Label)e.Row.FindControl("schedule");
            LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
            if (schedule.Text == string.Empty)
            {
                LinkButton1.Enabled = false;
            }
            else
            {
                LinkButton1.Enabled = true;
            }
        }
    }

This will help you!

Upvotes: 1

Related Questions