ObjectDataSource UpdateMethod with integer collection

J

J055

Hi

I have a method with the following signature:

public bool UpdateSetAccounts(int[] items) { }

How do I pass a collection of selected values in a ListBox to this
ObjectDataSource UpdateMethod?

I can pass the ListItemCollection in a ControlParameter, e.g.

<UpdateParameters>
<asp:ControlParameter ControlID="lbxAssignees" Type="object"
PropertyName="Items" Name="items" />
</UpdateParameters>

Not sure how to deal with it in the Updating event, e.g.

protected void odsSetAccounts_Updating(object sender,
ObjectDataSourceMethodEventArgs e)
{
ListItemCollection items = (ListItemCollection)e.InputParameters[0];
// what happens next?
}

I must be missing something obvious in the documentation. Handling multiple
selected values in ListBoxes, CheckboxLists, etc must be fairly standard. Is
there a recommended way of using these with ObjectDataSource controls?

Thanks
Andrew
 
S

Steven Cheng[MSFT]

Hello Andrew,

For your scenario, your data access class's update method will take a int[]
input parameter and you use it in ObjectDatasoruce, and the parameter
source is a ListBox(configuer as multi-select ), I think you need to take
care of the following things:

1. You should not directly associate the ListBox.Items to
ObjectDataSource's update parameter, because they're unmatched on type(one
is ListItemCollection, the other is int[])

2. You're right that you need to manually pass in the paramter through
ObjectDataSource's Updating event

Here is a simple example demonstrate pass parameter from ListBox's selected
items into my custom object's update method through objectDatasource:

=========custom class=========
public class DataAccessObject
{
public DataAccessObject()
{

}

public int[] GetItems()
{

HttpContext.Current.Response.Write("<br/>DataAccessObject.GetItems....");
return new int[] { 1,2,3,4,5,6,7,8,9};
}


public void UpdateItems(int[] items)
{

HttpContext.Current.Response.Write("<br/>DataAccessObject.UpdateItems...."
+ items.Length);
}
}
============================


===========aspx==============
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetItems" TypeName="DataAccessObject"
UpdateMethod="UpdateItems" OnUpdating="ObjectDataSource1_Updating">
<UpdateParameters>
<asp:parameter Name="items" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ListBox ID="ListBox1" runat="server"
DataSourceID="ObjectDataSource1" Height="286px" SelectionMode="multiple"
Width="167px"></asp:ListBox><br />
<asp:Button ID="btnUpdate" runat="server" Text="Update"
OnClick="btnUpdate_Click" /></div>
</form>
==============================

=========code behind==========
public partial class dataaccess_ObjArrayDS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpdate_Click(object sender, EventArgs e)
{

ObjectDataSource1.Update();
}
protected void ObjectDataSource1_Updating(object sender,
ObjectDataSourceMethodEventArgs e)
{
ArrayList list = new ArrayList();
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected == true)
{
list.Add(int.Parse(item.Value));
}
}

int[] items = (int[])list.ToArray(typeof(int));

e.InputParameters["items"] = items;

}
}


==============================


Actually, for such scenario, it's more straightforward that we directly
call the custom class's method instead of ObjectDataSource.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top