Paged GridView Only Populates First Page - c#

I have populated a GridView using the following method:
List<MyObject> items = new List<MyObject>();
// here I am filling the list using SQL
PanelGridView.DataSource = items; //fill GridView with objects, this works when NOT using paging
PanelGridView.DataBind();
With this, and paging disabled, I have a fully populated GridView. However, when I turn on paging, the first page is filled, but all subsequent pages are empty. How can I make sure all of the items are accounted for, and divided properly among the pages (given the page size I've specified)?
EDIT: I forgot to include this code:
protected void PanelGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
PanelGridView.PageIndex = e.NewPageIndex;
PanelGridView.DataBind();
}

Make sure you databind the grid after changing the page and also make sure to set the page index to the new page index. This is done in the PageChanging event handler.

Related

asp.net 2nd combobox not taking selectedvalue

I'm loading a record to an aspx page. I have two comboxes. (AJAX comboboxes in this case) The second Loads based on the id from the first.
When I pass a key to the page in the query string I retrieve data into a class, then I populate the page fields from the class. When I set the carrier.selectedvalue = class.1value the selection shows correctly. When I set product.selectedvalue = class.2value the selection does not take. (The product has nothing selected)
If I drop down the list of the product combobox the correct data is loaded based on the the first combox (carrier).
I have tried two methods:
1) query product sql dataset (2nd dataset) based on selected value from the carrier combox
2) Load ALL products (2nd dataset) then FILTER the products based on the value from the carrier combox
Both methods load the 2nd combox with the values I need. Neither method helps me get the product combobox to show the selected value once the page renders.
Note that I'm doing all this in page_load
-Thanks in advance for looking.
The problem is that you are doing everything in Page_Load. Do things in the following order in the following events to prevent your issue:
Page_Init: Populate Carrier Items
Page_Init: Set Carrier selected value
Page_Load: Clear Items Product.Items.Clear()
Page_Load: Populate Product Items
Page_Load: Set Product Selected Items
See this guide for how to effectively use the Page events: http://attemptsatprogramming.blogspot.com/2011/03/practical-guide-to-aspnet-event-model.html
Gthompson83 put me on the right track. It was a databinding issue. I moved the set of the product combobx (the second combobox) to the databound event like so:
protected void cboProduct_DataBound(object sender, EventArgs e)
{
// Set the Product cbo
cboProduct.SelectedValue = c.Product_ID.ToString();
}
That is all it took. The class is still populated in the Page_Load event based on values from the query string. Once the DataBound event is fired i use the values I've placed in the class to set the value of the product combobox.
Some helpful info is here: Databinding events for data-bound controls
On the page linked above there is a good section on "Nested Data-Bound Controls".
Note that I did not follow that example completely... I did not do the databind on the second combobox programmatically... just catching the DataBound event was enough to allow me to set the selectedvalue.

ASP.Net GridView 5000 Records

If a procedure returns 5000 (Five Thousand) rows and I want bind it with a Asp.net Grid View. Neither it won’t be good approach to call all the rows and bind it with grid and then on grid view Pageindex see all the data or this approach to strike the DB at each Pageindex change. So can anybody give me good answer.
Normally your GridView is only presentation. So you only have to query the correct page to display in the GridView. If you go the the next page you query the db for the next page.
This article will give you directions on how to do this with a datasource:
MSDN - Tutorial 25: Efficiently Paging Through Large Amounts of Data
Efficient Server Side Paging with the ASP.NET GridView Control
Session["dt"] = DataTable; //after you get table from procedure, assign it to session
Then in pageload, every time bind grid view to datatable saved in session, you call it from DB just first time
protected void Page_Load(object sender, EventArgs e)
{
GridView.DataSource = Session["dt"];
GridView.DataBind();
}

DropDownList not issues in asp.net

I have two DropDownList which are being populated from a database(SQL) on Page_Load. Now I want to take the Text/Value selected from each DropDownList and insert them into a database. But when I click on the button and the OnClick Event is action the DropDownLists go back to the original state and not the selected values get inserted, instead I get the first Text/Value of the DropDownList all the time. How can I prevent this from happening? Any help will really appreciate it.
I'm using C# for this.
Thanks
In page load, load up the dropdowns like this
protected void Page_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropDowns();
}
}
Basically button click causes postback and if you are populating controls in Load event without check for postback, it will repopulate your controls resetting it's state.
Your DDL is getting rebuilt on every page load. You need to wrap your ddl data source calculation in an
if (!IsPostBack)
or put it in a part of the lifecycle that only loads once, like the OnLoad()

DataBind or DataBound

I thought I had a simple task; add "Select..." to several dropdown lists.
However I am not getting the results I want and I am getting more and more confused if I should be using the dataBound or dataBinding event in my Gridview edit mode.
My code is pretty simple;
protected void ActivityList_DataBinding (object sender, System.EventArgs e)
{
DropDownList ddl2 = (DropDownList)(sender);
var act = Eval("myactivity").ToString();
if (act != "") { ddl2.SelectedValue = act; }
ddl2.Items.Insert(0, new ListItem("Select..", "-1"));
}
This checks if a value has already been selected, and would hopefully jump to the selection if it has been, still adding a Select item to the list.
Using the dataBound event works in the sense it addes my Select, but does not go to the selected value if there is one. Also it is creating odd behavior, jumping to the top of my page upon selection, rather than staying on the row I am editing.
Using dataBinding does not show my added items at all.
All advice welcome!
Alex (Lost in CodeLand)
Set your AppendDataBoundItems=True in your DDL. If you are calling the databind method in code you will want to clear the items and readd the new listitem before databinding.

Is it possible to re-DataBind a DropDownList upon postback triggered by SelectedIndexChanged event?

Let's say I have a DropDownList of product categories and a ListView of products based on the category selection in the DropDownList. When a user visits the page, there is a possibility of conconrency issue as new product belonging to a new category may be added to the inventory as the user is browsing.
When the user selects a different category to view(a SelectedIndexChanged event) and causes a postback, I want the DropDownList to update the list of categories to include the new category being concurrently added and at the same time still able to make a change of selected index. The DropDownList does not seem to support this updating while postback. So how do I handle this concurrent issue?
You need to bind and rebind regardless of checking postback dropdown in Page_Init event. It will not cause any change in selected index of the dropdown and you can continue your work properly, the page init method called before loading view state. means your dropdown index will be selected after this method and you can access it in dropdown_selectedIndexChanged method.
For more information on ASP.Net page life cycle
Edit 1: have a look on sample code
protected void Page_Init(object sender, EventArgs e)
{
// here you bind your dropdown
// don't check IsPostBack
DropdownList1.DataSource = db.GetCategories();
DropdownList1.DataTextField = "TextField";
DropdownList1.DataValueField = "ValueField";
DropdownList1.DataBind();
}
ASP.Net loads controls viewstate after Page_Init and before Page_Load event, so DropDownList1's selectedIndex will not be affected, and you got desired results.
just check if the page-call is a postback with IsPostBack and then call dataBind() on your dropdownlist
Of course you can very well change the data source after the post-back. You can even refresh the data in it by calling DataBind() method on the DropDownListbox.
Another way of doing this if you can't bind in Page_Init for some reason (as in my situation):
var selected = ddList.SelectedValue;
ddList.DataSource = DBHelper.GetCategories();
ddList.DataBind();
ddList.SelectedValue = selected;
You'll still need to handle situation when selected category was deleted.

Categories