473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. namespace WebApplication3
  2. {
  3.     public partial class WebForm1 : System.Web.UI.Page
  4.     {
  5.         string sku;
  6.         string sku_trim;
  7.         string sku1;
  8.         string sku_uppercase;
  9.         string order_confirm;
  10.         string quoation;
  11.         string year;
  12.         string searchwithin;
  13.         string searchfor;
  14.         int match;
  15.         int number;
  16.        LinkButton lbn;
  17.        string path;
  18.        string path_quote;
  19.  
  20.        private void Page_Load(object sender, EventArgs e)
  21.        {
  22.            year = Session["year"].ToString();
  23.            sku = Session["search"].ToString();
  24.            if (Session["search"] != null)
  25.            {
  26.  
  27.                Label1.Text =  Session["search"].ToString();
  28.                sku_trim = Label1.Text;
  29.                sku_uppercase = sku_trim.Replace(" ", "");
  30.                sku = sku_uppercase.ToUpper();
  31.                order_confirmation();
  32.  
  33.            }
  34.        }
  35.  
  36.         public void order_confirmation()
  37.         {
  38.  
  39.                 DirectoryInfo static1 = new DirectoryInfo(@"C:\Users\Dell\Desktop\SKU");
  40.                 path = static1.FullName + @"\" + year + @"\OrderConfirmation";
  41.  
  42.                 DirectoryInfo dir = new DirectoryInfo(path);
  43.                 //  DirectoryInfo  dir = new DirectoryInfo(@"C:\Users\Dell\Desktop\SKU\2010\OrderConfirmation");
  44.                 FileInfo[] xlsxfiles = dir.GetFiles("*.xlsx", SearchOption.AllDirectories);
  45.                 number = xlsxfiles.Count();
  46.                 //Response.Write("Order Confirmation");
  47.                 Table dynTable = new Table();
  48.                 dynTable.ID = "order_confirmation";
  49.                 dynTable.BorderWidth = 1;
  50.                 //   this.Controls.Add(dynTable);
  51.                 this.form1.Controls.Add(dynTable);
  52.  
  53.                 foreach (FileInfo f in xlsxfiles)
  54.                 {
  55.  
  56.  
  57.                     searchwithin = f.Name;
  58.                     searchfor = sku;
  59.                     match = searchwithin.IndexOf(searchfor);
  60.                     // sku1 = sku + ".xlsx";
  61.                     Label confirm_label = new Label();
  62.                     Label button_perform = new Label();
  63.                     if (match >= 0)
  64.                     {
  65.  
  66.                         confirm_label.Text = f.FullName;
  67.                         order_confirm = f.FullName;
  68.                         TableRow row = new TableRow();
  69.                         row.BorderWidth = 1;
  70.                         TableCell cell1 = new TableCell();
  71.                         cell1.BorderWidth = 1;
  72.                         cell1.Controls.Add(confirm_label);
  73.                         row.Controls.Add(cell1);
  74.                         dynTable.Controls.Add(row);
  75.  
  76.                         TableCell cell2 = new TableCell();
  77.                         cell2.BorderWidth = 1;
  78.  
  79.                         lbn = new LinkButton();
  80.                         lbn.ID = f.FullName;
  81.                         lbn.Text = "Open";
  82.                         lbn.Command += new System.Web.UI.WebControls.CommandEventHandler(btn_open_Click);
  83.  
  84.                         lbn.CommandName = f.FullName;
  85.                         lbn.CommandArgument = f.FullName;
  86.  
  87.                         cell2.Controls.Add(lbn);
  88.                         row.Controls.Add(cell2);
  89.                         dynTable.Controls.Add(row);
  90.  
  91.                         //  this.Controls.Add(new LiteralControl("<br>"));
  92.                          //Response.Write("<br><a href=" + f.Name >" + f.FullName+ "</a>");
  93.  
  94.                     }
  95.                     else
  96.                     {
  97.  
  98.                     }
  99.  
  100.                 }         
  101.  
  102.         }
  103.  
  104.  
  105.         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  106.         {
  107.  
  108.         }
  109.  
  110.         protected void btn_open_Click(object sender,CommandEventArgs e)
  111.         {
  112.  
  113.             //HttpContext.Current.Response.Clear();
  114.             //HttpContext.Current.Response.ClearHeaders();
  115.             //HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + order_confirm);
  116.             //HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  117.             //HttpContext.Current.Response.WriteFile(order_confirm);
  118.             //HttpContext.Current.Response.End();
  119.  
  120.             Response.Write("hello");
  121.  
  122.  
  123.         }
  124.  
  125.  
  126.     }
  127.  
  128. }
  129.  
Sep 7 '10 #1
1 3035
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.
Sep 7 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: simon | last post by:
On code behind file: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim ctrl As New LinkButton ctrl.ID =...
1
by: Gopal Krish | last post by:
I'm have coded a simple menu (using link buttons as menu items) in a user control to be reused across many ASPX pages. In the page_load method I dynamically create the link buttons as follows ...
1
by: Karthick Kumar | last post by:
Hi, I have the following code which displays all the images from a specific folder with a Radio button in it: Dim objFile i = 1 For Each objFile In objFolder.Files If (i = 1) Then...
0
by: Nathan Sokalski | last post by:
I am making a webform that allows the user to add another textbox by clicking a button to allow them to enter however many names they need. The procedure that does this uses information entered in...
2
by: showens | last post by:
I have a table that has rows appended dynamically, based on the item selected from a dropdownlist. The rows have a number of cells, one with a literal control using the text of the dropdownlist,...
0
by: prashantnurvi | last post by:
I have programaticaaly created several Link Buttons on my ASPX page, I would like to raise the Click event of these Link buttons, passing in appropriate parameters depending on the Link button I...
3
by: Mark | last post by:
Assume you want to dynamically add one to many link button controls to a web page dynamically at run time. Each link button needs to post back and execute code. As the link buttons are created at...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.