using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;public partial class _Default : System.Web.UI.Page  
{
  public string path3 = "";
    
  protected void Button1_Click(object sender, EventArgs e)
  {
  Label label = new Label();
  label.ID = "label4";
  label.Text = "";
  GridView gridview = new GridView();
  gridview.ID = "GridView1";
  Panel1.Controls.Add(gridview);
  Panel1.Controls.Add(label);  string inputpath = TextBox1.Text + TextBox2.Text + TextBox3.Text;  path3 = inputpath;  string dir = @"D:\AA\BB";
   
  string[] files1 = Directory.GetFiles(dir);  foreach (string i in files1)
  {
  string path1 = dir + "\\" + inputpath + ".xls";
  if ( path1 == i)
  {
    
  label.Text = path1;
  Button button = new Button();
  button.ID = "botton1";
  button.Text = "打开文件";
  Panel1.Controls.Add(button);
  button.Click += new EventHandler(OpenClick);
  string excelFilePath = path1;   
  Excel.Application myExcel = new Excel.ApplicationClass();  
  object oMissing = System.Reflection.Missing.Value;
    
  myExcel.Application.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
    
  Excel.Workbook myBook = myExcel.Workbooks[1];
    
  Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];
    
  DataTable dt = new DataTable("mytable");
    
  dt.Columns.Add("F1", System.Type.GetType("System.String"));
    
  dt.Columns.Add("F2", System.Type.GetType("System.String"));
    
  dt.Columns.Add("F3", System.Type.GetType("System.String"));
  dt.Columns.Add("F4", System.Type.GetType("System.String"));
  dt.Columns.Add("F5", System.Type.GetType("System.String"));
  DataSet myDs = new DataSet();
  myDs.Tables.Add(dt);
  DataRow myRow;
  myDs.Clear();
  for (int j = 1; j <= 12; j++)  
  {
  myRow = myDs.Tables["mytable"].NewRow();
  for (int k = 1; k <= 5; k++)
  {
  Excel.Range r = (Excel.Range)mySheet.Cells[j, k];
  string strValue = r.Text.ToString();
  string aa = strValue;
  string columnname = "F" + k.ToString();
  myRow[columnname] = strValue;
  }
  myDs.Tables["mytable"].Rows.Add(myRow);
  }  GridView1.DataSource = myDs.Tables["mytable"].DefaultView;
  GridView1.DataBind();
  myExcel.Application.Workbooks.Close();
  myExcel.Quit();
  myExcel = null;  if (!IsPostBack)
  {
  Response.ClearHeaders();   
  Response.Redirect(path1);  
  }  break;
  }
  else
  {
    
  label.Text = "文件不存在";
    
    
  GridView1.DataSource = null;  
  GridView1.DataBind();   
  }
  }
  }    
  protected void Page_Load(object sender, EventArgs e)
  {
      }  public void OpenClick(object sender, EventArgs e)
  {  string dir = @"D:\AA\BB";
  string inputpath = TextBox1.Text + TextBox2.Text + TextBox3.Text;
  path3 = inputpath;  string path = dir + "\\" + path3 + ".xls";
    
  Excel.Application app;   
  Excel.Workbook workBook;   
  Excel.Worksheet workSheet;   
  app = new Excel.ApplicationClass();
  app.Visible = true;   
  workBook = app.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1);  }
}这是源代码,不好意思,新手,有点乱。
我的问题就是:生成的那个“打开文件”的按钮,虽然网页里面生成了,但是用鼠标一点,就没了,然后我把它放到页面加载的方法里面,虽然好用,但是找不到文件的时候,这个按钮也存在。
想请高手解决下,怎么弄?

解决方案 »

  1.   

    但是找不到文件的时候:Button.Visiable =false;
      

  2.   

    我的意思是找不到文件的时候,不显示button
      

  3.   

    动态生成的控件需要每次回发的时候重新生成。所以应该放在Page_Load()中
    根据文件的是否存在控制是否重新生成按钮
    if(文件存在)
    {
    Button b = new Button();
    }
      

  4.   

    页面变量 Boolean  bl=false;//标记是否找到文件。
      

  5.   

    上个贴子已经有答案了,只是你没好好研究;方法一:你在页面上直接加一个button,需要显示的时候就visible=true,不需要就false; 简单方法二:放在page_load内,Button1_Click(..)控制它是不是加载,以下的viewstate可用session代替
    Page_Load(..){
    if(this.ViewState["aaa"] == null)
    {
    this.ViewState.Add("aaa","");
    }
    if(this.ViewState["aaa"].ToString() == "1")
    {
    //new button .....
    }
    }
    Button1_Click(..)
    {
    this.ViewState["aaa"] = "";//不显示
    this.ViewState["aaa"] = "1";//显示
    }