I am trying to select items from listbox 1 on mainpage, click a button called add and send the items to a new page. But i am dont think my method in doing so is right.
MainPage.xaml.cs
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
List<Names> tmp = new List<Names>();
foreach(var names in lstNames.SelectedItems)
{
tmp.Add(names);
}
lstNames.ItemsSource = tmp;
Frame.Navigate(typeof(Page2), tmp);
}
Page2.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var tmp = (Names)e.Parameter;
lstNames2.ItemsSource = tmp;
}
}
Any guidance is appreciated on how I do this.
The object you pass as the second parameter ends up in the ExtraData property of the NavigationEventArgs.
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
var tmp = lstNames.SelectedItems.ToList();
Frame.Navigate(typeof(Page2), tmp);
}
I'm using Linq for the ToList() there.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
lstNames2.ItemsSource = e.ExtraData as List<Name>;
}
I recommend you look at MVVM though.
The way I would do this is an approach called viewmodel first.
I'd use a contentcontrol rather than a frame and have usercontrols rather than pages in it.
I'd use templating to template a viewmodel into a page-equivalent usercontrol.
Passing the list would be into the constructor of the viewmodel I "navigate" to.
It's a standard wpf technique you should be able to easily google.
Related
I have a big numbers of labels custom controls let's say 100.
I would like to give them an MouseHover event.
I could do something like:
private void label_custom1_MouseHover(object sender, EventArgs e)
{
TextBox.Text = label_custom1.backcolor.ToString();
}
But then I would need to do that 100 times. Since I have 100 of them.
Is there a way to do that only once?
I guess I should probably declare the function in my custom_label class but so far I couldn't make it work.
Any Idea how to proceed?
Create only one event handler method like this:
private void Label_MouseHover(object sender, EventArgs e)
{
TextBox.Text = (sender as Label).BackColor.ToString();
}
And subscribe all Events to that method:
this.label_custom1.MouseHover += Label_MouseHover;
this.label_custom2.MouseHover += Label_MouseHover;
Thank you, this was very helpful.
This part didnt work though, it couldnt recognize the controls:
private void SetEventAllLabels()
{
var labels = Controls.OfType<Label>().Where(x => x.Name.StartsWith("label"));
foreach (var label in labels)
{
label.MouseHover += Common_MouseHover;
}
}
So I create a list containing all the label names and Then it works using this:
foreach (string i in liste)
{
CustomLabel x = (CustomLabel)(this.Controls.Find(i, true).FirstOrDefault() as Label);
x.MouseEnter += Common_MouseHover;
}
As a side note, using MouseEnter instead of MouseHover makes it much more responsive (faster reaction!)
Firstly, create a common event, we receive the Label posted here.
private void Common_MouseHover(object sender, EventArgs e)
{
TextBox.Text = (sender as Label).BackColor.ToString();
}
Give the common event to all Labels on the form. Here I assume Label names start with label, for example label1, label2, label3 ...
private void SetEventAllLabels()
{
var labels = Controls.OfType<Label>().Where(x => x.Name.StartsWith("label"));
foreach (var label in labels)
{
label.MouseHover += Common_MouseHover;
}
}
Call the SetEventAllLabels () method in the Load method of the form.
private void Form1_Load(object sender, EventArgs e)
{
SetEventAllLabels();
}
In the Devexpress ToolTipControllerGetActiveObjectInfoEventArgs Event there is a parameter passed to the function.
There is a SelectedControl member variable which points to the DevEx grid control object.
From here I want can get the active GridView (this is because I have several grids coming in here).
Can someone give me some sample code to get from the SelectedControl to the GridView?
private void MyToolTipController_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
If you want to get the view under the mouse point then you can use GridControl.GetViewAt method.
Here is example:
private void MyToolTipController_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
var gridControl = e.SelectedControl as GridControl;
if (gridControl != null)
{
var view = gridControl.GetViewAt(e.ControlMousePosition);
//Your code here.
}
}
Also, if you want to get the focused view then you can use GridControl.FocusedView property.
Here is example:
private void MyToolTipController_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
var gridControl = e.SelectedControl as GridControl;
if (gridControl != null)
{
var view = gridControl.FocusedView;
//Your code here.
}
}
I want to use this solution to convert URLs to link in a listview label.
private string ConvertUrlsToLinks(string text)
{
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return r.Replace(text, "$1").Replace("href=\"www", "href=\"http://www");
}
I have tried this in the listview databound but its not working.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label ProjectPostLabel = (Label)e.Item.FindControl("ProjectPostLabel");
ProjectPostLabel = ConvertUrlsToLinks({0});
}
Thank you
According to your code you are using ASP.NET Web Forms, not MVC.
You must use your ProjectPostLabel instance with Text property - no need to create a new label that is not assign to anywhere.
From your event you must retrieve Url property, not the label control. I have used NorthwindEmployee class with URL property in my ListView. You must cast it to your own class that is used in the list view.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL);
}
And you must remember that only the last item from your list view will be displayed in the label (unless you expect that behavior). If you want to list of URLs from the list you can write this:
protected void Page_Load(object sender, EventArgs e)
{
ProjectPostLabel.Text = string.Empty;
}
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
}
Suppose there is a user control in a page called Paging.ascx that is embedded in PageWithResults.aspx. This control has the necessary properties to keep track of various details about what page you're on (ie: CurrentPage, TotalRecordsInResults, RecordsPerPage, etc..). It also contains events that fire off when you click on a hyperlink ("next page" or "previous page"). Example below. I need to tell PageWithResults.aspx that one of these LinkButton web controls was clicked. I think I need to assign a delegate in the page, so that when this user control event is called (hyperlink is clicked), it also calls some other method/event in the page class. That way I can quickly check what the new value of CurrentPage is (based on what was called in the event below) and get a new result set for the new page (based on the CurrentPage property). But I'm not sure of the best approach. I'm thinking this will require a delegate, but I'm not sure how to wire it up. If you need more details, please ask.
protected void btnNext_Click(object sender, EventArgs e)
{
this.CurrentPage = this.CurrentPage + 1;
if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
}
I'm thinking I have to put my delegate here somewhere. ??
protected void btnNext_Click(object sender, EventArgs e)
{
this.CurrentPage = this.CurrentPage + 1;
if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
//delegate to call object.method or something
}
Using an event would work fine.
You would create the event within your UserControl like so:
public event EventHandler ButtonClicked;
Then invoke the event when required:
protected void Button1_Click(object sender, EventArgs e)
{
if (ButtonClicked != null)
ButtonClicked(this, new EventArgs());
}
In your page you would need to assign an event handler:
protected void Page_Load(object sender, EventArgs e)
{
UserControl1.ButtonClicked += new EventHandler(UserControl1_ButtonClicked);
}
void UserControl1_ButtonClicked(object sender, EventArgs e)
{
}
As well as using the above approach you can also cast the Page reference in the UserControl and call a public method directly:
MyPage page = (MyPage)Page;
page.AMethod();
Hope this helps.
Here's the problem:
I have an (almost) empty aspx page and I want to insert a certain number of the same user control in this page.
The fact is that I don't know how many of them I will need.
I tried to add them from the CodeBehind but it seems that the UserControls are completely empty.
In the main page (MainDiv is a div with runat="server"):
protected void Page_Init(object sender, EventArgs e)
{
WebUserControl1 uc = new WebUserControl1();
WebUserControl1 uc1 = new WebUserControl1();
MainDiv.Controls.Add(uc);
MainDiv.Controls.Add(uc1);
}
(it doesn't work if i put this code on Page_Init, Page_Load or Page_PreRender)
UserControl (gw is a Gridview contained in the UserControl):
protected void Page_PreRender(object sender, EventArgs e)
{
if (_data != null)
{
gw.DataSource = _data;
gw.DataBind();
}
}
when I arrive there, gw is null (this.Controls.Count is 0).
How can I solve this?
That's what the repeater control is for.