Aditya Gavali
Aditya Gavali

Reputation: 11

I am exporting repeater data in Excel, but I don't want linkbuttons present in repeater to display in Excel

I don't want linkbuttons to be shown in the Excel sheet, is there a way to remove them in the class that I have created?

I am exporting repeater data into Excel, but I don't want linkbuttons present in repeater to display in Excel.

This is my class to export data

public void Export(Repeater rpt, string FileName)
{
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.Buffer = true;
      HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename="+ FileName +".xls");
      HttpContext.Current.Response.Charset = "";
      HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
      StringWriter sw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(sw);
      
      HttpContext.Current.Response.Output.Write("<table>");
      sw.Write("<tr>");
      sw.Write("<h3>Company Name<h3>");//Enter Company Name Here
      sw.Write("</tr>");
      sw.Write("<tr>");
      sw.Write("<h3>Address<h3>");//Enter Company Address 
      sw.Write("</tr>");
      sw.Write("<tr>");
      sw.Write("<h3>Contact<h3>");//Enter Company Contact Details 
      sw.Write("</tr>");
      sw.Write("<tr>");
      sw.Write("<h3>Email<h3>");//Enter Companys Emails 
      sw.Write("</tr>");
      #region trying foreach
      List<Repeater> rpts = new List<Repeater>();
     
      //foreach (Control control in rpts)   //remove linkbuttons 
      //{
      //    switch (control.GetType().Name)
      //    {
      //        case "HyperLink":
      //            rpts.Add(new Literal { Text = (control as HyperLink).Text });
      //            break;
      //        case "TextBox":
      //            cell.Controls.Add(new Literal { Text = (control as TextBox).Text });
      //            break;
      //        case "LinkButton":
      //           // rpts.Add(new Literal { Text = (control as LinkButton).Text });
      //            rpts.Add(new Literal { Text = (control as LinkButton).Text });
      //            break;
      //        case "CheckBox":
      //            cell.Controls.Add(new Literal { Text = (control as CheckBox).Text });
      //            break;
      //        case "RadioButton":
      //            cell.Controls.Add(new Literal { Text = (control as RadioButton).Text });
      //            break;

      //    }
      //    cell.Controls.Remove(control);
      //}
      //foreach (Control hlk in rpts)
      //{
      //    if (hlk is LinkButton)
      //    {
             
      //        LinkButton pp = (LinkButton)hlk;
          
      //        pp.Enabled = false;
      //        pp.Visible = false;
      //    }
      //}
      #endregion

      rpt.RenderControl(hw);

      HttpContext.Current.Response.Output.Write(sw.ToString());
      HttpContext.Current.Response.Output.Write("</table>");
      HttpContext.Current.Response.Flush();
      HttpContext.Current.Response.End();
}

This is button click event for exporting

protected void btnExport_Click(object sender, EventArgs e)
{
    ShowDetailsInExport s = new ShowDetailsInExport();
    s.Export(Repeater1);
}

If needed this is my ASP.NET repeater creation

<div class="acrepeater" style="background-color:#efefef;width:500px;height:350px;margin-left:30%">

    <table id="tblSuntech">
        <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="item_Command" >
            <HeaderTemplate>
                <thead >
                    <th style="border:1PX solid">Action</th>
                    <th style="border:1PX solid">CountryId</th>
                    <th style="border:1PX solid">CountryName</th>
                    <th style="border:1PX solid">Status</th>
                    <th style="border:1PX solid">Action</th>
                    <th ></th>

                </thead>
            </HeaderTemplate>
            <ItemTemplate>
                <%--OnClientClick="EditPop();return false;"--%>
                <tr class="tr">
                    <td style="border:1PX solid">
                 <asp:LinkButton ID="LinkButton3" CssClass="tbl-id-btn" runat="server" 
                     
                     ToolTip="View" CausesValidation="false" CommandName="DashBoard" 
                     CommandArgument='<%#Eval("CountryId")+","+ Eval("CountryName")%>' ForeColor="#0066FF">Edit</asp:LinkButton>
            </td>
                    <td style="border:1PX solid"><%# Eval("CountryId")%></td>
                    <td style="border:1PX solid"><%# Eval("CountryName")%></td>
                    <td style="border:1PX solid"><%# Eval("Status")%></td>

                    <td style="border:1PX solid">
         
                        <asp:LinkButton ID="LinkButton1" CssClass="tbl-id-btn" style="width:100px" runat="server" 
                     
                     ToolTip="View" CausesValidation="false" CommandName="IsBlock" 
                     CommandArgument='<%# Eval("[CountryId]")+","+Eval("Status")%>' ForeColor="Red"><%# Eval("[StatusMessage]")%></asp:LinkButton>
                        </td>
                   
            </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
</div>

Upvotes: 0

Views: 218

Answers (1)

AliNajafZadeh
AliNajafZadeh

Reputation: 1328

Every object in programming that displays a list of data needs a data source. This data source may be Datatable, Dataset Datatable, Linq List or any other type. All of these return a list, and this list can be represented by those objects. Before displaying the list, you can perform your desired operations (e.g. Sorting, Editing, Deleting, etc.) on the received list. You can also use the same list to send to Excel output. When you convert an object such as DataRepeater to Excel, a number of other objects with that object may be sent to Excel output. But when you send a pure list of data, such as a Datatable, to the Excel file creation method, in fact, only the sorted list submitted is converted to an Excel file, and the problems you mentioned no longer arise. I hope I was able to guide you with my own explanation. If you need more help please inform me.

Upvotes: 1

Related Questions