all groups > asp.net webcontrols > february 2006 >
You're in the

asp.net webcontrols

group:

how to bind dropdownlist selectedvalue to custom type


how to bind dropdownlist selectedvalue to custom type Trapulo
2/27/2006 12:00:00 AM
asp.net webcontrols:
Hello,
I have a detailsView that contains a dropdownlist. The ddl loads data from
an objectdatasource, as the detailsView makes (with an other datasource, of
course).

I need to bind the selected item of my ddl to a property of the object
binded to the main detailsView.
My main object has a property called Graduation, of type Graduation. This
type has two properties: ID and Description. I need to create a ddl with id
value, description text, that may be default selected with the
object.Graduation property value.

I've tried with many ideas, as:
<EditItemTemplate>

<asp:DropDownList ID="ddlGraduation" runat="server"
DataSourceID="dataSourceGraduations"

DataTextField="Description" DataValueField="ID" SelectedItem='<%#
Bind("Graduation") %>' EnableViewState="False"></asp:DropDownList>

</EditItemTemplate>


or SelectedValue='<%# Bind("Graduation.id") %>'

The only that works is using Eval ("Graduation.ID"), but this cannot update
data when going to save changes.
Is there any way I can define this databinding in declarative mode, without
write custom code inside control's event handlers?

thanks



RE: how to bind dropdownlist selectedvalue to custom type stcheng NO[at]SPAM online.microsoft.com
2/28/2006 12:00:00 AM
Hi Trapulo,

Welcome to the MSDN newsgroup.

As for the DetailsView/ObjectDataSource binding problem. I think currentlly
due to the limitation of the "Bind" expression, we can not direclty make
the <%# Bind() %> expression refrence a nested property in the DataObject
like direct property. Based on my research, what we have are the following
two means to workaround the problem:

1. Make our DataObject expose another direct public property which can be
used by the "bind" expression instead of the nested class property.

2. We use "Eval" expression to read the data in ItemTemplate and
EditTemplate, however, for updating, we need to register the DetailsView's
"ItemUpdating" event and manually add the nested property's parameter into
the Parameter collection so as to suit the Update Method's parameter
requirement. For example, we have the following DetailsView, the "Category"
property of the data object is a class type which contains another property
(CategoryID).

======================================
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
DataSourceID="ObjectDataSource1"
Height="50px" Width="125px" AutoGenerateRows="False"
OnItemUpdating="DetailsView1_ItemUpdating">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Category">
<EditItemTemplate>

<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Eval("Category.CategoryID")
%>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Category") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Eval("Category.CategoryID")
%>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField ShowEditButton="True" />

</Fields>
</asp:DetailsView>
=====================================

We use the "Eval" expression to display the Nested property. And manually
use the ItemUpdating event to Add the argument into the parameter
collection like below:(you need to extract the value from DetailsView's
certain row and cell instead of using the hard code value in my case)

===========================
protected void DetailsView1_ItemUpdating(object sender,
DetailsViewUpdateEventArgs e)
{
foreach (string key in e.NewValues.Keys)
{
Response.Write("<br>" + key + ": " + e.NewValues[key]);
}

//
e.NewValues.Add("Category", 3);
}
============================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Re: how to bind dropdownlist selectedvalue to custom type Trapulo
3/2/2006 5:45:27 PM

[quoted text, click to view]

Hi Steven,

[quoted text, click to view]

This is not a really available option, because I cannot modify all my
objects model only for a limit of presentation layer.

[quoted text, click to view]

I understand. This is sure the approach I'll use. However it's a way more
like ASP.NET 1.1 that new declative databinding features of 2.0 :)
Now Eval can solve nested properties, and this is't very good (in fact is't
quite simple to obtain this, so this is more a missing of prev release that
an improvements), so I'll wait for ASP.NET 3.0 where either Bind() will make
this, ok? :)

thank you

Re: how to bind dropdownlist selectedvalue to custom type stcheng NO[at]SPAM online.microsoft.com
3/3/2006 1:37:23 AM
Thanks for your followup Trapulo,

Yes, I agree with you that this is a reasonable feature and what a pity
that it is not included in the current release. Not sure whether it'll be
enhanced in sequential version or in a SP. However, I do recommend you
submit this request to the MSDN feedback center:

http://lab.msdn.microsoft.com/productfeedback/default.aspx

Also, you can go to the www.asp.net site and post some comments there since
some ASP.NET dev team's members will often go there.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
AddThis Social Bookmark Button