I need to add the diable field "Select..." in dropdownList. The data is bind along with datasourse in code behind. I am have tried to add disable field but I can see any result.
Code Behind
AvailableRolesList = RoleDefinationRelay.GetAllRoles(null);
ddlRolesList.DataSource = AvailableRolesList;
ddlRolesList.DataTextField = "Title";
ddlRolesList.DataValueField = "RoleID";
ddlRolesList.DataBind();
DropdownList
<asp:DropDownList ID="ddlRolesList" runat="server">
<asp:ListItem Text="SelectRole" Enabled="false" Selected="True">Select Role</asp:ListItem>
</asp:DropDownList>
enter code here
You need to add Select item after binding dropdownlist in code behind.
AvailableRolesList = RoleDefinationRelay.GetAllRoles(null);
ddlRolesList.DataSource = AvailableRolesList;
ddlRolesList.DataTextField = "Title";
ddlRolesList.DataValueField = "RoleID";
ddlRolesList.DataBind();
ddlRolesList.Items.Insert(0, "Select Role");
here is my answer
ddlRolesList.Items.Insert(0, new ListItem("Select Role","NA"));
Related
I would like to display a DropdownList but it isn't working yet.
This is my asp.net code:
<asp:Panel ID="pnlChannel" runat="server">
<asp:SqlDataSource ID="sdsChannel" runat="server"></asp:SqlDataSource>
<asp:DropDownList ID="ddlChannel" runat="server">
<asp:ListItem id="limDefault" runat="server"></asp:ListItem>
</asp:DropDownList>
</asp:Panel>
And then this is my codebehind:
public Panel GetDropDownList()
{
// Create drop down list and data source
Panel pnlChannel = new Panel();
DropDownList ddlChannel = new DropDownList();
ListItem limDefault = new ListItem();
SqlDataSource sdsChannel = new SqlDataSource();
// Configure data source
sdsChannel.ConnectionString = ConfigurationManager.ConnectionStrings["CR_SQL"].ConnectionString;
sdsChannel.SelectCommand = "SELECT * FROM TABLE";
sdsChannel.ID = "sdsChannel";
// Configure drop down list
ddlChannel.DataTextField = "Channel";
ddlChannel.DataValueField = "Channel";
ddlChannel.AppendDataBoundItems = true;
ddlChannel.DataSourceID = "sdsChannel";
// Configure default list item
limDefault.Selected = true;
limDefault.Text = "All";
limDefault.Value = "-1";
// Add controls to static panel in footer
ddlChannel.Items.Add(limDefault);
pnlChannel.Controls.Add(ddlChannel);
pnlChannel.Controls.Add(sdsChannel);
return pnlChannel;
}
Did I miss something?
So I think the ListItem isn't working, because there is a DropDownList but I can't see any list to drop down.
You have not set Text property of ListItem. Also remove code from .cs file for creating controls because this will duplicate the controls.
<asp:DropDownList ID="ddlChannel" runat="server">
<asp:ListItem id="limDefault" runat="server" Text="SomeText" Value="SomeValue"></asp:ListItem>
</asp:DropDownList>
I would like to make a DropDownList inside a panel. This is my code from the codebehind file. But if I execute it, it always says: "in DropdownList it is not allowed to make multiple selections." Do I have to do something with the autopostback? So the error comes when I want to select something else than than "All".
DropDownList1.DataTextField = "Kanal";
DropDownList1.DataValueField = "Kanal";
DropDownList1.AppendDataBoundItems = true;
ListItem limDefault = new ListItem();
limDefault.Selected = true;
limDefault.Text = "All";
limDefault.Value = "-1";
DropDownList1.Items.Add(limDefault);
Then this is my ASP.NET code:
<asp:Panel ID="Panel1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CR_SQL %>" SelectCommand="Select * from table" >
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" AutoPostBack="True">
</asp:DropDownList>
</asp:Panel>
I guess you execute the first snippet on every postback which adds the default item every time. Do that only at the first time the page loads, therefore use Page.IsPostBack to check that:
if(!IsPostBack)
{
ListItem limDefault = new ListItem();
limDefault.Selected = true;
limDefault.Text = "All";
limDefault.Value = "-1";
DropDownList1.Items.Add(limDefault);
}
One column in a DataGrid has a DropDownList displaying locations but right now its only displaying the same location for each row, not the correct location.
protected void PopulateDDLs(DropDownList ddlTrailerLoc)
{
DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
if (dsTrailerLocation.Tables[0].Rows.Count > 0)
{
ddlTrailerLoc.DataSource = dsTrailerLocation;
ddlTrailerLoc.DataValueField = "Description";
ddlTrailerLoc.DataTextField = "Description";
ddlTrailerLoc.DataBind();
}
else
{
ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
}
}
protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
{
DropDownList ddlTrailerLocation = e.Item.FindControl("ddlTrailerLoc") as DropDownList;
if (ddlTrailerLocation != null)
{
PopulateDDLs(ddlTrailerLocation);
}
}
You can see from the picture that is does display the location but its only displaying one location which in incorrect. How do I set the dropdownlist to display the correct location?
Please refer to the following ASP.NET DropDownList Binding example:
<asp:DropDownList ID="cmbCountry" runat="server"
AppendDataBoundItems = "True"
DataSourceID="selectCountrySQL"
DataTextField="Country"
DataValueField="Country_Code"
SelectedValue='<%# Bind("O_CountryCode") %>' >
</asp:DropDownList>
You have to specify binding for DataTextField (e.g. Country Name) and DataValueField (e.d. Country Code) as shown in this example.
Notice the line:
SelectedValue='<%# Bind("O_CountryCode") %>' >
Pertinent to your case, instead of O_CountryCode it should be "Trailer Location" field in GridView DataSource.
To populate the DropDownList with values you can create and bind another DataSource (in this example selectCountrySQL) like the following:
<asp:SqlDataSource id="selectCountrySQL" runat="server"
SelectCommand="SELECT [Country], [Country_Code]
FROM [Ref_Countries]
ORDER BY [Country]" />
Notice that DropDownList and GridView are bound to different DataSources.
Hope this may help.
hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
I am working on a metrics screen that will display several charts based on different groups in a database. Part of it uses a function that hides selected charts until the user clicks to display them.
The problem is this: I'm using a Databind on the dropdownlist, so every time I select a new group, the page refreshes and everything returns to its default state.
My question is this: Is there a way that I can avoid refreshing the page every time I select a new option from the dropdown list? If so, how? If not, is there a better way to create the dropdownlist and attach values to it? If I set AppendDataBoundItems to false, then I always get the selected value as the first item in the list.
Here's my code for the dropdownlist:
<asp:DropDownList ID="MinistryDropdown" OnSelectedIndexChanged="Selection_Change" AutoPostback="true" AppendDataBoundItems="true" runat="server"/>
Then C# code behind it is this:
public void Page_Load(object sender, EventArgs e){
MinistryDropdown.DataSource = CreateDataSource();
MinistryDropdown.DataTextField = "Description";
MinistryDropdown.DataValueField = "Description";
MinistryDropdown.DataBind();
...other code here...
}
ICollection CreateDataSource(){
DataTable Ministries = new DataTable();
Ministries = oDatabase.GetData(#"SELECT DISTINCT B.Description
FROM tblInvolvement AS A LEFT JOIN tblMinistries AS B
ON A.Activity = B.MinistryID");
DataView dv = new DataView(Ministries);
return dv;
}
Try to use the ASP.NET UpdatePanel. Just wrap your DropDownList in it, and it should works. Here is a quick example that I didn't test.
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="MinistryDropdown" OnSelectedIndexChanged="Selection_Change" AutoPostback="true" AppendDataBoundItems="true" runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
On a final note, you will soon find out the limits of this solution, and later you might prefer to use Javascript instead.
I think the issue is that you are rebinding the data on Page_Load but you are not checking if !IsPostBack in other words, your code should look like this:
public void Page_Load(object sender, EventArgs e){
if(!IsPostBack)
{
MinistryDropdown.DataSource = CreateDataSource();
MinistryDropdown.DataTextField = "Description";
MinistryDropdown.DataValueField = "Description";
MinistryDropdown.DataBind();
...other code here...
}
}