user175084
user175084

Reputation: 4640

disable delete button if value in database table's--> column is true or checked

Ok... I have a database table called employees..

This has columns called ID, Name, datejoined and Cannotbedeleted (check boxes)...

Now i want to add a delete column which deletes the rows when clicked.

But there are some entries which cannot be deleted if the Cannotbedeleted field is true (checked)... so the delete button should be invisible for these rows.

Please tell me a way of how to do this...

            <asp:CheckBoxField DataField="CannotBeDeleted" HeaderText="CannotBeDeleted" 
                SortExpression="CannotBeDeleted" />
            <asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded" 
                SortExpression="TimeAdded" />
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"   
                        CommandName="Delete" Text="Delete" ></asp:LinkButton> 
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>

I also tried using in delete template field... ' >

But i did not know what to do in code behind?? protected bool GetShowDeleteButton() {

}

The solution is below.... but is there a way i can refresh the page once i click the delete button in the gridview....

Upvotes: 0

Views: 3362

Answers (2)

Matthew Jones
Matthew Jones

Reputation: 26190

Try looping through the rows in GridView_DataBound, and hide the button for each row that has the checkbox checked.

protected void GridView1_DataBound(object sender, EventArgs e)
{
    foreach(GridViewRow myRow in GridView1.Rows)
    {
        //Find the checkbox
        CheckBox ckbox1 = (CheckBox)myRow.FindControl("nameOfCheckBox");
        if(ckbox1.Checked)
        {
            //Find the Delete linkbutton and hide it
            LinkButton deleteButton = (LinkButton)myRow.FindControl("nameOfDeleteLinkButton");
            deleteButton.Visible = false;
        }
    }
}

Now, here's the difference you need:

Implement the CheckBox column as a TemplateField with a CheckBox in it. You can bind the CheckBox to the data from your datasource.

Upvotes: 2

hamstar
hamstar

Reputation: 1817

Do it like this:

if($row['cannotbedeleted'] == true) {
$buttonDisplay = 'none';
}

echo "<button onclick='delete();' style='display: $buttonDisplay;'>Delete</button>";

Well that would be the solution in PHP but if the style of the button has "display: none;" in it the button will be hidden. :) HTH

Upvotes: -1

Related Questions