Dropdownlist in Detailsview, not listing the items - c#

Hello Everyone
I have a detailsview, where there are 3 bound fields and a template field.
The template field has a DropDownList, which I have connected to an AccessDataSource.
But when I run, the dropdownlist has just the "System.Data.DataRowView" as it's items.
I wish to get the items from DB to be listed down in DropDownList
This is my code
asp:TemplateField HeaderText="State/Province" SortExpression="State/Province">
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="AccessDataSource1"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/db1.mdb"
SelectCommand="SELECT [State/Province_name] FROM [State/Province_List ]">
</asp:AccessDataSource>
</EditItemTemplate>
</asp:TemplateField>
Should I add "DataBinding" or "DataBound" event for DropDownList?? to make it perfect?
Help me regarding this issue
Thanks,
Arjun

Define the filed to use in the drop down list
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="State/Province_name"
DataValueField="State/Province_name"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
If you have problems with this non-standard column name, try to use an alias
SELECT [State/Province_name] AS StateProv FROM [State/Province_List ]
(Is the space in [State/Province_List ] OK?)
Then use this one
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="StateProv"
DataValueField="StateProv"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

Related

Filling a dropdownlist in a grid view with manual data

I am looking to fill a dropdownlist within a gridview with manual data. Is there anyway to do this through the markup code? I am just trying to fill it with days etc. 1 day(s), 2 day(s), 3 day(s) etc. Here is the code for my drop down
<asp:TemplateField HeaderText="No. Days">
<ItemTemplate>
<asp:DropDownList ID="txtFinishDate" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I've tried filling it manually on the page load but it doesn't seem to work and I think it is because of the grid view. Sorry if the question seems vague and thanks in advance!
what you have, but you have to add items to the list.
<asp:TemplateField HeaderText="custom">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="[ select an item ]" Value="0"></asp:ListItem>
<asp:ListItem Text="yes?" Value="1"></asp:ListItem>
<asp:ListItem Text="no?" Value="2"></asp:ListItem>
<asp:ListItem Text="umm..." Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

EditItemTemplate and InsertItemTemplate wont allow dropdownlist designer entry

I'm working on an aspx page where I'm trying to reference a dropdownlist from my code behind, now I add the dropdownlist to the page (ddlProgram) and its fine but soon as I move it into the InsertItemTemplate or EditItemTemplate or my GridView where I need it to go the designer entry disappears and I can no longer reference it from the code behind, is there a way around this? I have tried using the FindControl("ControlID") method but it always returns null. I need to set the selected index to 0 when the cancel button is click on my page. Thanks!
<asp:TemplateField HeaderText="Program" SortExpression="IDProgram">
<EditItemTemplate>
<asp:DropDownList ID="ddlProgram" AppendDataBoundItems="true" runat="server" DataSourceID="odsProgram" DataTextField="NameWithCode" DataValueField="IDProgram" CssClass="form-control ddl-normalize">
<asp:ListItem Text="No Program" Value="-1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="programName" runat="server" Text='<%# getProgramName((Eval("IDProgram")?? string.Empty).ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

How to make a Bind in SqlDataSource - Dropdownlist

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.

Eval() in a DropDownList within a GridView

How can I do Eval() in a DropDownList that are inside a GridView?
I tried the follow code, but still wrong:
<asp:TemplateField HeaderText="Prioridade">
<ItemTemplate>
<asp:DropDownList ID="ddlPrioridade" runat="server"
SelectedValue='<%# Eval("Prioridade") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Try this way
<asp:DropDownList ID="ddlPrioridade" runat="server" AppendDataBoundItems="true" DataSourceID="datasource" DataTextField="key" DataValueField="data" AutoPostBack="True" SelectedValue='<%# Eval("Prioridade") %>'>
also have a look at this DropDownList inside a GridView (or DataGrid). It might help you
You are receiving this error because the drop down list is not loaded with items and asp.net is trying to set the selected value of an empty drop down list and that's why it gives you error. So fill the drop down list first before setting it's selected value.

Render dropdown selected value in gridview

<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:DropDownList ID="Division" runat="server">
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text="Computer" Value="Computer"></asp:ListItem>
<asp:ListItem Text="Electronics" Value="Electronics"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
In i am not able to diplay the selected values in the dropdown which is already in the database.
If you are saying that you want to have the Division value for the row to be selected in the dropdown, you need to bind the SelectedValue property of the DropDownList to the field in your data source that contains the Division value. It would look something like this:
<asp:DropDownList ID="Division" runat="server" SelectedValue='<%# Bind("Division") %>' >
You can use Selected="True" as in:
<asp:ListItem Text="Select" Value="Select" Selected="True"></asp:ListItem>

Categories

Resources