简体   繁体   English

动态获取媒体ID

[英]Get Media ID dynamically

I am currently working in a project with c# and umbraco CMS. 我目前正在使用C#和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 现在我遇到了一些问题,其中之一是我不知道如何动态获取媒体ID,请看一下ID来自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)? 媒体文件=新媒体(id)? I want to get this id dynamically and I will explain why. 我想动态获取此ID,然后解释原因。 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. 我有4张图片,我想统计一下客户点击它们的次数。 So for that I can't have the id = 3557 , I need to get the id of the image dynamically. 因此,我不能拥有id = 3557,我需要动态获取图像的id。

When you render the image in the first place you want to put the image id in a data attribute so your html will be something like: 当您首先渲染图像时,您想将图像ID放在data属性中,以便html如下所示:

<img src="/media/someimage.jpg" data-id="someMediaIdHere" alt="alt" title="title" />

Then use that when your javascript picks up the onclick event, then you can pass the image id to the server side and do your tracking. 然后在您的javascript拾取onclick事件时使用它,然后可以将图像ID传递到服务器端并进行跟踪。

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. 在这种情况下,您可以将sender对象转换为控件类型,并读取其中设置的任何参数。 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. 我对umbraco代码不熟悉,可能不够精确,但这应该是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM