Timing a gridView Update in C# - c#

So I'm currently working with a C# ASP page in which I have a DropDownList and a GridView. I'm initializing both the GridView and the DropDownList (Along with their connected data sources) in the PageLoad event.
I've got the DropDownList set to AutoPostback=true. I'm changing the select statement for the GridView in the DropDownList_SelectedIndexChanged event. The end result is that the page loads again and then the select statement is changed, by which point the GridView has already loaded again. This basically means that it the GridView changes take two page refreshes to update.
Is there a way to avoid having to refresh the page twice? I tried simply updating the DataSource and the GridView in the Page_LoadComplete function instead but by that point it was too late to update the page this time around, meaning it still required another refresh.

So you may try this in your page load....
if(!IsPostBack)
{
// only then bind your grid View...
}
and in your DDL's selectedindex changed event bind your grid to what ever select result set you may want to bind to...
what this will do is, your grid view bind code will get executed only for the first time in your page load event...and on any subsequent postbacks you may cause with your dropdown...you can bind your gridview in the selected index changed event...this will avoid binding your gridview twice...

Related

ASP GridView and DropDownList

I have an asp page with a GridView control. One of the columns in the grid view is a dropdownlist. the drop down list are filled using the RowDataBound event on the page load event. However, one of the options in the list, when selected, will popup a dialog that will allow the user to add a new item to the list, and that newly added item becomes the selected item for that drop down. This part I have working fine, but I want to be able to add the new item to the drop down list for every row in the grid. How do I, in a way, re bind the drop down list in all rows after the page has loaded without having to loop through each field?
If all the DDL's have a shared DataSource then it should just be a matter of forcing a postback after adding the new listitem. But how are you adding the new listitem?
If your popup is simply adding a new html <option> tag to an html <select> tag, this is not the same thing as updating the DDL DataSource.
For all the DDL's to show the new listitem your popup would have to perform a postback that updates the DDL datasource(s) and all the DDL controls in the GridView would need to be rebound.
This would happen automatically on a postback assuming that the DDL controls are connected to their datasource via the DataSourceID property. If you are making use of the DataSource property then you will have to call DataBind() explicitly for each DDL control, which I think you said you are doing in the GridView RowDataBound event.
Alternatively you could loop through all your ddl controls clientside and add the same item to each control, but you would lose all those changes the next time the page refreshed and I don't think that's what you want.

GridView RowCount late update updatepanel

I have a gridview in an updatepanel where also my dropdownlist is. From the Trigger of the dropdownlist I am refreshing my gridview with the slected value. All that is working fine. The problem is that I am also displaying the gridview row count on the page which is also inside the updatepanel. The update seems to be happening to it, one selection too late.
protected void Drop_Change(object sender, EventArgs e)
{
String Value = AjaxDrop.SelectedValue;
GridView1.SelectParameters["Target"].DefaultValue=Value;
RowCount.InnerText = GridView1.Rows.Count.ToString();
}
I think its happening one selection behind because the parameter updates the gridview rowcount too late for the RowCount value to have it, what is a work around to get the actual value after parameter passed. Only way I can think of is using javascript and I wonder if that would even work. My desired solution would be to keep it all on server side though.
At the time you're calling this, the GridView hasn't done it's databinding, therefore the value is not valid. Move your RowCount.InnerText updating to somewhere that gets executed after the DataBinding event (e.g. Page_OnPreRender), or force the DataBinding to occur before you update the row count.
It's worth noting that GridView.Rows.Count isn't a reliable source of information if you are using GridView paging - as this will be the number of rows on one page, even if there are more rows returned by the query.

Dynamic gridview based on dropdown

I've got a bit of an chicken and an egg.
I need to bind a gridview to a set of database values. The gridview is dynamic and the columns, cells are created at run time.
As such, I need to re-bind the grid on every postback in the pre-init, init events after very post back.
However, the data used to populate the grid uses a value from a dropdown box on the same page. As such, the value of dropdown is not accessible through viewstate until after the init event (i.e. the selected index is always the first item in the list until after on init).
How can I get access to the value of the drop down in time to rebind the grid before the pageload event?
If the Gridview always has the same number of columns, you could have the dynamic query generate column names as aliases, and then just rebind the Gridview on dropdown change?
you can use and HtmlHidden control for store the value you need. It doesn't depend by the viewstate and it's available into the page init

Formview paging controls not showing

I have a formview which displays a recordset based on a parameter supplied by a dropdownlist. The formview has "AllowPaging = True".
The first time the dropdownlist is changed, the formview shows the (correct) first row of the retrieved recordset but the paging controls are missing. From that point on, when the dropdownlist is changed, the formview works as expected, showing the correct page controls.
What do I need to do to have the formview show the page controls the first time it is activated?
These kind of errors are sometimes due to a missing explicit binding call such as:
formview.DataBind();
Try this after the first change of the dropdownlist.

Gridview inside UpdatePanel refresh

I use a GridView to represent data from a table in my DB.
the GridView has some template fields whose content are determined before displaying the Grid ( I use the RowDataBound event to determine content of template fields before displaying the GridView).
The page displays a list of records from the table records and then, the recording process starts. after the process is over, the template fields should be updated.
how do I automatically refresh the GridView after the process is finished ? it should be noted that the GridView is contained within an control and that I continuously poll the server using a Timer control that executes "GridView1.DataBind()" at the server level every 60 seconds.
since the GridView is inside an UpdatePanel, calling DataBind() method on it doesn't seem to call the RowDataBound event.
How can I solve this ?
Yes, thats enough to call the GridView1.DataBind() Method in a particular time interval lapses using the Timer control, but before calling the DataBind() method, have you assigned the updated data source for the Gridview ? For eg; When the page loads you are drawing the gridview using a Dataset named "EmployeeDS". so after some update, you need to update the local dataset like below to the gridview, then call the DataBind() method.
GridView1.DataSource = EmployeeDS.Table[0];
GridView1.DataBind();

Categories