Click here to Skip to main content
15,799,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am really new to ASP and C#. I have a User control which has two Radio Buttons [Delete, Cover], and an Image control. I want to load this control dynamically on the click of a button and at the same time give ImageURL to the image control. FileUpload is on the aspx page. Can anyone help me? :confused:

C#
public partial class ImageDynamic : System.Web.UI.Page
{
    protected void Button2_Click(object sender, EventArgs e)
     {
        Control MyUserControl = LoadControl("MyControl.ascx");
        PlaceHolder1.Controls.Add(MyUserControl);
    }
}


Using the above lines of code I was able to load the user control. Now how do I set the ImageURL?
Posted
Updated 27-Jan-11 5:24am
v3
Comments
ubaidh sayed 27-Jan-11 10:37am    
Hi Ryan Zahra,
That worked.
Now i have a image and two radio buttons.
1> delete [If i click on this radio button this image should be deleted]
2> cover [if i click on this radio button this image should be set as the cover page of my album]
3> image.

What logic shall i follow to achieve this??
Ryan Zahra 27-Jan-11 11:00am    
Hi Ubaidh Sayed,

You must call the Check Changed event of the radio buttons. Then check if the radio button is checked, and then execute your code.

protected void rbDelete_CheckedChanged(object sender, EventArgs e)
{
if (rbDelete.Checked == true)
{
System.IO.File.Delete("ImagePath");
}
}

protected void rbCover_CheckedChanged(object sender, EventArgs e)
{
if (rbCover.Checked == true)
{
//don't know what you mean by setting image as cover page but this is where you code must be placed
}
}
ubaidh sayed 31-Jan-11 2:03am    
Hi Ryan Zahra,
Image as cover means, when u add an image on face book or orkut you select a radiobutton as cover and that image is set as a cover of your album i wanna do the same.

I have created a user control which has a image, delete radio button and cover radio button.
I am adding this control dynamically at runtime. I am able to add the control at runtime.
1> As i am adding more than one usercontrol at runtime, all images contain delete and cover radio button. Nw problem is as Album can have only one cover so the user should be able to select only one cover radio button from all the images which is not the case in mine.
Here is the code.

protected void btnAddURL_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Store file Name in arraylist for further reference.
ItemList.Add(FileUpload1.FileName);

// Add URL in a text box[Just to display]
txtAddURL.Text = txtAddURL.Text + System.Environment.NewLine + System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);

// 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);
showImage();
}
}

public void showImage()
{

PlaceHolder p = new PlaceHolder();
p.Controls.Clear();
Panel2.Controls.Clear();

for (int i = 0; i <= ItemList.Count - 1; i++)
{

// Load user control dynamically
MyUserControl = LoadControl("MyControl.ascx");

MyUserControl.ID = "MyUserControl" + i.ToString() ;

// Find Radio Button
RadioButton rdb1 = (RadioButton)MyUserControl.FindControl("deleteimage");
RadioButton rdb2 = (RadioButton)MyUserControl.FindControl("setascover");
// Attach Group Name.
rdb1.GroupName = "G";
rdb1.ID = "Rb_ID_D" + i.ToString();
rdb1.AutoPostBack = false;
rdb1.CheckedChanged +=new EventHandler(rdb1_CheckedChanged);

//Attach Group Name.
rdb2.GroupName = "G";
rdb2.ID = "Rb_ID_C" + i.ToString();
rdb2.AutoPostBack = false;
rdb2.CheckedChanged += new EventHandler(rdb2_CheckedChanged);

//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);
}
}
Please help or suggest a different way to do so.

Hi

You can use FindControl method in the user control to find the Image Control.

Like MyUserControl.FindControl("Your Image Control Id")

Thanks
Hope this helps
 
Share this answer
 
Image MyImage = (Image)MyUserControl.FindControl("ImageControlID");
MyImage.ImageUrl = "ImagePath";
 
Share this answer
 

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