473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

ObjectDataSource UpdateMethod with integer collection

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
Oct 17 '06 #1
2 3900
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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

Oct 18 '06 #2
Hi Steven

Thanks for the information. That's very helpful.

Thanks
Andrew
Oct 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jim Hammond | last post by:
I am passing the whole object instead or parameters in my select and update methods. I can get the updated object if I set UpdateMethod, let ASP.NET autogenerate an update button, and then press...
3
by: mthomason | last post by:
I keep getting this error when trying to update records using an ObjectDataSource. I have seen others post similar errors...but I haven't found any solutions. ObjectDataSource...
3
by: CWWong | last post by:
I am using DetailsView using ObjectDataSource with DataSourceTypeName assigned to the specific class. SelectMethod, UpdateMethod and DeleteMethod is working successfully, except InsertMethod. The...
0
by: Jai | last post by:
Hi, Somebody please tell me how to bind(two way) a checkboxlist with objectdatasource if the checkboxlist is inside a formview..... Code of FormView is like this::--- <asp:FormView...
0
by: Jai | last post by:
Hi, Somebody please tell me how to bind(two way) a checkboxlist with objectdatasource if the checkboxlist is inside a formview..... Code of FormView is like this::--- <asp:FormView...
0
by: avital | last post by:
Hi, Hope someone can please help. I have a Gridview with a checkboxlist. On edititemTemplate I want the checkboxlist items to be selected as returned from the database. In the database I...
4
by: tim.cavins | last post by:
I have a GridView populated by an ObjectDataSource. I am having issues passing the parameters to the objectdatasource. I have verified that the method is being called but none of the parameters...
9
by: Kernel Bling | last post by:
Hi Everyone, This Saturday the stage was set. The problem simply could not go on existing -- it had to be solved. Many hours, articles, compilations and frustrations later I still did not find...
0
by: steve | last post by:
I have been fighting with trying to update a GridView for a while. I don't want to use the "built-in" way to do it because I am using business layer methods for updating and deleteing and I don't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.