PC Review


Reply
Thread Tools Rating: Thread Rating: 6 votes, 2.33 average.

Asp.net, GridView and Linq on Array of objects

 
 
James
Guest
Posts: n/a
 
      29th Feb 2008
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="(E-Mail Removed)", Location="5100 Town Cntr Circle"},
new AContact {ContactId=1389, FirstName = "Ada", LastName = "peterson", Company="Oracle", E_mail="(E-Mail Removed)", Location="1364 Park Street"},
new AContact {ContactId=1390, FirstName = "Paul", LastName = "wellby", Company="Oracle", E_mail="(E-Mail Removed)", Location="54 State St Suite 1000"},
new AContact {ContactId=1391, FirstName = "Damien", LastName = "Johnson", Company="Google", E_mail="(E-Mail Removed)", 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;
}
}



 
Reply With Quote
 
 
 
 
David R. Longnecker
Guest
Posts: n/a
 
      29th Feb 2008
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)
> {
> var ContactList = (from contacts in
> GlobalListOfOutLookContacts where (contacts.ContactId == cid) select
> contacts).First();
> 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.
>
> 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="(E-Mail Removed)", Location="5100 Town Cntr Circle"},
> new AContact {ContactId=1389, FirstName = "Ada", LastName
> = "peterson", Company="Oracle", E_mail="(E-Mail Removed)",
> Location="1364 Park Street"},
> new AContact {ContactId=1390, FirstName = "Paul", LastName
> = "wellby", Company="Oracle", E_mail="(E-Mail Removed)",
> Location="54 State St Suite 1000"},
> new AContact {ContactId=1391, FirstName = "Damien",
> LastName = "Johnson", Company="Google",
> E_mail="(E-Mail Removed)", 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;
> }
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linq and gridview sorting David Microsoft C# .NET 2 18th Aug 2009 12:30 PM
LINQ and Gridview Arne Garvander Microsoft ASP .NET 2 16th Oct 2008 01:27 PM
Linq problem in GridView (related field) Raynald Microsoft ASP .NET 0 12th Aug 2008 10:09 AM
GridView LINQ double foreign key Chuck P Microsoft ADO .NET 5 19th Jun 2008 09:59 AM
Binding GridView to dynamic LINQ queries zainab Microsoft ASP .NET 0 31st Dec 2007 07:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:32 AM.