entitydatasource value retrieval - asp.net

I have an entitydatasource defined in the markup as so
<asp:EntityDataSource ID="edsFV" runat="server"></asp:EntityDataSource>
<asp:EntityDataSource ID="eds_fv_singleuserprofile" runat="server"
ConnectionString="name=webEntities" DefaultContainerName="webEntities"
EnableFlattening="False" EnableUpdate="True" EntitySetName="userprofiles"
Where="it.ASPUserId = #SelectedValue" >
<WhereParameters>
<asp:Parameter DefaultValue="20" Name="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
I it requeries for information on a dropdown selection. It returns a single item with 5 properties. I am trying to acces one of the properties, "FullName" to use in the legend of the fieldset within the update panel this dropdown is changing data for as follows;
<asp:UpdatePanel ID="udpUserProfile" runat="server" >
<ContentTemplate>
<fieldset id="singleuserprofile" >
<legend>Profile details for <%# I want to databind the "FullName" property value here%></legend>
"other code"
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
I cannot seem to find the method that works properly to do so. Any help appreciated.

While I haven't found anything explicitly stating so, it is my understanding that Data Sources are used solely to interface with data-bound controls. From the MSDN page:
Data source controls greatly expand the capabilities of data-bound
controls such as the GridView, FormView, and DetailsView controls. By
working together, data source controls and data-bound controls let you
retrieve, modify, page, sort, and filter data from different data
sources with little or no code.
You should probably get the data for the fieldset tag with an Entity query, and use the entitydatasource with your DetailsView only.
As best I can understand, you are using a DropDownList with AutoPostBack to select the item in the DetailsView. If, as your comment implies, the DetailsView is not updating when you do the partial postback you should call a .DataBind() on the control. Also, using a ControlParameter instead of a basic Parameter may be more appropriate in this context.

Related

Dropdownlist looses value during autopostback

I have a couple of straightforward dropdownlists. The first is bound by a linqdatsource in the html with autopostback set to true. The selection of the first determines the data in the second. When I select an item in the first, the selectedindexChanged event fires; however, the selected value is always the first item in the list and the list then re-binds and reverts to its default state. Do I have to bind it in code-behind to prevent this?
<asp:DropDownList ID="dd_shirtcolor" runat="server" AppendDataBoundItems="true" AutoPostBack="True">
<asp:ListItem Text="Select Color" />
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="IPC.IPCDataDataContext" EntityTypeName=""
TableName="Shirts" Where="IsActive == #IsActive">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="IsActive" Type="Boolean" />
</WhereParameters>
</asp:LinqDataSource>
Make sure you have ViewState enabled so it can populate the list before it "selects" the item.
Also, make sure you don't repopulate in Page_Load and lose the selected value.
eg.
if (!IsPostback) { // Populate }
Some alternatives to managing cascading drop down lists yourself are:
ASP.NET AJAX Control Toolkit:
ASP.NET AJAX Cascading Drop Down
Cascading Drop Down Using ASP.NET and jQuery:
Cascading Drop Down Using ASP.NET And jQuery
I was able to work around this problem by binding the dropdown in the code behind in page_init method with the proper !postback conditional instead of using the linqdatasource to bind it. I am still not sure what was causing it however.

Dynamically add items to DropDownList but still allow for a field that has no value?

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."

asp.net dropdownlist embedded in a listvew not providing selected value to update parameter

I have an asp.net page with a listview which is bound to an SQLDataSource. Embedded into the Edit and Insert templates I have a dropDownList which is bound to a second SQLDataSource. In the update parameters I have specified the corresponding parameter to use the Selected Value of the DropDownList (At least I think I have). Unfortunately the selected value is not making it through into the update. If I try to refer the the dropdowlist control explicitly and try to do the update or Insert, the runtime complains that there are too many parameters being passed to the stored procedure used to do the update/insert. Code snippets are below.
Edit Template
<td class="GridContent">
<asp:DropDownList
ID="lstDataType"
runat="server"
DataSourceID="DataType"
DataTextField="Name"
DataValueField="Name"
CssClass="GridContent">
</asp:DropDownList>
Control Parameter (Using the ListView control - Doesn't provide the selected value)
<asp:ControlParameter
ControlID="lstFields"
Name="DataType"
PropertyName="SelectedValue"
Type="String" />
Control Parameter (using the DropDownList control - Update complains of too many parameters)
<asp:ControlParameter
ControlID="lstTypeID"
Name="Name"
PropertyName="SelectedValue"
Type="String" />
So I suppose my question is how do I get the Control Parameter for the update to reference the selected value of the embedded dropdownlist.
UPDATE
In the end it took me a while to find the answer, but it is quite straight forward; although finding the controls at the bottom of the ListView control tree was a complete nightmare.
What I did was add a hidden label control into the update and insert templates and bound this to the SQLDataSource for the listview. Then I grabbed the FindControlRecursive function that's in some sample code on the MSDN site at the bottom of the article here:
http://msdn.microsoft.com/en-us/library/y81z8326.aspx
Then in the Page_Load method for my page I added two calls the the FindControlRecursive function, one to get the label and the other to get the dropdownlist.
Then simply assigned the DropDownList.text value to the Label.text and it all worked beautifully.
I've been doing another listview with an embedded dropdownlist and made an interesting discovery (Well I think so). It seems that it isn't necessary to have the code behind to assign the text for the selected item to the label control. All that is necessary is for the hidden label and the drop down to both be bound to the appropriate field in the DataSource.

A field or property with the name was not found on the selected data source

I have an Entity Data Model with two entities in it "Roles" and "Users". There is a navigation property from I have a EntityDataSource and a GridView. The EntityDataSource points to the Users entity and has an Include="Roles" parameter.
I've added a BoundField in the GridView that points to RoleName, a property of the entity Roles. However, when I execute the code I get the above error.
I have used very similar code successfully in another application. Any ideas why this isn't working?
Here is my EntityDataSource:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=pbu_checklistEntities"
DefaultContainerName="pbu_checklistEntities" EnableDelete="True"
EnableFlattening="False" EnableUpdate="True" EntitySetName="Users" Include="Role">
</asp:EntityDataSource>
And here is the BoundField:
<asp:BoundField DataField="RoleName" HeaderText="Role" SortExpression="RoleName" />
You cannot use an asp:BoundField for a property of a related navigation property. You can only use an asp:TemplateField and then bind it as readonly with Eval (not Bind). BoundFields are always using Bind internally, that's the reason why it doesn't work. I had to figure this out myself some time ago:
Columns of two related database tables in one ASP.NET GridView with EntityDataSource

Automatically rebind controls on ASP.NET postback

Is there a flag or property value I can set to have my controls re-bind on every page load, instead of just the initial one? I'm still very new to ASP.NET, so I would like to do it properly (if such a way exists) before resorting to the first thing that "works".
I am working on a simple WebForms page, which boils down to a few SQLDataSource-bound Repeaters; for example:
<asp:Repeater ID="ExampleRepeater" runat="server" DataSourceID="ExampleDataSource"
OnItemDataBound="ExampleRepeater_ItemDataBound">
<ItemTemplate>
<asp:Label ID="DataboundControlID" runat="server" Text='<%# Eval("ExampleColumnName")%>' />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="ExampleDataSource" runat="server" ConnectionString="example_connection_string"
SelectCommand="ExampleStoredProcedure" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="exampleValue" Name="parameter1" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
After the user has interacted with my page, they can initiate a postback which will change the state of the database. I'd like the postback'ed page to reflect the changes to the database. Please let me know if I can clarify my situation any further.
After your processing i.e. updating database you can re-bind the Repeater by calling DataBind method like:
ExampleRepeater.DataBind();
In my experience, some controls do, others don't. E.g. the standard ASP.NET controls from Microsoft always worked for me automatically, whereas the ASPxGridView from DevExpress needs a manual rebind on every postback/callback.

Resources