Search code examples
c#umbraco

Get Media ID dynamically


I am currently working in a project with c# and umbraco CMS. And now im facing some issues one of them is that I don't know how to get the media ID dynamically, please take a look ID COMES FROM UMBRACO

Media file = new Media(3557);

string url = file.getProperty("umbracoFile").Value.ToString();
string teste = file.getProperty("impressions").Value.ToString();
if (teste == "" || teste == null) { teste = "0"; }
int count= Convert.ToInt32(teste);
file.getProperty("impressions").Value = count+1;

file.Save();

Do you see that 1st line ? Media file= new media(id)? I want to get this id dynamically and I will explain why. I have this handler in order to get the banner clicks on the site. I have 4 images and I want to have a count for how many times the client clicks on them. So for that I can't have the id = 3557 , I need to get the id of the image dynamically.


Solution

  • I assume you are using an event handler, something like:

    protected void imgMyMedia_OnClicked(object sender, EventArgs e) { your code }

    In that case, you can cast the sender object to your control type, and read any parameter set there. I recon it would be something like this:

    MediaControl myMedia = (MediaControl)sender; int ID = myMedia.MediaId;

    or something similar. I am not familiar with the umbraco code and can not be as precise, but this should be about right.