Put a DropDownList instead of TextBox in DetailsView control - c#

I have an EntityDataSource control that is bind to an edmx file.
I create a detailsview and set it's DataSource property to entitydatasource control.
now I want to put a DropDownList instead of TextBox for "no_region_code" in Inserting mode.
Here is what I found from internet but it does not insert the contents of dropdownlist to the table when i click on insert button of detailsview.
<asp:TemplateField HeaderText="no_region_code" SortExpression="no_region_code">
<InsertItemTemplate>
<asp:DropDownList ID="drp_region_list" runat="server" DataMember="no_region_code">
<asp:ListItem Value="41">tabriz</asp:ListItem>
<asp:ListItem Value="21">tehran</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
How can I put a dropdownlist instead of these textboxes?

You didn't bind any property on that DropDownList. Try something like this
<asp:DropDownList ID="drp_region_list" runat="server" SelectedValue='<%# Bind("no_region_code")%>' DataMember="no_region_code">
<asp:ListItem Value="41">tabriz</asp:ListItem>
<asp:ListItem Value="21">tehran</asp:ListItem>
</asp:DropDownList>
EDIT : bind represent a way to link your property from the code behind (or model) to an asp control. This binding is bidirectional. For example, if you already have a value for no_region_code property in the code behind, the dropdown will already have that value selected on the view. Google asp.net data binding and you'll find a lot of examples and some more in depth explanations.

Related

First record in dropdown list will not update the other dropdownbox

I have a dropdown box that is populated from results from a SQL query. The selected value from dropdownlist1 successfully populates the lbAuthors dropdown list. During testing I realized that the first record from dropdownlist1 never updates into the lbAuthors dropdownlist. Here is an example: if I have three authors name in the 2nd dropdown box (Frost, Kipling, Poe) the first name - Frost - does not update into the first dropdown box. Kipling or Poe do - but not Frost.
My question is - What do I need to include in my event to allow Frost (or whatever the first record is) to update into the first dropdown box? –
Code-behind:
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Items.Add(new ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedItem.Text));
lbAuthorList.Items.FindByValue(DropDownList1.SelectedItem.Text).Selected = true;
}
Markup:
<asp:DropDownList runat="server"
ID="lbAuthors"
style="float:left;"
DataSourceID="odsAuthorList"
DataTextField="DisplayAuthorName" DataValueField="AuthorID"
onselectedindexchanged="lbUserList_SelectedIndexChanged"
AppendDataBoundItems="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1"
runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
</asp:DropDownList>
That is because when you select the first item, selcted index does not change. You need to insert a dummy item like this::
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
<asp:ListItem Text="--Select One--" Value="-1"></asp:ListItem>
</asp:DropDownList>
This issue rises because u wrote code for dropdownlist changed but initially first value selected at that time dropdownlist changed event not fired. If u choose second and then choose first one it will work fine.
Just add one dummy variable in dropdownlist item..
dropdownlist1.Items.Add("--Select--");

Change blank selection in drop down list?

I have a databound dropdown list on my page. The first value in the selection list is blank. Is there a way that I can change it so that the first selection says "Unassigned" instead of blank? I've tried the following, but it didn't work:
// Insert 'Unassigned' value for artist dropdown
ddlArtists.Items.Insert(0, "Unassigned");
After inserting the above code, the list still appears unchanged, with the first selection value being a blank. Any pointers would be great! Thank you!
EDIT: Here is the code for the dropdown:
<asp:DropDownList ID="ddlArtists" runat="server" Width="130px" TabIndex="5"
OnSelectedIndexChanged="ddlArtists_SelectedIndexChanged"
DataSourceID="sqldsArtist" DataTextField="Name" DataValueField="ID"
OnDataBound="ddl_DataBound"
AutoPostBack="True">
You don't need to do it on the CodeBehind. Just do it like that:
<asp:DropDownList ID="ddlArtists" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Unassigned" Value="0" Selected="true" />
</asp:DropDownList>
The AppendDataBoundItems property defines whether the contents of the DropDownList should be cleared out before data binding.
Don't forget to check for a PostBack when you're data binding it, to avoid duplicates.
Set the SelectedIndex property of your DropDownList to 0.

Add a blank value for the first entry of a DropDownList

I have a DropDownList that is data bounds to a SQL Server Data Source. As it is, the first entry from the data source is the first entry selected by default. In order to select this entry (and have the DropDownList_SelectedIndexChange() function be called), I have to select the second option, and then the first option again.
Is there a way to enter an invalid item into the DropDownList as the first item, sort of as a "default value"?
Set AppendDataBoundItems to true on the drop-down list and then add your default item to the list
<asp:DropDownList ID="ddlOne" runat="server" AppendDataBoundItems="True"
DataSourceID="" DataTextField="" DataValueField="">
<asp:ListItem Value="">Please Select</asp:ListItem>
</asp:DropDownList>
This will result in all your data-bound values being added to the dropdown after the ones you have specified in the markup above
By "SQL Server Data Source" I assume you mean a declarative SQLDataSource in the page markup, so that there's no actual C# code in question here? If so, there are two things you'd need to do.
First, add a default value to the DropDownList. Second, set the property AppendDataBoundItems to true:
<asp:DropDownList ID="someID" runat="server" DataSourceID="someDataSource" DataTextField="name" DataValueField="id" AppendDataBoundItems="true">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

ASP.NET Databinding a DropDownList

Greetings Gurus,
I have an ASP.Net app that I'm working on with a editable detailsview.
There is a boundfield in the detailsview called Status which I turned into a template field.
This templatefield (specifically the editItemTemplate) was changed to a dropdownlist with a unique datasource. How do I Bind this dropdownlist to the StatusField so my update query picks up it's value when I click update?
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStatus" runat="server"
DataSourceID="Status_DataSource" DataTextField="Status" DataValueField="Status">
</asp:DropDownList>
</EditItemTemplate>
You cannot bind to controls from a template field. You need to read the value (using FindControl to find your DropDownListStatus and read its value) and set it for the update query manually in your update method.

Bind SelectedValue of ASP.net DropDownList to nested property

(first of all, it seems this is a subject that is discussed many times before, but I can't find a proper answer for my case)
I have a asp.net FormView with a DropDownList for the selection of a month. The FormView is data bound to an ObjectDataSource.
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Bind("OrderDate.Month") %>' Width="100" runat="server" />
I like to bind the selected value to the nested property 'Month' of 'OrderDate' as shown above. The property OrderDate is of type DateTime. The error I'm getting while binding to a nested property is:
A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.
What is the best solution to be able to bind to a nested propety?
Thanks in advance.
Are you retrieving data from DataSourceID="MonthsListDataSource" and tryingo to bind it to another DataBase field (SelectedValue='<%# Eval("OrderDate.Month") %>') ?
In my application a do this like this:
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("MonthsListDataSource.Month") %>' Width="100" runat="server" />
And when Updating a retrieve de DropDownList with find control, get its selected value, associate it to other table (OrderDate.Month) and save it.
Sorry my answer, but i dont know if a undertand it.
What about using Eval keyword
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("OrderDate.Month") %>' Width="100" runat="server" />

Categories