Link to home
Start Free TrialLog in
Avatar of Radim88
Radim88

asked on

ASP.net C#, entity, linq

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
Avatar of Maulik Modi
Maulik Modi

Try this:

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

        }
    }
    

Open in new window

Avatar of Radim88

ASKER

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
Avatar of Radim88

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of JuanchoVzla
JuanchoVzla
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Radim88

ASKER

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
Avatar of Radim88

ASKER

thx