using C# solution for Listview label - c#

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));
}

Related

I want to change the background color of panel by choosing the colors from dropdown list

I am writing code which lets user to choose the color of background of page(drop down list) and the problem is that i got the known colors from library System.Drawing and i don't know how to get that values from list. how i can do it?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] colorArray = Enum.GetNames(typeof(KnownColor));
drpBack.DataSource = colorArray;
drpBack.DataBind();}
// page load where i took the names of colors;
//Here is the button update
protected void btnUpdate_Click(object sender, EventArgs e){
string back = drpBack.Text;
Color style = (Color)Enum.Parse(typeof(Color), back);
pnlCard.BackColor = style;}
When i click button update it gives me error : Type provided must be an Enum.
I tried to write instead of Color - KnownColor it gave me message: Can not implicitly convert type "KnownColor" to "Color"
You should parse the input back to KnownColor. Then you can create a Color from this value using Color.FromKnownColor():
protected void btnUpdate_Click(object sender, EventArgs e){
string back = drpBack.Text;
var parsedColor = (KnownColor)Enum.Parse(typeof(KnownColor), back);
var style = Color.FromKnownColor(parsedColor);
pnlCard.BackColor = style;
}

Send listbox selecteditems from one wpf page to another

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.

ASP.NET ImageButton Click event

I have a dynamic ImageButton and I want it to trigger a method when it's clicked. I have the following code, which unfortunatelly it's not working.
ImageButton imb = new ImageButton();
imb.Attributes.Add("runat", "server");
imb.Click += new ImageClickEventHandler(ImageButton1_Click);
imb.ID = "ID";
imb.ImageUrl = "link to image";
Panel1.Controls.Add(imb);
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//it should go here
}
you must add your ImageButton to a suitable continer.
e.g:
form1.Controls.Add(Imb);
/// form1 must have runat=server
you can find useful tips at here.
Adding dynamic controls to the page is a tricky matter. Unfortunately, you cannot just declare an object of WebControl derived type and expect it to work.
In ASP.NET WebForms, there is a concept called Page Lifecycle. It is a broad matter, but my point is that there is a particular order you must obey to plug-in the dynamic control into the page. A practical example follows.
Start with dynamic control declaration at the class level.
protected ImageButton imb = null;
Then you initialize it durring the page initialization. One possibility is to handle the PreInit event.
protected void Page_PreInit(object sender, EventArgs e)
{
imb = new ImageButton()
{
ID = "ID",
ImageUrl = "link to image"
};
imb.Click += Imb_Click;
Panel1.Controls.Add(imb);
}
private void Imb_Click(object sender, ImageClickEventArgs e)
{
// Do your stuff
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set initial properties as appropriate. E.g.
imb.AlternateText = "...";
}
}
You're good to go.

ComboBox.SelectedItem not working as expected C# asp.net

I have a combo box I added items to in the page load method and within a button click I am trying to set either the text that is typed or the item selected from the dropdown. I am using ComboBox.SelectedItem.ToString() which works when something is typed in but once the user selects something from dropdown the string is empty that is passed through.
protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.GetCurrent(Page) == null)
{
Page.Form.Controls.AddAt(0, new ScriptManager());
}
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value1);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value2);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value3);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value4);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value5);
}
protected void ChangeAppSettings_Click(object sender, EventArgs e)
{
string FOPath = FODirectory.Text;
string PolicyStoreName = DiffComboBoxUrls.SelectedItem.ToString(); //this comes back as "" when an item is selected and works when something is typed in
.....
}

How to insert hyperlinks in ListView

I am trying to make Link Web / link info directory in C# where a user can save his links.
The following is my code:
private void Form1_Load(object sender, EventArgs e)
{
Int i=0;
listView1.View = View.Details;
listView1.GridLines = true;
listView1.Columns.Add("Links",250,HorizontalAlignment.Center );
listView1.Columns.Add("Name", 250, HorizontalAlignment.Center);
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Add(textbox1.text);
listview1.Items[i].subitems.add("textbox2.text")
}
textbox2 contains the hyperlink but when I insert it displays as text, not as a hyperlink.
Use ObjectListView -- an open source wrapper around a standard ListView. It supports links directly:
Also have a look at the DataGridView control which has support for LinkLabel.
Using this control, you get all the functionality of the details view in a ListView, but with more customisation per row.
Also you can set one property to true --> listView1.HotTracking = true; You code will look like this:
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Add(textbox1.Text);
listView1.HotTracking = true;
listView1.Items[i].SubItems.Add("hyperlynk2.text");
}

Categories