我需要一个可以容纳图像和两个单选按钮的控件,就像我们在fb / orkut上传图片时那样。 1.图像 2.删除单选按钮。 3.盖上单选按钮。[将此图像设置为专辑封面]
我用这三件事创建了一个用户控件。 在我的aspx页面上单击按钮我需要添加此用户控件。 手段,用户将使用FileUpload选择图像,当他点击按钮时,应加载此用户控件。 我能够加载控件。请检查以下代码。
<code>
<pre lang="cs">
protected void btnAddURL_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//ItemList is an array list used to store the filename.
ItemList.Add(FileUpload1.FileName);
showImage();
}
}</pre>
public void showImage()
{
PlaceHolder p = new PlaceHolder();
//Create Thumbnail
FileUpload1.SaveAs(@"D:\ASP\Project_Dec_16\RealEstate\SaveImage\" + FileUpload1.FileName);
System.Drawing.Image img1 = System.Drawing.Image.FromFile(@"D:\ASP\Project_Dec_16\RealEstate\SaveImage\" + FileUpload1.FileName);
System.Drawing.Image bmp2 = img1.GetThumbnailImage(100, 100, null, IntPtr.Zero);
bmp2.Save(@"D:\ASP\Project_Dec_16\RealEstate\SaveImage\thumbnail\L\" + FileUpload1.FileName);
//Load the images selected by user
for (int i = 0; i <= ItemList.Count - 1; i++)
{
Control MyUserControl;
// Load user control dynamically
MyUserControl = LoadControl("MyControl.ascx");
MyUserControl.ID = "MyUserControl" + cnt++;
// Assign URL
Image MyImage = (Image)MyUserControl.FindControl("Image1");
// MyImage.ID = "Image" + cnt;
MyImage.ImageUrl = "~/SaveImage/thumbnail/L/" + ItemList[i].ToString();
// Add control to panel.
p.Controls.Add(MyUserControl);
Panel2.Controls.Add(p);
</code>
问题: 1 GT;所有图像都出现在新线上,我希望它们彼此相邻。 2 - ;如何检测单击哪个单选按钮,因为我有多个图像,并且都有单选按钮。 3 GT;如何从aspx页面捕获单击单选按钮? 4&GT;如果有其他方法可以实现这一点,请告诉我。
在谷歌搜索但无法找到解决方案。 :( 提前谢谢。
[更改后的代码] 在ascx文件中,我添加了以下代码
public event EventHandler rd_ClickDemo;
protected void deleteimage_CheckedChanged(object sender, EventArgs e)
{
rd_ClickDemo(sender, e);
}
protected void setascover_CheckedChanged(object sender, EventArgs e)
{
rd_ClickDemo(sender, e);
}
In aspx file on click of a button i am doing the following.
protected void btnAddURL_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Add file name to array list
ItemList.Add(FileUpload1.FileName);
//Add The URL in a text box.
txtAddURL.Text = txtAddURL.Text + System.Environment.NewLine + System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
//Image1.ImageUrl = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
//this.Button1_Click(this, e);
showImage();
}
}
public void showImage()
{
PlaceHolder p = new PlaceHolder();
// Create a Thumbnail Image.
FileUpload1.SaveAs(@"D:\ASP\Project_Dec_16\RealEstate\SaveImage\" + FileUpload1.FileName);
System.Drawing.Image img1 = System.Drawing.Image.FromFile(@"D:\ASP\Project_Dec_16\RealEstate\SaveImage\" + FileUpload1.FileName);
System.Drawing.Image bmp2 = img1.GetThumbnailImage(100, 100, null, IntPtr.Zero);
bmp2.Save(@"D:\ASP\Project_Dec_16\RealEstate\SaveImage\thumbnail\L\" + FileUpload1.FileName);
//Load all images from array list.
for (int i = 0; i <= ItemList.Count - 1; i++)
{
// Load user control dynamically
MyUserControl = LoadControl("MyControl.ascx");
MyUserControl.ID = "MyUserControl" + cnt++;
// Find Radio Button
RadioButton rdb1 = (RadioButton)MyUserControl.FindControl("deleteimage");
RadioButton rdb2 = (RadioButton)MyUserControl.FindControl("setascover");
//Attach Group Name.
rdb1.GroupName = "G1" + cnt.ToString();
rdb1.ID = "Rb_ID_D" + cnt.ToString();
rdb1.AutoPostBack = true;
rdb1.CheckedChanged +=new EventHandler(rdb1_CheckedChanged);
//Attach Group Name.
rdb2.GroupName = "G1"+ cnt.ToString();
rdb2.ID = "Rb_ID" + cnt.ToString();
rdb2.AutoPostBack = true;
rdb2.CheckedChanged += new EventHandler(rdb1_CheckedChanged);
//Image MyImage = (Image)MyUserControl.FindControl("Image1");
// MyImage.ID = "Image" + cnt;
//Attach URL to Image.
Image MyImage = (Image)MyUserControl.FindControl("Image1");
MyImage.ImageUrl = "~/SaveImage/thumbnail/L/" + ItemList[i].ToString();
p.Controls.Add(MyUserControl);
Panel2.Controls.Add(p);
}
}
仍然无法触发radiobutton_CheckedChanged事件。 请帮助。
答案 0 :(得分:1)
好的,这是第一个问题的解决方案:
1&GT;所有图像都出现在新线上,我希望它们彼此相邻 Ans:您可以在自定义控件的div块的CSS中执行此操作,只需将float设置为左边
style="display:block;float:right;"
2 - ;如何检测单击哪个单选按钮,因为我有多个图像,并且都有单选按钮。
Ans:你可以获得控制哪个主机你的RadioButton所以你可以使用单选按钮的NamingContainer属性来获取主机控件,在你的情况下必须是你自定义控件。
3&GT;如何从aspx页面捕获单击单选按钮?
答:在您的代码中执行以下操作:
首先循环添加您的自定义控件到Panel2时,将一个组名和一个事件处理程序附加到RadioButton。 (假设您的自定义控件中已经有单选按钮),然后处理CheckedChanged事件(我使用匿名委托处理事件)
RadioButton rdb1 = (RadioButton)MyUserControl.FindControl("deleteimage");
RadioButton rdb2 = (RadioButton)MyUserControl.FindControl("setascover");
rd1.GrouprdoSelect2.GroupName = "radiobutton" + i.ToString() ;
rd1.ID = "radiobutton" + i.ToString() + i.ToString() + Convert.ToString(1);
rd1.AutoPostBack = true;
rd1.CheckedChanged += (thesender, ev) => {
RadioButton rb = (RadioButton) thesender;
MyUserControl mcl = rb.NamingContainer as MyUserControl;
//Perform your task based on the fact of mcl.ID
}
rd2.GrouprdoSelect2.GroupName = "radiobutton" + i.ToString() ;
rd2.ID = "radiobutton" + i.ToString() + i.ToString() + Convert.ToString(2);
rd2.AutoPostBack = true;
rd2.CheckedChanged += (thesender, ev) => {
RadioButton rb = (RadioButton) thesender;
MyUserControl mcl = rb.NamingContainer as MyUserControl;
//Perform your task based on the fact of mcl.ID
}