I was wondering if there is a way to use the new model binding in combination with a dropdownlist. When searching on the net, all I found were examples of the DropDownList being used a filter control or e.g. a bound GridView. Let's say however a user would
be able to select a gender (M/F). For this data I would like the user to be able to select the correct gender from a DropDownList. The code below illustrates in general what I am aiming for:
The first control effectively returns the ID of the selected Gender.
The second control returns an Empty Gender object.
This would however imply that i have to retrieve the Gender object again in code behind. Is there no way to get the second way to return the selected Gender object?
The second control returns an Empty Gender object.
Because you didn't specify DatavalueField and DataTextField for it. The SelectedValue and SelectedText both can deal with string types only, so you can't return the whole object through the selectedvalue property.
Because you didn't specify DatavalueField and DataTextField for it. The SelectedValue and SelectedText both can deal with string types only, so you can't return the whole object through the selectedvalue property.
Absolutely. But since the DropDownList is able to return the entire object when working with Items (~ the ReadOnly property SelectedItem), I would assume there would be another approach to achieve the same result with (Model) Binding.
tim_tegenbos...
0 Points
3 Posts
Model Binding to DropDownList
Apr 20, 2012 03:49 PM|LINK
I was wondering if there is a way to use the new model binding in combination with a dropdownlist. When searching on the net, all I found were examples of the DropDownList being used a filter control or e.g. a bound GridView. Let's say however a user would be able to select a gender (M/F). For this data I would like the user to be able to select the correct gender from a DropDownList. The code below illustrates in general what I am aiming for:
<asp:FormView runat="server" ID="frmClubMembers" DataKeyNames="MemberId" ItemType="Domain.Member" DefaultMode="ReadOnly" InsertMethod="InsertMember" UpdateMethod="UpdateMember" SelectMethod="SelectMember"> <ItemTemplate> <fieldset> ... </fieldset> </ItemTemplate> <EditItemTemplate> <fieldset> <ul> <li> <asp:Label ID="lblFirstName" runat="server" AssociatedControlID="txtFirstName">FirstName:</asp:Label> <asp:TextBox runat="server" ID="txtFirstName" Text='<%#: BindItem.FirstName %>' /> </li> <li> <asp:Label ID="lblLastname" runat="server" AssociatedControlID="txtLastName">LastName:</asp:Label> <asp:TextBox runat="server" ID="txtLastName" Text='<%# BindItem.LastName %>' /> </li> <li> <asp:Label ID="lblGender" runat="server" AssociatedControlID="cmbGender">Gender:</asp:Label> <asp:DropDownList runat="server" ID="cmbGender" SelectMethod="SelectGender" SelectedItem='<%# BindItem.Gender %>' /> </li> </ul> <br /> <ul> <li> <asp:Button ID="btnSave" runat="server" CommandName="Update" Text="Save" /> <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" /> </li> </ul> </fieldset> </EditItemTemplate> <InsertItemTemplate> <fieldset> ... </fieldset> </InsertItemTemplate> </asp:FormView>Is this somehow possible, or do I need another approach?
DROPDOWNList
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: Model Binding to DropDownList
Apr 23, 2012 08:14 AM|LINK
SelectedItem is a readonly property, can you try to use SelectedValue instead ?
<asp:DropDownList runat="server" ID="cmbGender" SelectMethod="SelectGender" SelectedValue='<%# BindItem.Gender %>' />
tim_tegenbos...
0 Points
3 Posts
Re: Model Binding to DropDownList
Apr 23, 2012 05:00 PM|LINK
I have added the following controls the the page:
The first control effectively returns the ID of the selected Gender.
The second control returns an Empty Gender object.
This would however imply that i have to retrieve the Gender object again in code behind. Is there no way to get the second way to return the selected Gender object?
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: Model Binding to DropDownList
Apr 23, 2012 06:11 PM|LINK
Because you didn't specify DatavalueField and DataTextField for it. The SelectedValue and SelectedText both can deal with string types only, so you can't return the whole object through the selectedvalue property.
tim_tegenbos...
0 Points
3 Posts
Re: Model Binding to DropDownList
Apr 24, 2012 10:43 AM|LINK
Absolutely. But since the DropDownList is able to return the entire object when working with Items (~ the ReadOnly property SelectedItem), I would assume there would be another approach to achieve the same result with (Model) Binding.