2

我有中继器连接到我的数据库,我想将其数据导出到 xls 文件中。我能够做到这一点,除了因为我的 repater 包含一个控制按钮来操作行(删除)我的 xls 是用这个按钮呈现的。

<asp:Repeater ID="Repeater1" runat="server"   EnableTheming="true"onitemcommand="Repeater1_ItemCommand" >
    <HeaderTemplate>
            <table class="datatable fullwidthpercent"> 
               <tr>
                   <td>ID</td>                     
                   <td> Nom</td>                             
                   <td>Prénom</td>                              
                   <td>Email</td>                      
                   <td>Adresse</td>
                   <td>Ville</td>
                   <td>Tél</td>
               </tr>

    </HeaderTemplate>
        <ItemTemplate>

               <tr>
                   <td><%# Eval("id") %></td>                           
                   <td><%# Eval("nom") %></td>                         
                   <td><%# Eval("prenom") %></td>                         
                   <td><%# Eval("email") %></td>
                   <td><%# Eval("adresse") %></td>
                   <td><%# Eval("ville") %></td>
                   <td><%# Eval("tel") %></td>
                   <td><asp:Button ID="Btn1" runat="server" CommandArgument= ' <%# Eval("id") %> ' Text="Eliminer" OnClick = "buttonEliminer_click" ></asp:Button></td>                   
               </tr>                  

        </ItemTemplate>
        <SeparatorTemplate>
        <tr>
        <td colspan="6"><hr /></td>
        </tr>
        </SeparatorTemplate>
        <FooterTemplate>
           </table>
        </FooterTemplate>
</asp:Repeater>
</div>

代码背后:

  protected void ButtonExporter_Click(object sender, EventArgs e)
    {

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=Gagnants.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        Repeater1.RenderControl(htmlWrite);
        Response.Write("<table>");
        Response.Write(stringWrite.ToString());
        Response.Write("</table>");
        Response.End();

    }

使用内部控制渲染中继器时克服表单应答器问题的覆盖方法:

public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

我在输出 xls 得到这个: 在此处输入图像描述

4

1 回答 1

2

基本上,您需要实现将按钮 ( Btn1) 的可见性设置为 false 的代码。当服务器控件的可见性设置为 false 时,它​​不会呈现在页面上。

我建议进行以下代码更改:

首先,创建一个页面级变量来确定您要呈现的内容。您想将此设置为默认值true,因为这是您的默认状态(渲染到屏幕)

private bool _IsRenderForScreen = true;
public property bool IsRenderForScreen {
   get {
      return this._IsRenderForScreen;
   }
   set {
      this._IsRenderForScreen = value;
   }
}

然后,在ButtonExporter_Click方法中,将此变量设置为 false:

this.IsRenderForScreen = false;

然后挂钩到中继器的ItemDataBound事件:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" ...

然后添加以下方法来处理事件:

public void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        if(!this.IsRenderForScreen) {
            ((Button)e.Item.FindControl("Btn1")).Visible = false;
        }
    }
}

HTH。让我知道是否有任何不清楚的地方

于 2013-08-10T00:23:43.753 回答