How can I use the dropdownlist to populate/enter data? - asp.net

Here's my existing code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="dsEmployees" DataTextField="Last_First"
DataValueField="EmpNum"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

Yonita, you need some more moving parts. You need a datasource to bind to, for instance.
Have a look at the Data Access tutorials on the ASP.NET website:
http://www.asp.net/learn/data-access/

DropDownLists end up being rendered as <select> tags in HTML. These do not allow for data entry of new values. If you're looking for something similar to a ComboBox (a textbox and a listbox combined into the same control) then you'll need to find a third-party javascript implementation that is acceptable to you; there is no control which provides that functionality in HTML or the .net framework.

Related

entitydatasource value retrieval

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.

AssociatedControlID for RadListBox and RadDateTimePicker controls?

I am using RadListBox and RadDateTimePicker in a traditional C# ASP.Net 3.5 web application, and I have labels like this:
<asp:Label ID="lblStartDate" runat="server" Text="Start Date" AssociatedControlID="dtpStartDate" />
<telerik:RadDateTimePicker ID="dtpStartDate" runat="server">
</telerik:RadDateTimePicker>
The label doesn't connect to the date picker with a for attribute because the picker is a complex control and it can't work out which element to target. Telerik propose a JavaScript solution to this, but that seems unwieldy to me and also depends on client side scripting.
Is there a better way which will provide a reasonable level of accessibility? Either wrapping the Telerik solution somehow or an altogether alternative approach?
Yep, the Telerik picker is a composite control as well and similar javascript to attach the label to the date input should do the trick. To reference the input field, use the get_dateInput() property from the client API of the picker.

Various asp controls in a ASP.NET page

I am creating a products page, where the user selects an option in a radiobuttonlist for example, and then a control with the various options of that product appears in a placeholder or in a div when on of the radiobuttons is selected.
At the moment this is the code:
aspx:
<form runat="server">
<asp:CheckBoxList ID="Lentes" runat="server" OnClick="EscolheLentes">
<asp:ListItem Value="LU">
Lentes Unifocais
</asp:ListItem>
<asp:ListItem Value="LP">
Lentes Progressivas
</asp:ListItem>
</asp:CheckBoxList>
<asp:PlaceHolder runat="server" ID="PHLentes"></asp:PlaceHolder>
</form>
aspx.vb:
Protected Sub EscolheLentes()
Dim ControlLente As Control
If (Me.Lentes.Items.FindByValue("LU").Selected) Then
ControlLente = LoadControl("LentesUnifocais.ascx")
ElseIf (Me.Lentes.Items.FindByValue("LP").Selected) Then
ControlLente = LoadControl("LentesProgressivas.ascx")
End If
Me.PHLentes.Controls.Add(ControlLente)
End Sub
Need to use some ajax to load the control right?
Am i going in the right direction?
Thanks.
There are several ways to achieve that:
True ASP.Net Web forms: Do postbacks with AutoPostback and play with the visibility of the other controls
Javascript: Load all the data possibly displayed with the page and handle the conditional display with javascript. This is only reasonable if the amount of data to display on one page is somewhat limited. You may want to look into JQuery or something similar if you go that way.
Ajax: load asynchronously only the bits you need. You may use the MSAjax framework, or Jquery (or similar) to do the client side code.
The first option is probably the fastest one to implement.
Have you tried adding AutoPostBack="true" and Visible="true" on your control?

Two-way data binding of controls in a user control nested inside a FormView doesn't work

I'm trying to perform two-way data binding on the controls in my user control, which is hosted inside a FormView template:
<asp:ObjectDataSource runat="server" ID="ObjectDataSource"
TypeName="WebApplication1.Data" SelectMethod="GetItem" UpdateMethod="UpdateItem">
</asp:ObjectDataSource>
<asp:FormView runat="server" ID="FormView" DataSourceID="ObjectDataSource">
<ItemTemplate>
<uc:WebUserControl1 runat="server"></uc:WebUserControl1>
</ItemTemplate>
<EditItemTemplate>
<uc:WebUserControl1 runat="server"></uc:WebUserControl1>
</EditItemTemplate>
</asp:FormView>
The web user control:
<%# Control Language="C#" ... %>
<asp:TextBox runat="server" ID="TitleTextBox" Text='<%# Bind("Title") %>'>
</asp:TextBox>
The binding works fine when the FormView is in View mode but when I switch to Edit mode, upon calling UpdateItem on the FormView, the bindings are lost. I know this because the FormView tries to call an update method on the ObjectDataSource that does not have an argument called 'Title'.
I tried to solve this by implementing IBindableTemplate to load the controls that are inside my user control, directly into the templates (as if I had entered them declaratively). However, when calling UpdateItem in edit mode, the container that gets passed into the ExtractValues method of the template, does not contain the TextBox anymore. It did in view mode!
I have found some questions on SO that relate to this problem but they are rather dated and they don't provide any answers that helped me solve this problem.
How do you think I could solve this problem? It seems to be such a simple requirement but apparently it's anything but that...
My current workaround for this is, although rather cumbersome, to subclass the FormView class and use subclassed controls in it, implementing my own data binding logic (taking the data field name from a new property) instead of using the <%# %> syntax. Apparently, the code the latter generates is the real culprit here as it doesn't support this nested control scenario.
I ended up, using old asp include statement
<--%include file = "filename" -->
instead of using user controls for dealing with the code duplication issue.

ASP.Net - Two way databinding of a single entity - Options, best way...roll your own?

I have been searching around to find the best option of doing this.
Basically, I want to do two way databinding of multiple controls (textbox, dropdownlist, checkbox, etc) to a single instance of custom class/entity, ie: Person.
It seems, the (only?) way to do this is using an like so:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="OrderID"
DataSourceID="SqlDataSource1" DefaultMode="Edit">
<EditItemTemplate>
OrderID:
<asp:Label ID="OrderIDLabel1" runat="server" Text='<%# Eval("OrderID") %>' />
<br />
CustomerID:
<asp:DropDownList ID="CustomerIDDropDownList" runat="server"
DataValueField='CustomerID' DataSourceID="CustomerDataSource"
DataTextField="CompanyName"
SelectedValue='<%# Bind("CustomerID") %>'
/>
EmployeeID:
<asp:TextBox ID="EmployeeIDTextBox" runat="server"
Text='<%# Bind("EmployeeID") %>' />
<br />
Some issues:
- This is limited to using an ObjectDataSource control (ie: can't just use an instance of the desired class in the code behind)
- Forces you to define a second (likely identical layout) read only template....would be nice to have some some mechanism that could intelligently render a read-only view derived from the edit template.
- The binding declaration Text='<%# Bind("EmployeeID") %>' is loosely typed, so vulnerable to spelling errors
- etc
So my first question I guess is, is an asp:FormView the only way in ASP.Net to do declarative databinding of a single entity?
Secondly, how feasible would it be to hand roll some sort of a two way binding mechanism? I guess it would have to be reflection based, but I could live with that. Anyone recommendations on how one would declare the binding relationships in the aspx page? Would the proper way be like:
Text='<%# MySuperDuperBind("EmployeeID") %>'
And then somewhere (where?) my MySuperDuperBind implementation will get called as the page is rendered....how this is done is a bit beyond me though. And if I want to render in readonly, I can call a secondary function that will remove the editable UI control from the form and replace it with the corresponding read only version (ie: a Textbox is replaced with a Label).
Another alternative route is getting away from webforms and going to a client side templating solution such as this very nice looking solution:
http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx
However, I have no clue how to write the asp.net webservices properly in order to retrieve and save data in this type of an architecture.
Ideas?
is an asp:FormView the only way in
ASP.Net to do declarative databinding
of a single entity?
There's also DetailsView but it has the same issues.
I've mostly given up on 2-way databinding. It's great for prototyping and gets me 80-90% of the way to a complete solution but the last 10-20% is a nightmare. Binding any non-trivial object always seems to involve so many event handlers to customize behavior that it feels like spaghetti code to me.
I usually have two methods:
MapEntityToView(entity)
MapViewToEntity(entity)
that I call to display the entity and to populate from the page, respectively. It can be tedious to write but I don't have to wrestle with data binding issues.
I do use 1-way binding extensively for read only pages and displaying items in list controls.

Resources