Get Media ID dynamically - c#

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.

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.

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:
<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.

Related

How to click browser and input into textbox?

How I can click buttons with ids or anything other, input to text box string etc. I know in windows form is easy with getelementbyid. But in WPF I cant find anything. I know how get source to string but I cant make click. Any ideas how do this or is even possible? I can get list of Ids from source +regex. Or is there too something I can get simply list? Need something like this:
HtmlElement button = webBrowser1.Document.GetElementById("lButtonSearch");
button.Click += new HtmlElementEventHandler(GotoSearchPage);
I can do something like this but what next, how display it?
System.Windows.Forms.WebBrowser weba = newSystem.Windows.Forms.WebBrowser();
weba.Navigate(new Uri("www.google.com"));
string testowo = "btnI";
System.Windows.Forms.HtmlElement htmlElement = weba.Document.GetElementById(testowo);
htmlElement.InvokeMember("click");
How now convert it to display lets say WebBrowser id is =browserwindows
browserwindow=weba
wont work
Try this:
var doc = webBrowser1.Document as IHTMLDocument2;
var button = doc.all.OfType<IHTMLInputElement>().FirstOrDefault(b => b.name == "btnG");
if(button != null)
{
((IHTMLElement)button).click();
}

Couldn't assign value in FileUpload tool

I grabbed the value from database and I am trying to assign those values in Edit Form. But the only problem is with the FileUpload. It don't take the value. Can anyone suggest me what I'm missing here
private void EditForDataByID(int TitleId)
{
ReadmoreController objFormController = new ReadmoreController();
ReadMoreInfo objInfo = objFormController.GetListObjectOfAllArticle(TitleId);
if (objInfo != null)
{
TextTitle.Text = objInfo.Title;
txtSummary.Text = objInfo.Summary;
TextDate.Text = objInfo.Date.ToString();
//FileUpload1.FileName=objInfo.Image; I even tried this but it doesn't work
FileUpload1 = objInfo.Image;
Session["TitleId"] = TitleId;
ListDiv.Visible = false;
form.Visible = true;
BindGrid();
}
}
For client security reason you can not assign value to FileUploadControl as it could cause the uploading of unwanted files from client machine. So let the use pick the file to upload.
If it is allowed then one can steel the important files from client machine like c:\PersonalPasswords
Edit Based on comments
If you need to ensure that user has selected an Image and does not need to change it then you can use a image control and assign image to it. Use the same image control to find if the image is selected or not.

Select one string line in a list

I'm trying to simply select one string line out a long list strings that are held on a server and seperated with a pipe character. This string is grabbed by a php script and the string line is a list of all the media and folders I have on my server.
In my code I'm getting this information and returning it with the following code:
using (var client = new WebClient())
{
result = client.DownloadString("http://server.foo.com/images/getDirectoryList.php");
}
textBox1.Text = string.Join(Environment.NewLine, result.Split('|'));
And it looks like this:
But when I try to simply click on one of them, my cursor simply just goes to where I've clicked. Like this, I tried to select md-harrier.jpg and my cursor just ends up at the end of jpg:
What I'm really wanting is pictured below. I click on Koala.jpg and the whole thing is highlighted and I have the ability to store the name of what it is I've just clicked on. TO achieve that screen shot I had to click next to Koala.jpg and then drag my mouse along.
Is there anyway I can achieve what I want to achieve?
The key thing to note about this is that I will have no idea how many files will be on the server, nor what they will be called. My php script is grabbing this information and displaying it in my winform text box using the code I have wrote above.
as Simon said you need a ListBox, a ListBox fits here because it allows you to select a line, and you can register to the event of SelectedIndexChanged and store the name that was selected.
to initiate the values do
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
listBox1.Items.AddRange(result.Split('|'));
listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
and on the selectedItemChanged:
string currVal;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
currVal = (string)listBox1.SelectedItem;
}
As you said you have no reason to use TextBox,then by using ListBox you can achieve that in this way;
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
string[] names=result.Split('|');
foreach(string name in names)
{
if(name!="|"&&name!=" ")
{
listbox.Items.Add(name);
}
}
Additionally,if you would like to store selected item in a variable subscribe to ListBox's SelectionChangedEvent and store the selection index in a variable in this way;
int selection=;
private void ListBox1_SelectionIndexChanged(object sender,EventArgs e)
{
selection=ListBox1.SelectedIndex;
}

Image name as string, use it to make image visible WPF with C#

I have an image name as a string. The real imagename on the form is called "image". So i get something like this:
image.Visibility = Visibility.Hidden;
string imageName = "image";
// need something here to make it usable...
changedImageName.Visibility = Visibility.Visible;
Now, a string can not be used in combination with the Visibility property.
I cant really find what i must make the string to, to make it usable for the visibility property.
If i see this page: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx
Do I understand correct that I make it a "enum" ? And if yes, how do I get a string to that property?
EDIT 1
I see I have not been explaining it proper enough.
I forgot to mention I am using a WPF form.
on this form, I have put an image.
In the initialize part, the image get set to hidden.
so for example the imagename I named "Image"
so I use image.Visibility = Visibility.Hidden;
later on in my code, I want to make the image visible again, depending on what the user does.
so, instead if just using the name to get the image visible again, I want to use a string.
this string is looking exactly as the name of the image.
but i cant use the string in combination with the Visibility function.
but i cant find anywhere what i must make this string to, to be able to use that visibility option on it.
hope i explained a bit better now :).
Later on, i will have multiple images on the WPF window.
So the key is that i will use the string, that is corresponding with the name of the image.
Depending on what the user has input into the string, some image will or will not show.
EDIT 2
If you have:
String theName = ImageName.name
you can get the name of the image into a string, so you can do stuff with it.
i am looking for a way to do the exact opposite of this. So i want to go from a string, to that name, so after that i can use this to control the image again.
Edit 3
some example:
private void theButton_Click(object sender, RoutedEventArgs e)
{
//get the Name property of the button
Button b = sender as Button;
string s = b.Name;
MessageBox.Show("this is the name of the clicked button: " + s);
//the name of the image to unhide, is the exact same as the button, only with IM in front so:
string IM = "IM";
IM += s;
MessageBox.Show("this string, is now indentical to the name of the image i want to unhide, so this string now looks like: " + IM );
// now, this wont work, because i cant use a string for this, although the string value looks exactly like the image .name property
// so string IM = IMtheButton
// the image to unhide is named: IMtheButton.name
IM.Visibility = Visibility.Visible;
}
looks like you're using WPF, so you can create a boolean to visibility converter and use it with a boolean (and create a method that receives string if necessary) and just use:
<ContentControl Visibility="{Binding Path=IsControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"></ContentControl>
or any other converter...
check this links:
http://bembengarifin.wordpress.com/2009/08/12/setting-visibility-of-wpf-control-through-binding/
http://andu-goes-west.blogspot.com/2009/05/wpf-boolean-to-visibility-converter.html
http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx
EDIT 1:
so then you will have to iterate over the images and check if your string is equals to name of the Image class.
something like this (not tested):
foreach (Image img in container.Items)
{
if img.Name == yourMagicallyString;
{
img.Visibility = Visibility.Visible;
}
else
{
img.Visibility = Visibility.Hidden;
}
}
If I understand correctly, you are trying to find a control based on the name or ID of the control. If so, try this:
Control changedImage = this.Controls.Find("image", false)[0];
Depending on what you are targeting and what version you might need to tweak a little
EDIT Updated per #Alexander Galkin comments about Find returning an array. There should definitely be some checking and whatnot but I'm leaving that up to the OP.
EDIT 2 For finding a control by name in WPF see this post.
The code I was looking for:
object item = FindName(IM);
Image test1 = (Image)item;
test1.Visibility = Visibility.Visible;

silverlight, ListBox navigation to new page with object possible?

Hi all i have a listbox MainListBox where i add items to dynamically.
Now i want to navigate to DetialsPage.xaml.cs when i choose an item in the listbox.
where i can then display my info about the selected item.
private void SetListBox()
{
foreach (ToDoItem todo in itemList)
{
MainListBox.Items.Add(todo.ToDoName);
}
}
MainListBox_SelectionChanged ("Generated by visual studio 2010 silverlight for windows 7 phone)
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
in DetailsPage.xaml.cs is the next method. ("Generated by visual studio 2010 silverlight for windows 7 phone)
I'm aware that the below method does not do what i try.
// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
}
}
I would like to access the selectedIndex and call my methods of my object that is in the MainListbox
so Basicly:
Mainlistbox => select item => send that item to details page => details page access the item and call methods on the item (object)
I'm sure this is a basic question tough it seems hard to find any specifics on it. i would like to add that this is my first windows phone 7 app.
There are many ways you can pass an object from page to page:
serialize and deserialize like Dennis said, but this, although feasable, is not practical, unless you want to save the object in isolated storage and retrieve it later.
Place an object in the App.cs class, which is accessible to all pages. Set your object in the master page, retrieve it from the Details page.
Code to put in App.cs: MyObject selectedObject;
Code to put in MasterPage.cs: application.selectedObject = MainListBox.selectedItem;
Code to put in DetailsPage.cs: MyObject selectedObject = application.seletedObject;
You can set the Object in the DataContext of your LayoutRoot, but i don't have the code for that on top of my head.
The answer here is simple - you cannot directly pass an object to another page. You can serialize it to JSON or XML and then deserialize it on the target page, but the serialized item will still have to be passed as a parameter.
Instead of sending the selectedindex as a query string parameter you could send the ID for the object or similar, something that uniquely can identify the object.
Then in the details page you could fetch the correct object from the same datasource that the main list box get its data from (in your case "itemList" which could come from e.g. IsolatedStorage).
If itemList is instantiated and kept only within the main page then you won't be able to fetch the item by ID from the details page. So in that case you'd need to move the itemList to some static or app level storage.
HTH

Categories

Resources