First of all, sorry if I make mistakes in english…
I’m making a web with c#, and I have some problems for refresh the data displayed in the GridView
I’m getting the data throw the SqlDataSource defined at the aspx view:
<asp:SqlDataSource ID="PRODUCTOS_CON_STOCK" runat="server" ConnectionString="<%$ ConnectionStrings:XXXX %>" ProviderName="<%$ ConnectionStrings:XXX.ProviderName %>" DataSourceMode="DataSet" SelectCommand=" select EAN, CODART..... "> </asp:SqlDataSource>
When I click a button, I update some data in the database, and I want refresh de GridView with the new data, without reload the page.
I’m making: gridView.DataBind();
, but that doesn’t refresh the data in the GridView.
(At the database is updated)
The GridView
is inside of an UpdatePanel
.
I was trying some things, like:
-
Reassing the
DataSourceID
and make thegridView.DataBind();
-
Assing the
DataSourceID
innull
, make thegridView.DataBind();
, and alter reassing theDataSourceID
and make thegridView.DataBind();
-
I tried too:
DataSourceSelectArguments argumentos = new DataSourceSelectArguments();
PRODUCTOS_CON_STOCK.Select(argumentos);
gridView.DataBind();
- Set the
UpdatePanel
toupdatemode="Always"
But any of all of that worked…
Someone can help me?
Thanks.
2
Answers
Finally I resolved the issue.
In my case, I was calling in the front, in
AJAX
, the C# method, that method is anWebMethod
HttpPost
The issue was that I was saving in a
Session
theGridView
and theSqlDataSource
, and although I was executing the Select statement again, I was obtaining the old's values.I was saving in a
Session
that information because theAJAX
execute the method direct and theGridView
and theSqlDataSource
werenull
Finally I tried to make a refresh from the
GridVeiw
by a button, and the info is updated correctly, so, I hided the button, and simulate theclick
button inAJAX
:So, the click event call to
LoadGrid();
, and this one execute:In this way, I can refresh the data "automatically" without clicking in any button
Why not dump the datasource setting on the page of PRODUCTOS_CON_STOCK.
I find that when you need to filtere, load, and play?
Just remove the data source from the markup. So, in your GridView, remove the data source id setting of PRODUCTOS_CON_STOCK, and then delete the data source.
You have to write a BIT more code, but you now have control over the data, and MORE important, control over which parameters you need and want.
So, say I have this markup:
A grid, and also a search/text box to filter the grid.
Note close – I did build this grid using the wizards. But I THEN removed the Datasource ID for the GV, and removed the Datasource that was created in the markup.
So, now my code to load is this:
And I get this:
And note the "optional" filter for the text box. If you type in some text, (I used a "like" match in sql), then the code is only this:
but, the idea here is often it is much better to dump the SqlDataSoruce placed in the markup, and roll + write your own code. The problem is you can try and set the data source in code, but ALSO WITH a data source on the page – they fight over each other. So, try the above idea – and remove the data source in the markup.