I have a list of names to select from a dropdown list. The names are being pulled from a data source and not from a ListItem. I want to leave the box blank while no selection has been. How can I do this?
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="director"
DataValueField="director">
<!--<asp:ListItem Text="Please Select Director" Value="-1"/>-->
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:dvdsConnectionString %>" ProviderName="<%$
ConnectionStrings:dvdsConnectionString.ProviderName %>"
SelectCommand="SELECT [director] FROM [dvds]">
</asp:SqlDataSource>
Try add AppendDataBoundItems="True" to the tag and add <asp:ListItem Selected="True"></asp:ListItem> as an item
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="director"
DataValueField="director" AppendDataBoundItems="True">
<asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:dvdsConnectionString %>" ProviderName="<%$
ConnectionStrings:dvdsConnectionString.ProviderName %>"
SelectCommand="SELECT [director] FROM [dvds]">
</asp:SqlDataSource>
Related
This is how I should be able to update a product on the page, it must grasp the category which I have chosen, so I will be free to save my choice every time.
I chose to do it via SqlDataSource dropdownlist.
I have written the code like this:
<asp:DropDownList ID="DropDownList1"
runat="server"
CssClass="form-control"
DataSourceID="SqlDataSource1"
DataTextField="navn"
DataValueField="Id"
SelectedValue='<%# Bind("kategori") %>'
></asp:DropDownList>
<%--datasource--%>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT kategori.id, kategori.navn, produkter.fk_kategori FROM kategori INNER JOIN produkter on kategori.id=produkter.fk_kategori ORDER BY [Id] DESC"></asp:SqlDataSource>
It appears this one error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
<asp:DropDownList ID="DropDownList1"
What should I do to the grasp it I've chosen category.
I have the following in my data base -
breedId Species Breed
0 dog Alsatian
1 dog pitbull
2 dog Shetland sheepdog
3 dog Boxer
4 cat Dragon Li
5 cat Australian Mist
6 cat Korat
In the c# designer view, I have 2 drop-down list one which has species and other for breed.
What I want is that when user picks 'dog' in species list,
the breed list should have the following Alsatian, pitbull, Shetland sheepdog,Boxer
At the moment when I pick 'dog', all the breed from the database is shown.
<asp:DropDownList ID="DropDownListSpecies" runat="server"
Height="27px" Width="107px" DataSourceID="hs330"
DataTextField="Species" DataValueField="Species">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Species] FROM [Breed]">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px"
Width="110px" DataSourceID="breed" DataTextField="Breed"
DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Breed] FROM [Breed]">
</asp:SqlDataSource>
You need to use ControlParameter in SelectParameters.
Make sure that AutoPostBack="True" for DropDownListSpecies
FYI: You have typo in Speecies
<asp:DropDownList ID="DropDownListSpecies" runat="server"
Height="27px" Width="107px" DataSourceID="Species"
DataTextField="Species" DataValueField="Species" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Species] FROM [Breed]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownListBreed" runat="server"
Height="20px" Width="110px"
DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species=#Species">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
Name="Species " Type="String" DefaultValue="cat" />
</SelectParameters>
</asp:SqlDataSource>
You are not filtering the data in the second drop down list, based on the selection made in the first drop down list (which is what you want).
<asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px" Width="110px" DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species = #Species">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
Name="Species " Type="String" DefaultValue="cat" />
</SelectParameters>
</asp:SqlDataSource>
Also, you need to add the AutoPostBack="True" property to each DropDownList, if you want the changes to be reflected as soon as you change the value of each DropDownList.
I have an access data source which loads a listbox values. I am trying to pass a parameter to the accessdatasource but I'm not sure what I am doing wrong.
<asp:DropDownList ID="customerName" runat="server">
<asp:ListItem value="69" Text="Danny"></asp:ListItem>
<asp:ListItem value="23" Text="Sarah"></asp:ListItem>
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="/App_Data/data.mdb" SelectCommand="SELECT * FROM table WHERE customerID='?'">
<selectparameters>
<asp:ControlParameter Name="customerID" ControlID="customerName" PropertyName="SelectedValue" />
</selectparameters>
<asp:ListBox ID="lstCustomers" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="customerName"
DataValueField="customerID" Width="175px" Height="365px"
onselectedindexchanged="lstCustomers_SelectedIndexChanged"></asp:ListBox>
The list comes back as blank... not sure what I'm doing wrong!
Just to clarify.. This was solved by Richard Deeming, but there's no answer to answer. I'll post the code here
It was solved by removing the single quote marks around the ? parameter in the SQL
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="/App_Data/data.mdb" SelectCommand="SELECT * FROM table WHERE customerID=?">
Does anyone how can I get the parameters for SelectParameters from another dropdown in c# .net?
I check online , and I follow the steps that been told, but it's doesn't seem working for me.
Code:
If I have the following code to retrieved the data for a dropdown:
<asp:DropDownList ID="DropDownVisaType" runat="server" DataSourceID="dsDropDownVisaType" CssClass="dsDropDownVisaType"
DataValueField="VisaTypeID" DataTextField="VisaType" AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsDropDownVisaType" runat="server" ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
SelectCommand="app_visa_type_select" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
After I select the dropdown, I want to use the VisaTypeID from the dropdown to retrieve data from another dropdown as below:
<asp:DropDownList ID="DropDownVisaTypeSpecific" runat="server" DataSourceID="dsDropDownVisaTypeSpecific" CssClass="dsDropDownVisaTypeSpecific"
DataValueField="VisaTypeSpecificID" DataTextField="VisaTypeSpecific" AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsDropDownVisaTypeSpecific" runat="server" ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
SelectCommand="app_visa_type_specific_select" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Property="SelectedValue" ControlID="DropDownVisaType" Name="VisaTypeID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Does anyone know what is my code going wrong and how can I make it work?
There is an event SelectedIndexChanged for A dropdown by using that you can get the value of selected Item.
Like below:
protected void DropDownVisaType_SelectedIndexChanged(object sender, EventArgs e)
{
Int32 value = Convert.ToInt32(DropDownVisaType.SelectedItem.Value);
DropDownVisaTypeSpecific.DataSource=Method_Name(Value);
DropDownVisaTypeSpecific.DataBind();
}
I have two nested DropDownLists (the value selected in the 1st ddl will limit the content of the 2nd ddl).
The example attached here runs fine outside of a GridView. As soon as I try to insert this into a GridView, I get an error because I cannot identify the ControlID to use in the ControlParameter.
The code outside of the GridView looks like this:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
And the DataSources look like this:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
Thanks!
I have tried something similar and it works fine. I can't tell from your post whether the data sources are together with the dropdowns inside the ItemTemplate of the GridView. At least the second one should be inside the ItemTemplate so that you will have a data source for each line in the grid.
I have posted my page below. It's using different tables, but it's the same idea.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = #FamilyId)">
<SelectParameters>
<asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Family]">
</asp:SqlDataSource>
</form>
The actual id's are build based on where they are, so you will see something like $GridView1 inserted in the id's.
To solve this you can set ClientIDMode="Static" on your page properties (first line of your .aspx file).
Or use "javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');