ASP.net C#, entity, linq

Radim88
Radim88 used Ask the Experts™
on
Hello,

I am trying to create easy web page, but i still have problems with postback. There is one gridview and one details view and easy search (textbox). If I write something into textbox It will find it for me and shows it in the gridview, but if I click the select on the gridview, gridview loads back from previous state. I am addidng code:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;



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

        BindData();  
        if (!IsPostBack)
        {

                   

        }
    }
    
   

    private void BindData()
    {

        DataClassesDataContext myCont = new DataClassesDataContext();

        var query = from c in myCont.Customers
                    select c;

        GridView1.DataSource = query.ToList();
        GridView1.DataBind();

    }


   
    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        GridView1.DataBind();
    }
    

    
    
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
       
        DataClassesDataContext myCont = new DataClassesDataContext();
        var query2 = from c in myCont.Customers
                     where c.Fname.StartsWith(TextBox2.Text)
                     //where  SqlMethods.Like( c.Fname, firstName)
                     select c;

        GridView1.DataSource = query2;
        GridView1.DataBind();
        TextBox2.Text = "";
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {

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

        
        DataClassesDataContext myCont = new DataClassesDataContext();
        var query2 = from c in myCont.Customers
                     where c.Lname.StartsWith(TextBox3.Text)
                     //where  SqlMethods.Like( c.Fname, firstName)
                     select c;

        GridView1.DataSource = query2;
        GridView1.DataBind();
        TextBox3.Text = "";

    }

    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
    }

    
   
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.EditIndex = -1;
        GridView1.SelectedIndex = -1;
    }


   




}

Open in new window


Please advice how can it be done.

Many thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                         BindData();                    

        }
    }
    

Open in new window

Author

Commented:
I tried it already , after refresh the gridview completely vanish.
The problem is that you're calling the BindData() method all over your code. And every time you call this method the search is lost.
What you should do is
1.- Store your search parameters in a viewstate variable (in order to keep the data between postbacks)
2.- Include the search within the BindData() method
This way anytime the binddata is called it will keep the search
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Pls would you help with this code? I am learning it via creating this page and some stuff are for me the spanish village:-). I enabled viewstate on all controls but seems it doesnt help.

Many thanks
Something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        DataClassesDataContext myCont = new DataClassesDataContext();

        if (ViewState["searchTerm"] == null)
        {
            var query = from c in myCont.Customers
                        select c;
            GridView1.DataSource = query.ToList();
        }
        else{
            var query = from c in myCont.Customers
                        where c.Fname.StartsWith(ViewState["searchTerm"].ToString())
                        select c;
            GridView1.DataSource = query.ToList();
        }

        GridView1.DataBind();
    }

    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        GridView1.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ViewState["searchTerm"] = TextBox2.Text;
        BindData();
        TextBox2.Text = "";
    }

    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
    }
}

Open in new window

Author

Commented:
Thanks thats it, and maybe how to propagate these changes into Detailsview.
I have there:

protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        DetailsView1.PageIndex = GridView1.SelectedIndex;
    }

But it seems this doesnt work for this case.

And also how to do for both Lname and Fname.

Many thanks

Author

Commented:
thx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial