How can I populate a drop down list control Collection? - c#

How can I populate a drop down list control (ASP.NET) from a SPListTemplateCollection (SharePoint 2007) ?
SPWeb web = SPContext.Current.Web;
ddlTemplateList = new DropDownList();
ddlTemplateList.DataSource = web.ListTemplates;
ddlTemplateList.DataBind();
This code doesn't work in a right way... The name of the list template is not showed.

You need to specify DataTextField and DataValueField to make it work.
ddlTemplateList.DataSource = web.ListTemplates;
ddlTemplateList.DataTextField = "DisplayColumnName";
ddlTemplateList.DataValueField = "ValudColumnName";
ddlTemplateList.DataBind();

use DisplayMember and ValueMember property!
update
DisplayMember and ValueMember are property for WinForm controls.
For asp.net correct solution, as metioned by Muhammad, is to use DataTextField and DataValueField.

Try this one
List<SPWeb> lstSPWeb = web.ListTemplates
ddlTemplateList.DataSource = lstSPWeb;
ddlTemplateList.DataBind();

foreach (SPListTemplate lt in SPContext.Current.Web.ListTemplates)
ddlTemplateList.Items.Add(new ListItem(lt.Name, lt.Type.ToString()));

Related

datagridview datasource not binding when change

My code works fine but when I add data to datagridview it not binding to gridview. I didn't understand. When I search some I find BindingSource.If ı use this I have to change lots of things of my code. are there some shortcuts of binding to datagridview
This is loading datagridview
schoolGridView.DataSource = load.GetDataSource();
And this is when I adding row.
List<DataSourceObject> src = (List < DataSourceObject >)schoolGridView.DataSource;
DataSourceObject dat = new DataSourceObject();
dat.sinif = "asd";
dat.okulAdi = "ad";
dat.ogrenciAdi = "123";
dat.ilce = "43";
dat.il = "123";
src.Add(dat);
schoolGridView.DataSource = src;
Have you ever tried BindingList<> generic class that are simillar to List<> class? BindingList are more suitable for role of DataSource. I belive you can also benefit from methods like GridView.Invalidate(), GridView.Update() and GridView.Refresh(). You can find more information in DataGridView class.

Add "--Select--" in ComboBox

I have a Combobox the data for which is being populating from Sharepoint List.
I need to add a value called "--Select--" in the combobox and set it as default upon launching the application.
I cannot add "--Select" in the Sharepoint list. Please assist on how can i do it.
Below is the code.
teamName = templatedata.getTeamName();
cmbteams.DataSource = teamName;
I assume Winform,
cmbteams.Items.Insert(0, "SELECT");
cmbteams.SelectedIndex = 0;
do like this:
cmbteams.Items.Insert(0, "Select");
or
cmbteams.Items.Add(new Item("Select", 0));
you can set default like this:
cmbteams.SelectedValue=0;
or
cmbteams.SelectedIndex=0;

ASP.Net Dropdownlist ListItem (Enabled=false) not showing in page

I have a dropdownlist in aspx called ddlService.
I want to added the listitems from behind.
When I add, I will create them in order of Title and items underneath like..
Title1
Item1
Item2
Title2
Item1
Item2
Titles should not be able to click. Only Items should be able to click.
ListItem tempServicesItem = new ListItem();
tempServicesItem.Text = tempTitle;
tempServicesItem.Value = tempTitle;
tempServicesItem.Enabled = false;
ddlServices.Items.Add(tempServicesItem);
tempServicesItem = new ListItem();
tempServicesItem.Text = tempItem;
tempServicesItem.Value = tempItem;
ddlServices.Items.Add(tempServicesItem);
The problem I encountered is The ListItems with (Enabled=false) are not appearing in aspx.
When I change it to (Enabled=true), it is appearing.
I must have missed out something. Can anyone point out?
Thanks.
I believe this is what you're looking for. (Not tested)
tempServicesItem.Attributes.Add("disabled", "disabled");
MSDN documentation says
You cannot use this property to disable a ListItem control in a
DropDownList control or ListBox control.
I think you need to set the "disabled" attribute which corresponds to the HTML markup for the option element
tempServicesItem.Attributes["disabled"] = "true";

What should be the return type of property while assigning datasource to HTML select in .net

Please advise how I can assign datasource to HTML select control from code behind using a property in asp.net. I want to know what should be the type of property like will it be OBJECT
HTML
<select name="searchKey" id="selDropdown" runat="server"></select>
thanks
You have to list items just like an asp Dropdown list, something like...
selDropdown.Items.Add(new ListItem("text", "Value", true));
I am not sure, but have you tried this?
selDropdown.DataSourceID = "DatasourceID";
selDropdown.DataTextField = "";
selDropdown.DataValueField = "";

Manually set value Member of Combobox

Actually I need to set the value member of combobox using Foreach loop
My code goes like this.
foreach(DataRow row in dsTable.Tables["mytable"].Rows)
{
combobox1.Items.Add(row["my column"]);
}
how do i set value member on it?
Here is the definition and some examples of combobox use.
msdn combobox link
Basically a combobox accepts "objects" so u could add any kind of items in it.
Hopes it helps.
You're not binding a DataSource so basically there's no point in setting the ValueMember property.
You should do this instead of the loop:
combobox1.DataSource = dsTable.Tables["mytable"];
combobox1.ValueMember = "MyValueColum";
combobox1.DisplayMember = "MyDisplayColumn";

Categories