Search code examples
c#asp.netexcelgridviewexport-to-excel

Gridview export to excel picking up non-visible control


I have a gridview that I use export to excel on (http://exporttoexcel.codeplex.com/)

One of the columns use a template field to either show a literal or linkbutton depending on the value of a column:

            <asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Right">
                <ItemTemplate>
                    <asp:LinkButton ID="taskLinkButton" Text='<%# Eval("Total") %>' Visible='<%# Eval("StoryTitle").ToString() != "" %>' runat="server" OnClick="taskLinkButton_Click" />
                    <asp:Literal ID="Literal1" Text='<%# Eval("Total") %>' Visible='<%# Eval("StoryTitle") == "" %>' runat="server" />
                </ItemTemplate>
          </asp:TemplateField>

Here is the gridview being rendered:

enter image description here

My problem is that when I export it is picking up both of the controls, here is the excel file:

enter image description here

How can I prevent this so it only shows the one control?


Solution

  • Instead of using two controls, why not just enable / disable the link button based on the same criteria for showing the two controls you have now? Or you could set the onclient_click to return false if you don't want them to be able to click the button based on your criteria.