Opening a file using dynamically created link button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sujeetharan
    New Member
    • Sep 2010
    • 1

    #1

    Opening a file using dynamically created link button

    I am writing a code for a search function. I need to search the folders for the particular file/files(only excel files/ there may be several files with the same name) & display them. I should also be able to open each of the file by clicking it.

    i have succeeded in displaying the results in a dynamic table. I am using link buttons to open the files.when i click the button there is no action.
    my code
    Code:
    namespace WebApplication3
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            string sku;
            string sku_trim;
            string sku1;
            string sku_uppercase;
            string order_confirm;
            string quoation;
            string year;
            string searchwithin;
            string searchfor;
            int match;
            int number;
           LinkButton lbn;
           string path;
           string path_quote;
    
           private void Page_Load(object sender, EventArgs e)
           {
               year = Session["year"].ToString();
               sku = Session["search"].ToString();
               if (Session["search"] != null)
               {
    
                   Label1.Text =  Session["search"].ToString();
                   sku_trim = Label1.Text;
                   sku_uppercase = sku_trim.Replace(" ", "");
                   sku = sku_uppercase.ToUpper();
                   order_confirmation();
    
               }
           }
    
            public void order_confirmation()
            {
                         
                    DirectoryInfo static1 = new DirectoryInfo(@"C:\Users\Dell\Desktop\SKU");
                    path = static1.FullName + @"\" + year + @"\OrderConfirmation";
    
                    DirectoryInfo dir = new DirectoryInfo(path);
                    //  DirectoryInfo  dir = new DirectoryInfo(@"C:\Users\Dell\Desktop\SKU\2010\OrderConfirmation");
                    FileInfo[] xlsxfiles = dir.GetFiles("*.xlsx", SearchOption.AllDirectories);
                    number = xlsxfiles.Count();
                    //Response.Write("Order Confirmation");
                    Table dynTable = new Table();
                    dynTable.ID = "order_confirmation";
                    dynTable.BorderWidth = 1;
                    //   this.Controls.Add(dynTable);
                    this.form1.Controls.Add(dynTable);
    
                    foreach (FileInfo f in xlsxfiles)
                    {
    
    
                        searchwithin = f.Name;
                        searchfor = sku;
                        match = searchwithin.IndexOf(searchfor);
                        // sku1 = sku + ".xlsx";
                        Label confirm_label = new Label();
                        Label button_perform = new Label();
                        if (match >= 0)
                        {
    
                            confirm_label.Text = f.FullName;
                            order_confirm = f.FullName;
                            TableRow row = new TableRow();
                            row.BorderWidth = 1;
                            TableCell cell1 = new TableCell();
                            cell1.BorderWidth = 1;
                            cell1.Controls.Add(confirm_label);
                            row.Controls.Add(cell1);
                            dynTable.Controls.Add(row);
    
                            TableCell cell2 = new TableCell();
                            cell2.BorderWidth = 1;
    
                            lbn = new LinkButton();
                            lbn.ID = f.FullName;
                            lbn.Text = "Open";
                            lbn.Command += new System.Web.UI.WebControls.CommandEventHandler(btn_open_Click);
    
                            lbn.CommandName = f.FullName;
                            lbn.CommandArgument = f.FullName;
    
                            cell2.Controls.Add(lbn);
                            row.Controls.Add(cell2);
                            dynTable.Controls.Add(row);
    
                            //  this.Controls.Add(new LiteralControl("<br>"));
                             //Response.Write("<br><a href=" + f.Name >" + f.FullName+ "</a>");
    
                        }
                        else
                        {
    
                        }
    
                    }         
                
            }
    
           
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            protected void btn_open_Click(object sender,CommandEventArgs e)
            {
    
                //HttpContext.Current.Response.Clear();
                //HttpContext.Current.Response.ClearHeaders();
                //HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + order_confirm);
                //HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                //HttpContext.Current.Response.WriteFile(order_confirm);
                //HttpContext.Current.Response.End();
                
                Response.Write("hello");
               
               
            }
       
    
        }
        
    }
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    I would check the following 2 things:
    1) What happens with the "Click" event handler instead of "Command" handler
    2) What happens if you make the handler public instead of protected.

    Comment

    Working...