• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6094
  • Last Modified:

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?
0
sbornstein2
Asked:
sbornstein2
  • 5
  • 3
1 Solution
 
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
 
SammyCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now