Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Asp.net, GridView and Linq on Array of objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James - 29 Feb 2008 20:14 GMT
I've got a GridView control on a simple web page in ASP.net 3.5.

I've an Array of Employees. An Employee has First Name, Last Name, IsActiveEmployee etc

My GridView control is Bounded to the Array: DataSource = myarray, gridview1.DataBind()

I have template column with holds a link button

when the button is clicked, I've got a method which will set the selected

employee "IsActiveEmployee"  flag to false.

My problem is that when the page reloads and binds, the array returned is NOT change - it is the same array.  

Can somebody please help?

My Page file is defined as:

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
       {
         
           this.GridView1.DataSource = Utils.GetGlobalListOfOutlookContacts();
           this.GridView1.DataBind();
       }

       protected void MarkAsPrivate(int id)
       {
           int tmpId = id;
           Utils.MarkPrivate(tmpId);
       }

protected void LinkButton1_Command(object sender, CommandEventArgs e)
       {
           object o = e.CommandArgument;
           String s = Convert.ToString(o);
           int tmpId = Convert.ToInt32(s);            
           Utils.MarkPrivate(tmpId);
       }

}

My Utils file is defined as:

public static class Utils
   {
       public static readonly String SPACE = " ";
       private static List<AContact> GlobalListOfOutLookContacts = new List<AContact>()
       {
           new AContact {ContactId=1388, FirstName = "Kriss", LastName = "Bellamy", Company="Oracle", E_mail="kriss.bellamy@oracle.com", Location="5100 Town Cntr Circle"},
           new AContact {ContactId=1389, FirstName = "Ada", LastName = "peterson", Company="Oracle", E_mail="ada.peterson@oracle.com", Location="1364 Park Street"},
           new AContact {ContactId=1390, FirstName = "Paul", LastName = "wellby", Company="Oracle", E_mail="paul.wellby@oracle.com", Location="54 State St Suite 1000"},
           new AContact {ContactId=1391, FirstName = "Damien", LastName = "Johnson", Company="Google", E_mail="damien.johnson@oracle.com", Location="1 Atlon Court Paris"}
       };

public static List<AContact> GetGlobalListOfOutlookContacts()
       {
           var ContactList = from contacts in GlobalListOfOutLookContacts where contacts.IsPrivateContact != true select contacts;

           List<AContact> retVal = new List<AContact>();
           foreach (AContact c in ContactList)
           {
               retVal.Add(c);

           }
           return retVal;
       }

       public static void MarkPrivate(int cid)
       {
           var ContactList = (from contacts in GlobalListOfOutLookContacts where (contacts.ContactId == cid) select contacts).First();
           ContactList.IsPrivateContact = true;
       }
}
David R. Longnecker - 29 Feb 2008 21:36 GMT
James-

When you query out an object using LINQ and then update it, you must submit
it back to the data source.

I'm assuming your LinkButton1_Command is what is toggling your IsActiveEmployee
(IsPrivateContact?).

> public static void MarkPrivate(int cid)
> {
[quoted text clipped - 3 lines]
> ContactList.IsPrivateContact = true;
> }

After you update your ContactList object here, add in:

TheNameOfYourDataContextObject.SubmitChanges();  // If you've build your
own Update method, you could fire that off instead.
gridview1.DataBind(); // To rebind your data to the updated data source.

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

> I've got a GridView control on a simple web page in ASP.net 3.5.
>
[quoted text clipped - 80 lines]
> ContactList.IsPrivateContact = true;
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.