Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

I try to bind image in runtime button click event in asp.net with c#. Could you please anyone help and where am dmistake on my code.

Code Behind:

C#
protected void Button1_Click(object sender, EventArgs e)
{
    //if (FileUpload1.HasFile)
    //{
    //    string fileName = FileUpload1.FileName;
    //    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
    //}
    LoadImages();
//    Response.Redirect("~/TestApp.aspx");
}

private void LoadImages()
{
    string filepath = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\";
  //  string filepath = "~/Images";
    string[] fileEntries = Directory.GetFiles(filepath, TextBox1.Text + "*.jpg", SearchOption.TopDirectoryOnly);
    foreach (string filename in fileEntries)
    {
        ImageButton imagebutton = new ImageButton();
        FileInfo fi = new FileInfo(filename);
        imagebutton.ImageUrl = filepath + fi.Name;
        imagebutton.AlternateText = fi.Name;
        imagebutton.Height = Unit.Pixel(100);
        imagebutton.Style.Add("padding", "5px");
        imagebutton.Width = Unit.Pixel(100);
        imagebutton.Click += new ImageClickEventHandler(imageButton_Click);
        imagebutton.Visible = true;
        this.Controls.Add(imagebutton);

        Panel1.Controls.Add(imagebutton);
    }

    //foreach (string strfile in Directory.GetFiles(Server.MapPath("~/Images")))
    //{
    //    ImageButton imageButton = new ImageButton();
    //    FileInfo fi = new FileInfo(strfile);
    //    imageButton.ImageUrl = "~/Images/" + fi.Name;
    //    imageButton.Height = Unit.Pixel(100);
    //    imageButton.Style.Add("padding", "5px");
    //    imageButton.Width = Unit.Pixel(100);
    //    imageButton.Click += new ImageClickEventHandler(imageButton_Click);
    //    Panel1.Controls.Add(imageButton);
    //}
}

protected void imageButton_Click(object sender, ImageClickEventArgs e)
{
    Response.Redirect("WebForm1.aspx?ImageURL=" + ((ImageButton)sender).ImageUrl);
}

Design Code:
XML
<form id="form1" runat="server">
    <div>

        <%--<asp:Label ID="Label1" runat="server" Text="FileNames"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>--%>
        <%--<asp:FileUpload ID="FileUpload1" runat="server" />--%>
        <asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="imagebutton_Click" runat="server" Text="Upload" OnClick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server" Width="440px"
    BorderStyle="Dotted" BorderColor="#000066">
    </asp:Panel>
    </div>
    </form>


Thanks & Regards,

IsaiSelvan. S
98408-46643
Posted
v2
Comments
Can you debug and check if you get anything for fileEntries or not?
IsaiSelvan 21-Nov-13 1:30am    
yes am debug on this code. the image path will be bind in variable. but i cant to show on runtime event.

1 solution

In this problem may be the image path

user Server.MapPath to assign the image path.
and one more thing put the image folder in your project and then assign the path

suppose you have a image->userimage folder

string filepath="images/userimage";

string[] fileEntries = Directory.GetFiles(Server.MapPath(filepath), TextBox1.Text + "*.jpg", SearchOption.TopDirectoryOnly);
 
Share this answer
 
Comments
IsaiSelvan 21-Nov-13 1:45am    
Ok vinay. But i need to bind the images from local drive not for the project solution image folder. Is this possible to bind the images from local drive?
[no name] 21-Nov-13 2:10am    
No you can not do this . if you want on that case create a virtual directory of you image folder. and then use
[no name] 21-Nov-13 2:12am    
i tried but i get this error

Exception Details: System.Web.HttpException: 'D:/vinay/Code Share/CITIS/portal/images/citsLogo.jpg' is a physical path, but a virtual path was expected.
IsaiSelvan 25-Nov-13 2:04am    
I am also getting same error this code. Do you know why this error came on runtime?
[no name] 25-Nov-13 22:46pm    
Hi i have got a solution on code project have a look of this solution. it may help you..
http://www.codeproject.com/Questions/508243/aplusphysicalpluspath-2cplusbutplusaplusvirtualplu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900