GridView Button Field, can I enable/disable the button in the column in code behind

I have in my code right now where I can make the column visible or not visible such as:

<asp:ButtonField Text="Publish" ShowHeader="false" HeaderStyle-BackColor="white" ButtonType="Button" CommandName="Publish">
                                    <ItemStyle HorizontalAlign="Center" Width="150px" />
                                    <HeaderStyle HorizontalAlign="Center" Width="150px" />
                                </asp:ButtonField>

this is column 6:

if my condition then:
 GridView1.Columns[6].visible = false;

I want to always show it but make the buttons enabled or disabled?
sbornstein2Asked:
Who is Participating?
 
SammyConnect With a Mentor Commented:
Remove
------------------------------------------------------------------------------------------------------------------------------------------------------
<asp:ButtonField Text="Publish" ShowHeader="false" HeaderStyle-BackColor="white" ButtonType="Button" CommandName="Publish">
                                    <ItemStyle HorizontalAlign="Center" Width="150px" />
                                    <HeaderStyle HorizontalAlign="Center" Width="150px" />
                                </asp:ButtonField>
-----------------------------------------------------------------------------------------------------------------------------------------------------------

<ItemTemplate>
<asp:Button ID="btnPublish" runat="server" Text="Publish" CommandName="Publish" />
</ItemTemplate>
</asp:TemplateField>

and your code behind should look like this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        {
        Button btnPublish = (Button)e.Row.Cells[0].FindControl("btnPublish");
       if(btnPublish!=null)
{
        btnPublish.Visible = false;
}
    }
}
0
 
SammyCommented:
You need to do this in the RowDataBound event of the GridView
0
 
sbornstein2Author Commented:
can you provide an example at sammy please to handle the enable
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sbornstein2Author Commented:
thanks Sammy really appreciate your help
0
 
sbornstein2Author Commented:
One last question Sammy.  Once the button is clicked I do the following in the row command:

if (e.CommandName=="Publish")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            string BookFormatID = row.Cells[1].Text;

for some reason the commandargument is "" and it throws an error.  Any idea?  I am trying to get the row to get the cell text of column 1.
0
 
sbornstein2Author Commented:
Input string was not in correct format.  I beleive before it would be a number the commandargument
0
 
sbornstein2Author Commented:
Forget it, I figured it out Sammy, thanks again.

CommandArgument="<%# Container.DataItemIndex %>"
0
 
SammyCommented:
Sorry I was too busy today
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.