Im trying to do bind a dropdown list to a details view but keep getting an error about the dropdown list ID field:
<asp:TemplateField HeaderText="Approval">
<ItemTemplate>
<asp:DropDownList ID="Approved" runat="server" DataValueField="Approved" SelectedValue='<%#Bind("Approved") %>'>
<asp:ListItem Text="Approved" Value="Approved" />
<asp:ListItem Text="Denied" Value="Denied"/>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
The error message as follows:
'Approved' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
What is the proper way to attach the value of the dropdownlist to my object so that it can be properly created in the database? Most of my searches keep telling me how to bind a dropdownlist to an object data source, but that is not what I need to do. This is a basic drop down list of 2 items that will never change.
This error happens because you are binding the SelectedValue of the DropDownList to the "Approved" field of your DetailsView's datasource, but the value it's trying to assign isn't one of the two you have listed ("Approved" and "Denied").
I see you have set DataValueField="Approved". Are you setting the Dropdown's Datasource in code-behind? Because that is not going to set your ListItems to the values from the "Approved" column in the DetailsView's datasource, it's setting them to whatever the datasource for the Dropdown is.
Depending on your logic, here are some possibilites:
Make sure your static items match the possible items returned in the "Approved" field of the DetailsView. Or,
Bind the DropDownList to a dataset that includes all the possible values from "Approved", and completely removed your static items. Or,
Set AppendDataBoundItems="True" in your DropDown and have both static and databound items
Related
I have a situation where I have a gridview which is populated from a sql table which editing is allowed. For one of the fields, I have it so the user has to select a value from a drop down box in the edit template for the gridview.
<asp:TemplateField HeaderText="Department" SortExpression="Department">
<EditItemTemplate>
<asp:DropDownList ID="ddlEditDepartment" runat="server"
SelectedValue='<%# Bind("Department") %>'>
<asp:ListItem>Department 1</asp:ListItem>
<asp:ListItem>Department 2</asp:ListItem>
<asp:ListItem>Department 3</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
If you select "edit" for a record in which this field already has a value stored in the table for it, no problem. However, if you select "edit" and there is no value in the table for it yet, you get the following error
'ddlEditDepartment' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
How can I account for blank or null values in this situation and make this work?
You're binding your DropDownList to a field named "Department" in your GridView's datasource:
SelectedValue='<%# Bind("Department") %>'
It's kind of a hack, but you could modify the query that populates the Department field of your GridView to account for the nulls (by coalescing them out):
SELECT COALESCE(Department, "No department entered") AS Department
FROM YourTable
And then just add that as kind of a "default" option in your dropdown:
<asp:ListItem>No department entered</asp:ListItem>
It sounds like you are trying to update a record that does not exist. So, you have a few options, depending on what you want to do. You could capture the error and display a user friendly alert to the user that they need to create a new record before editing. Or, you could capture the error and run an insert command if the error gets raised. Or you could prevent the error from firing all together. This would be my suggestion. In your code behind, when the dropdowlist event fires, take the ID and run a query to see if the record exists. If it does, update it. If not, either alert the user or you could just do the insert then. If you have a specific question on how to do this, let me know.
We are using ASP.NET for one of our projects. However, when we are trying to read the SelectedItem or SelectedValue property of the DropDownList we are using in the callback function of a Link click, we are not getting the correct SelectedItem.
<FooterTemplate>
<asp:DropDownList ID="cmbTesters" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID"></asp:DropDownList>
</FooterTemplate>
This is the DropDownList in the aspx file. The drop down is present within a GridView's Footer Row. We are invoking the following set of code on clicking a link.
if (int.TryParse(((DropDownList)dgCreateCPRVerificationResponse.FooterRow.FindControl("cmbTesters")).SelectedValue, out TesterID))
{
TesterID = int.Parse(((DropDownList)dgCreateCPRVerificationResponse.FooterRow.FindControl("cmbTesters")).SelectedValue);
}
The problem we are facing is that whatever value we choose, the SelectedValue is always of the first item in the list. We are using REST based URL defined in the global.asax file. Also note that this is not built on any framework.
Please help as soon as possible
Make sure to put the binding methods of the dropdownlist and the gridview inside if (!IsPostBack)
I have populated my DropDownLists with different columns of items from a database but I'm trying to make it so that the first item in the list has no value with text similar to "Select an Item"
For some reason Even though I add the item to the list and make it the selected item, It gets overridden by the items from the database...
How can I accomplish this?
UPDATE:
Everything is done from the .aspx page designer but here is the generated code-
<asp:DropDownList ID="ddlUnits1" runat="server" DataSourceID="UnitsEDS"
DataTextField="unitId" DataValueField="unitId">
<asp:ListItem Selected="True">Select Units</asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="UnitsEDS" runat="server"
ConnectionString="name=UnitsEntity"
DefaultContainerName="UnitsEntity" EnableFlattening="False" EntitySetName="spillunits">
</asp:EntityDataSource>
Use the ListControl.AppendDataBoundItems Property:
AppendDataBoundItems Documentation
From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items."
I've got a ListBox within a DetailsView, each having a different (Sql)DataSource:
<asp:DetailsView ID="dvwSomeDetailsView" DataSourceID="sdsSomeDetailsView" runat="server" ...
...
<asp:TemplateField HeaderText="SomeHeaderText">
<ItemTemplate>
<asp:ListBox ID="lstSomeListBox" runat="server" DataSourceID="sdsSomeListBox" DataTextField="SomeColumn" DataValueField="SomeOtherColumn" Rows='<%#Eval("NumberOfRows") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</asp:DetailsView>
....
Trying to access the column "NumberOfRows" now seems to try and read it from the SqlDataSource that is associated with the DetailsView as an exception is thrown: "DataBinding: System.Data.DataRowView does not contain a property with the name NumberOfRows".
Filling the items of the ListBox with what is returned from the SqlDataSource for that ListBox is not a problem, but how can I access data from other columns (if at all possible)? Just entering the column name as in Rows="NumberOfRows" does of course not work.
Update
Clarification of what I would actually like to do: I want the "Rows" property of the ListBox to be set dynamically to the number of items, i.e. if there are six items, "Rows" should be set to six so no scrolling is neccessary and not empty space is in the listbox.
Thanks in advance.
G.
Not sure if this is entirely what you want, but you can try this:
// Get the ListBox - I used the first row for sake of illustration
ListBox lb = (ListBox)dvwSomeDetailsView.Rows[0].FindControl("lstSomeListBox");
// Now you can access the ListItemCollection of the ListBox
string value = lb.Items[0].Value;
Hopefully that will at least get you going in the right direction.
I have a grid view that is data bound to a dataset. In a grid I have a column DefaultValue, which has three controls in it - a dropdownlist, a checkbox and a textbox. Depending on the data that is coming in it can switch to any of these controls. Everything is simple enough when we need just to display data - in gridview_prerender event I simply make one of the controls visible. The control is setup like following:
<asp:TemplateField HeaderText="Default Value" SortExpression="DefaultValue">
<ItemTemplate>
<asp:TextBox ID="txt_defaultValue_view" runat="server" Text='<%# Bind("DefaultValue") %>' Enabled ="false" />
<asp:DropDownList ID="ddl_defaultValue_view" runat="server" Enabled ="false" />
<asp:CheckBox ID="chk_defaultValue_view" runat="server" Enabled ="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_defaultValue_edit" runat="server" Text='<%# Bind("DefaultValue") %>'/>
<asp:DropDownList ID="ddl_defaultValue_edit" runat="server" />
<asp:CheckBox ID="chk_defaultValue_edit" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
My problem starts when I am in edit mode and I need to update the grid with new data. Since only the textbox control is databound, the RowUpdating event can only access data from the textbox column and all of my other data simply gets discarded. I also can't databind with checkbox and dropdownlist control, since they can get an invalid data type exception. So, does anyone know how can I update a column that has three different controls, where each of these three controls might have a valid data?
Thanks
First, i would switch visibility of the controls in RowDataBound of the Grid and not on PreRender, because it can only change on DataBinding and not on every postback.
You can there also bind the Dropdown to its datasource, change the checked-state of the Checkbox and set the Text-property of the Textbox according to the the Dataitem of the Row.
When you update you can find your controls with FindControl in RowUpdating and get their values(f.e. Dropdownlist.SelectedValue).
GridView has pair of events:
RowUpdating/RowUpdated
RowDeleting/RowDeleted
....
In your case your need RowUpdating - is called right before update is performed, So find in row expected control (saying checkbox) and make preparation before pass to database
As another way review possibility to use ObjectDataSource event Updating - it is usual way to specify parameters for query execution.