本文介绍了在运行时中绑定图像按钮单击面板中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我尝试使用c#在asp.net中的运行时按钮点击事件中绑定图像。你可以请任何人帮忙,我的代码在哪里。
代码背后:
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:
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:
<form id="form1" runat="server">
<div>
<%--<asp:Label ID="Label1" runat="server" Text="FileNames"></asp:Label>
<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>
谢谢&此致,
IsaiSelvan。 S $ / $
98408-46643
Thanks & Regards,
IsaiSelvan. S
98408-46643
推荐答案
这篇关于在运行时中绑定图像按钮单击面板中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!