0

I am a new ASP.NET Web Forms developer and I am struggling now in with the best way of filtering the data by the value of QueryString before binding it to the GridView control. I am binding the GridView to the GetData() method, and I would like to filter the data in the code-behind based on the value of the QueryString if there is a QueryString. So should I do the checking of the QueryString in the BindGrid() method or in the Page_Load() method? And how should I do it?

For your information, the GridView has a pagination capability as shown in the code-behind below.

Here's the C# code for the GetData():

public IEnumerable<Item> getData(Item itemObj)
    {
        List<Item> itemList = new List<Item>();
        using (ATMSEntities context = new ATMSEntities())
        {
            itemList = (from item in context.Item
                       select new Item()
                       {
                           ItemId = item.ItemId,
                           Name = item.Name,

                       }).ToList();
            if (itemObj.ItemId != 0)
            {
                itemList = itemList.Where(item => item.ItemId == itemObj.ItemId).ToList();
            }

        }
    }
        return itemList;
}

And here's the code-behind for the aspx page that has the GridView control:

protected void Page_Load(object sender, EventArgs e)
{
    if(Request.QueryString["ItemId"] != null) //the filtration is not working here.
    {
        bindGrid();
    }
}

    private void bindGrid()
    {
        Item itemObj = new Item();
        var result = itemObj.getData(itemObj).ToList();
        gvItems.DataSource = itemObj.getData(itemObj);
        gvItems.DataBind();
    }


    protected void gvItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvItems.PageIndex = e.NewPageIndex;
        bindGrid();
    }

Thanks in advance for your help.

Technology Lover
  • 259
  • 1
  • 7
  • 19
  • In your model, can you have multiple items with the same ItemId? – Praveen Paulose Mar 29 '15 at 20:43
  • @PraveenPaulose, No, it can't be. Each item has a unique id. However, this page could be accessible with no querystring, so in this case all the items will be displayed. And sometimes it might be accessible with a querystring, so it the data in gridview should be filtered by this ItemId. – Technology Lover Mar 29 '15 at 20:57

1 Answers1

1

You are not using the QueryString value to filter the list items. What you should be doing is this

private void bindGrid()
{
    Item itemObj = new Item();
    if(Request.QueryString["ItemId"] != null) 
    {
        itemObj.ItemId = Convert.ToInt32(Request.QueryString["ItemId"]);
    }
    var result = itemObj.getData(itemObj).ToList();
    gvItems.DataSource = itemObj.getData(itemObj);
    gvItems.DataBind();
}
Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19
  • what about if I have multiple items with the same id? is it going to be different? if yes, could you please add this case to your solution as well. I really appreciate your help in this regard and thank you very much. – Technology Lover Mar 29 '15 at 21:14
  • The solution will not differ if you have multiple items with the same id. The same logic would work. I hope this answers your query. – Praveen Paulose Mar 29 '15 at 21:21
  • I would appreciate if you could show me the answer of this case as well. – Technology Lover Mar 30 '15 at 16:47
  • I did not understand. I was saying that the same code would work even if you have multiple items with the same id. – Praveen Paulose Mar 30 '15 at 17:55