Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

You might have already played around with the ObjectDataSource control and found out that you can send individual values to the class methods. You can also send the object itself to the class method. Take a look at the code below which sends the object filled with data being updated to the class method.

User.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

public class User
{
    
private int _userID;  
    
private string _firstName;
    
private string _lastName;
    

    
public int UserID
    {
        
get return _userID; }
        
set { _userID = value; } 
    }

    
public string FirstName
    {
        
get return _firstName; }
        
set { _firstName = value; } 
    }

    
public string LastName
    {
        
get return _lastName; }
        
set { _lastName = value; } 
    }


    
public User()
    {        
    }

   
public User(string firstName, string lastName) 
   {
       _firstName = firstName; 
       _lastName = lastName;  
   }



    
public void UpdateUser(User updatedUser)
    {
        
// Update the user here 
    
}


    
public List<User> GetUsers()
    {
        List<User> userList = 
new List<User>(); 

        
for (int i = 1; i <= 10; i++)
        {
            User user = 
new User("FirstName" + i, "LastName" + i);
            userList.Add(user); 
        }

        
return userList; 

    }


}

And then we have the code for the ObjectDataSource control and the GridView control.

 <asp:GridView DataKeyNames="UserID"
 ID="MyGridView" DataSourceID="objUser" runat="server" BackColor="White"
                BorderColor="#CC9966" 
                BorderStyle="None"
                BorderWidth="1px" CellPadding="4" Font-Names="Verdana"
                Font-Size="Small" AllowPaging="True">
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <RowStyle BackColor="White" ForeColor="#330099" />
                <SelectedRowStyle BackColor="#FFCC66"
                Font-Bold="True" ForeColor="#663399" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
                HorizontalAlign="Center" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" 
                ForeColor="#FFFFCC" />
                <Columns>
                    <asp:CommandField ShowEditButton="True"
                    ShowSelectButton="True" />
                </Columns>
            </asp:GridView>
            

<asp:ObjectDataSource 
            ID="objUser" UpdateMethod="UpdateUser"
            DataObjectTypeName="User" TypeName="User"
                runat="server" SelectMethod="GetUsers">                               
                </asp:ObjectDataSource>

This will send the updated values from the GridView row (Which is being updated) to the UpdateUser method in the User.cs class. The only problem right now is that you need to set AutoGenerateColumns=True for this to work. If you like to use the Template Columns then you might have to use the ControlParameters or the Parameters property of the ObjectDataSource control.

 

 

 

 

 

 

 

powered by IMHO 1.3

Posted on Sunday, April 9, 2006 10:11 PM | Back to top


Comments on this post: Populating the custom object from GridView control using ObjectDataSource control

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net