具体代码(下载交换机资料):

//DataGrid控件不能分页,否则下载会失败.

<%@page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.OleDb"%>
<%@import namespace="System.IO"%>

<script language="c#" runat="server">
string connstr=ConfigurationSettings.AppSettings["connstr1"];
public void page_load(Object obj,EventArgs e)
{
   if(!Page.IsPostBack){
     BindData();
   }
}

private void BindData()
{
   string sql=@"Select

catalyst.id,catalyst.label,catalyst.types,catalyst.ipv4,catalyst.moreinfo,catalyst.up_port,catalyst.port,catalyst.enterdate,l

ocation.location_name From catalyst,location where catalyst.location=location.location_id
order by substring(catalyst.ipv4,8,3)";

   DataView dv=new DataView(ExecuteToDataSet(sql));
   int RowCount=dv.Count;
   mess.Text="<b>(表的总记录数:"+RowCount.ToString()+")</b>";
   Results.DataSource=dv;
   Results.DataBind();
}

private DataTable ExecuteToDataSet(string sql)
{
 try{ 
   OleDbConnection conn=new OleDbConnection(connstr);
   DataSet ds=new DataSet();
   OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
   da.Fill (ds);
   return ds.Tables[0];
  }
  catch(OleDbException ex){
    info.Text=ex.Message;
    return null;
  }    
}

private void Lb_Click(Object sender,EventArgs e)
{
    Response.ContentType="application/vnd.ms-excel"; 
    Response.Charset=""; 
    this.EnableViewState=false;   
    StringWriter sw=new StringWriter();
    HtmlTextWriter hw=new HtmlTextWriter(sw);
    Results.RenderControl(hw); 
    Response.Write(sw.ToString()); 
    Response.End();
}
</script>
<html>
<head>
    <title>c#</title>
</head>
<body>
<form runat="server">
<asp:LinkButton OnClick="Lb_Click" Text="下载交换机资料" runat="server"/>
<asp:label id="mess" forecolor="red" runat="server"/>
<asp:label id="info" forecolor="blue" runat="server"/>

<asp:datagrid id="Results" runat="server"
alternatingitemstyle-backcolor="#799AE1"
ItemStyle-BackColor="#CCCCFF"
AutoGenerateColumns="false"
headerstyle-backcolor="#6699ff"
font-size="9pt"
bordercolor="#FFFFFF">
<columns>

<asp:TemplateColumn HeaderText="编号" >
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"Label")%>#
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="类型" >
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"types")%>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="交换机IP" >
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"ipv4")%>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="线缆来源">
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"location_name")%>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="具体位置">
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"moreinfo")%>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="上联口">
  <ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"up_port")%>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="父级端口">
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"port")%>
  </ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="加入日期" >
  <ItemTemplate>
   <%#DataBinder.Eval(Container.DataItem,"enterdate","{0:yyyy/MM/dd}")%>
  </ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:datagrid>
</form>

</body>
</html>

posted on 2005-04-27 10:09  oupatch  阅读(2600)  评论(2编辑  收藏  举报