Bind SelectedValue of ASP.net DropDownList to nested property - c#

(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" />

Related

Put a DropDownList instead of TextBox in DetailsView control

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.

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.

Error when displaying data in a datalist from sqldatasource in asp project

I am building a website for a class and have come across an error that I can not find the solution to. I have a dropdownlist on the page that displays the cutomer name and sets the selected value to the customer id selected from a sqldatasource (data tables from access database - fig1) (this works fine as I have tested it before I added the second sqldatasource). I add a datalist control to the page and a second sqldatasource for the datalist to bind to. I configured the datasource using the configure data source wizard as shown in fig2. I then use the wizard to test the data returned by the query and see that it works, the wizard shows that the data that I wanted is returned. So I bind the datalist to the data source and now the itemtemplate (in the html source view) contains data bound labels that will show the values:
Products.Name:
<asp:Label ID="Products_NameLabel" runat="server" Text='<%# Eval("Products.Name") %>' />
<br />
Technicians.Name:
<asp:Label ID="Technicians_NameLabel" runat="server" Text='<%# Eval("Technicians.Name") %>' />
<br />
Incidents.DateOpened:
<asp:Label ID="Incidents_DateOpenedLabel" runat="server" Text='<%# Eval("Incidents.DateOpened") %>' />
<br />
Incidents.DateClosed:
<asp:Label ID="Incidents_DateClosedLabel" runat="server" Text='<%# Eval("Incidents.DateClosed") %>' />
<br />
Incidents.Description:
<asp:Label ID="Incidents_DescriptionLabel" runat="server" Text='<%# Eval("Incidents.Description") %>' />
The labels are now bound to the data returned from the second source....or so it would seem. When I run it and select a customer that should return the data that is being selected in the sqldatasource select statement, my program crashes and gives this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Products'.
I do not understand why it is saying that the datarowview does not contain a column with that name when the sqldatasource clearly returns it when I test it in the wizard. Can anyone please help?
P.S. Sorry for the links to imgur...apparently you have to have a higher rep to post pictures
I tried this and the generated HTML was NOT prefixed with the table name like in your code.
I hand-added the table name and reproduced your error.
So, if you have duplicate column names like you do, you need to modify the SQL statement to use an alias:
Products.Name as Products_Name

How to Bind database integer value to TextBox in FormView control?

On my page I have FormView control, and I am binding Integer database field to the TextBox that's residing inside a FormView's EditItemTemplate.
<FormView ...>
<EditItemTemplate>
<dx:ASPxTextBox ID="txtDiameter" runat="server" Text='<%# Bind("Diameter") %>' />
...
<EditItemTemplate>
...
</FormView>
My problem is that when Diameter field is null, the txtDiameter gets value of empty string. When I click Update command (if I didn't provide any numerical value in txtDiameter), a client error is raised
Sys.WebForms.PageRequestManagerServerErrorException: is not a valid value for Int32.
I found some post from 2005 that claims that this is happening due to some bug. Well, now is 2012. The only way I figured how to deal with this is by using FormView_ItemUpdating event, to circle through all problematic values and convert them from String.Empty to null.
I am just little suspicious that it's maybe not necessary. Is there another way to deal with this problem?
Bind the editor's Value property instead:
<dx:ASPxTextBox ID="txtDiameter" runat="server" Value='<%# Bind("Diameter") %>' />
Does this work?

Looking for suitable DataBound control to implement questions page

I looking forward to find a suitable asp.net DataBound control (C#) to implement in my application.
I wanted to create a exam page where each page show 10 questions, each question got a Label control and a radiobutton control to display the choices, the data to be bind into the DataBound control might be having multiple rows where each rows represent each question.
I found that DetailView control was quite suit with my requirement but I not able to set the page size.
Please help to give some suggestion and advises, thank you in advanced.
I would use a DataList or a ListView, because it will allow you to enter a template for each item. The reason I would choose these over a repeater is because you can use data keys, which will probably come in handy.
Here's a simple example of how you could implement a list of questions:
<asp:DataList ID="DataList1" runat="server" DataKeyField="QuestionID" ...>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Question")%>' />
<asp:RadioButton ID="RadioButton1" runat="server" Text="Yes" GroupName="QuestionAnswer" ... />
<asp:RadioButton ID="RadioButton2" runat="server" Text="No" GroupName="QuestionAnswer" ... />
</ItemTemplate>
</asp:DataList>
I would recommend to you to use the Repeater control since you can customize it's design very easily to suit your needs.
Here are two tutorials on how to use it:
http://www.w3schools.com/aspnet/aspnet_repeater.asp
http://www.learn-asp.net/asptutorials/Repeater.aspx
Update:
Repeater doesn't have pagination included so you would have to add it:
http://blog.divergencehosting.com/2009/03/25/adding-paging-repeater/
Other option would be to just use a GridView which has pagination included.

Categories