Retrieve SharePoint List Data and bind this to a dropdownlist - drop-down-menu

I'm fairly new to SharePoint so apologies in advance for sounding like a 'Newbie'.
I have created a simple Webpart, which uses a Web User Control - [.ascx file] to provide all the controls for the Webpart. On the .ascx file, there is a DropDownList which is hard-coded at the moment and works well in the Webpart (within a SharePoint site).
However, I want the DropDownList on the .ascx file to be bound to a particular Column of a SharePoint List, so that when I update that column of the SharePoint List, the DropDownList reflects the update automatically.
Do any of you kind folk have any ideas on how to achieve this please?
Thank you very much in advance,
Ash 8-)
(p.s. Happy New Year to you All !)

I found the answer within minutes of posting the above article (typical).
The solution is to place the following code in the Page_Load event of the .ascx.cs (code-behind) file:
if (!Page.IsPostBack)
{
using (SPSite site = new SPSite("http://yoursharepointsite"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["NameOfYourList"];
dropSite.DataSource = list.Items;
dropSite.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
dropSite.DataTextField = "Title"; // List field holding name to be displayed on page
dropSite.DataBind();
}
}
}
I found the solution here:
http://blogs.msdn.com/mattlind/archive/2008/02/12/bind-a-asp-dropdownlist-to-a-sharepoint-list.aspx
Thanks,
Ash

Related

Increment Buttons with 1 when clicking wix code

i am new with wix code so i need to know how can i use buttons that will increment text with 1 when the user click and then i need the number of clicks to be stored in database and then maker this number the same when the page is reloaded.
thanks in advance.
var num = 0;
$w.onReady(function () {
$w("#text2").text= num.toString();
$w('#button1').onClick(function (){
$w("#button1").link = "http://wix.com";
num ++;
$w("#text2").text= num.toString();
$w("#dataset1").setFieldValue("number", $w("#text2").text);
$w("#dataset1").save();
});
});
Rajaa,
is the field $w(“#text2”) connected to your dataset using the Wix Editor connection panel?
The answer to this question depends on how your page elements are wired up. If they are then you need to be careful what you change in code and when because the javascript you write might be impacted by the Wix Dataset engine working automatically due to the element binding.

Retain Enum values in sharepoint web part on update-spsolution

We built a visual web part with an enum as a custom property to allow a user to select a theme for a button. The theme list is alphabetical.
We now have new themes to add to the web part, but if we just update the enum list of themes adding new items between the existing values in the web part, anyone who is using this web part may get a different theme based on the enum value.
I've posted the code below, but I guess I'm looking for advice on how to best update a solution and keep the existing values for people using the web part.
Existing
public enum ButtonThemes
//these choices link to images in the /_layouts/images/MWHCallToAction directory eg Brown.jpg the any new images choices should be added here and the actual images uploaded to the server.
{
None,
Announcement,
ApprovedSoftware,
ComputerMouse
}
New Enum could be
public enum ButtonThemes
//these choices link to images in the /_layouts/images/MWHCallToAction directory eg Brown.jpg the any new images choices should be added here and the actual images uploaded to the server.
{
None,
Announcement,
Attention,
ApprovedSoftware,
ComputerMouse,
DollarSign,
Email,
HelpDesk
}
//button theme (the type of image shown to teh left of the grey button text)
[DefaultValue(ButtonThemes.Announcement)]
[Description("Select the theme for the button.")]
[WebBrowsable(true)]
[Personalizable(PersonalizationScope.Shared)]
[Category("Button Properties")]
public ButtonThemes ButtonTheme
{
get;
set;
}
In this example if someone is using the Approved Software theme - but we add 'Attention' before it,then their theme could automatically be 'Attention' in the web part when we update the solution.
We could add the new values after the current ones, but looking for something more elegant.
Cheers
Rich
fixed it.
Assign a value to each enum item and its all ok so
None,
Announcement = 1,
ApprovedSoftware = 2,
Attention = 4,
ComputerMouse = 3
Hope this helps.

Regarding MVC3 master page and user control

Regarding MVC3 MASTER PAGE ISSUE
I have one master page and inside that master page i have used one usercontrol.
Now inside that usercontrol I have bind out one dropdown list from database from actionresult.
Now the problem is that whenever I use another view i have to call that action result for binding that dropdown list again and again in all other pages which include my master page.
How could I prevent this from happening?
Remember my friends, I am using MVC3.
As per your question, I understand that you want do not want to make DB call on your execution of each view as you included Dropdown in masterpage.
To overcome this problem, you need to implement ouputcache for that usercontrol (partialpage). Please find more details here. Do let me know if you still have any query.
If you don't need to check data from DB then make a static dictionary and bind it to the DropDownListFor:
private static readonly Dictionary<int, string> yourData
= new Dictionary<int, string>
{
{ "1", "Your data one" },
{ "2", "Your data two" }
};
Regards

Create product compare page like in e-commerce site

I was creating a website(in PHP) which shows information about various Mobile Phones(its not an e-commerce site, but an Informative site), I want to create a feature to compare the selected product(i.e Mobile phones). This feature is seen in most e-commerce sites.
I just want to know the Algorithm to create this functionality and not the code. I am not using any e-commerce application like prestashop, or any CMS to build this web application, only PHP(PHP, MySQL).
** This is C# Code ** Hope you can use the logic for PHP **
Step #1 – Create a products listing page, for each product add a CheckBox field.
Step #2- Create a link “Compare” that has runs a function. Here is a sample:
private void funcCompare()
{
// REMOVED ALL SESSION
Session.Remove("arrCompare");
Session.Remove("catCompare");
// CREATE NEW ARRAY
List<string> arrCompare = new List<string>();
// COLLECT CHECKBOX DATA into ARRAY
for (int i = 0; i < Repeater1.Items.Count; i++)
{
CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("cbCompare");
if (chk.Checked)
{
arrCompare.Add(chk.ToolTip);
}
}
// PLACE ARRAY INTO SESSION
Session["arrCompare"] = arrCompare;
// GO TO COMPARE PAGE
Response.Redirect("ProductCompare.aspx");
}
The functions gets all the checked items (productID’s) and creates an array and then places that array in a Session("arrCompare");
I then redirect to ProductCompare.aspx page where loop through the array and display each item.
Just display the information from one phone next to the information of the other phone. How it looks on the page is up to you and it depends on what info you want to be most easily compared. The same idea is expandable to more than just a 2-pone compare as well.

Passing data from page to page

I'm looking for the best practice on how to pass data from page to page.
In Page A I have a button that fires off Page B.
On Page B I have 6 textboxes that allow the user to enter information.
When the user is done, the click on a button that brings them back to Page A.
I want to pass that data back to Page A.
I've seen suggestions to:
build XML documents and save to Isolated Storage
use the App class to store information in properties
pass it like a query string
I'm looking for the Best practice. Is there one that Microsoft recommends or one that is generally accepted as the best way?
Thanks
PhoneApplicationService.Current.State["yourparam"] = param
NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative));
then in other page simply
var k = PhoneApplicationService.Current.State["yourparam"];
Personally I'd store the values entered on Page B in a model(object) that is also accessible to Page A.
Depending on how you're navigating to Page A the second time, one or more of the following may be usful to help understand passing values between pages:
How to pass the image value in one xaml page to another xaml page in windows phone 7?
Passing a complex object to a page while navigating in a WP7 Silverlight application
How to pass an object from a xaml page to another?
How to pass a value between Silverlight pages for WP7?
How do I navigate from one xaml page to another, and pass values?
One thing you can consider is to use MVC: let your App be the controller, store all data in the model, and the pages are just views that contains pure UI logic. In this case your pages are painters and you pass your model object around. This gives nice isolation of business logic and the UI so that you can rev them easily.
BTW, Silverlight and XAML are great tools for MVC so it's a natural match.
There's a couple of things at play here. First of all, if/when the user uses the Back button to return to page A instead of your button, is the information in the text boxes exchanged or not (is Back = Cancel, or is Back = OK?)
That said, if you're using NavigationService.GoBack (which you should be instead of NavigationService.Navigate, because if you use the Navigate call, repeated hits of the back key will cause all kinds of bad UX for your users), then QueryStrings are not an option. Because pages really have no way to reference each other in the WP7 Silverlight nav system, you need to use a 3rd party to hold your data. For that, you can turn to (a) Isolated Storage (slow & heavy, but fail-safe), (b) Use the PhoneApplicationService.State dictionary, or (c) use Global properties of some kind, either hung off of the application object, or using Statics/Singletons...
Remember to watch for Tombstoning behavior when you do this - your page will process the OnNavigatedTo method when (a) you navigate into it in your application (b) you navigate back to it when you complete your work on Page B, or (c) you tombstone your app from that page and return to your application using the Back key.
Sorry I didn't give a more direct answer there - a lot depends on your specific circumstances. In the most general case, I'd strongly consider using the App State Dictionary on the PhoneApplicationService...it is lightweight, easy to use, and survives tombstoning. Just be sure that your keys are as unique as they need to be.
If you create a new Windows Phone project and use the Windows Phone Databound Template you will have most of the work done for you.
What you will want to do is set up the ViewModel to contain all the data for your app. You can serialize and deserialize this data using IsolatedStorage so that it's saved across application sessions and when Tombstoning.
In the template you will notice MailViewModel and ItemViewModel. MainViewModel stores all the data your application needs including an ObservableCollection of ItemViewModel, and ItemViewModel represents the individual data type for your application.
On the DetailsPage.xaml page you'll want to DataBind each textbox to the App.MainViewModel Items. Set the binding to TwoWay if you want the ViewModel to get updated as soon as the user manipulates the data on DetailsPage.xaml. You can optionally set the Binding to OneWay and then have an OK button that writes the changes back to the ViewModel and saves to IsolatedStorage.
Here is an example of what a Binding looks like:
<TextBlock x:Name="ListTitle" Text="{Binding LineOne}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
In this case LineOne is a property in ItemViewModel and the page gets this data from the query string when the user selects an item from the MainPage.xaml. The DataContext for the page determs where the databound information comes from.
Here is the snippet where the MainPage passes the selected item from the ViewModel to the DetailsPage.
// 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;
}
Here is how the DetailsPage gets the selected item.
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];
}
}
Play around with the default template above and ask any additional questions.
The beauty of databinding and the ObservableCollection is that you can just update the data and the UX will reflect those changes immediatley. This is because any changes to the data fires off an event:
public string LineOne
{
get
{
return _lineOne;
}
set
{
if (value != _lineOne)
{
_lineOne = value;
NotifyPropertyChanged("LineOne");
}
}
}
NotifyPropertyChanged() that broadcasts this information to the View.
You can also keep it simple and use PhoneApplicationService.Current.State which is basically a hashtable. You will need to implement your own marshalling to and from isolated storage if you want anything to outlive the app.
Omar's suggestion to use the Windows Phone Databound Template is probably the best idea on this page. It amounts to the same as my suggestion but you will get a better result (more maintainable code) at the cost of a longer steeper learning curve.
I suggest you do it my way and then do it again Omar's way.
as i implemented like this.. Whether its correct or not i dont know..
When u click news list page it should open the news detail page.
I want to pass the selected news item contents from news List-Page to news-details Page.
the News list page contains following method.
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
NewsDetailsPage newsDetailPage = (e.Content as NewsDetailsPage);
if (newsDetailPage != null)
newsDetailPage.SelectedNewsItem = SelectedNewsItem; //Contains the news details
base.OnNavigatedFrom(e);
}
In the News details Page. U can access that(SelectedNewsItem) object.
This may or may not be correct.
One option is to use Application.Resources:
Store data:
Application.Current.Resources.Add("NavigationParam", customers);
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
Retrieve data:
var customers = (List<Customer>) Application.Current.Resources["NavigationParam"];
Here's a blog post with describes this in more detail: http://mikaelkoskinen.net/windows-phone-pass-data-between-pages-application-resources/ (author: me)

Resources