I asked a question Gridview is not displayed when using stored procedure in SQLDatasource which I want to resolve . Based on my old question, I am not sure when exactly is gridView update fired. I am under the impression
Gridview Update is fired automatically when the page is loaded if you use SQL Statement in SQLDatasource
GridView needs to be manually bound using sqldatasource.databind() function to update data if you use stored procedure.
But even then, the second case is not working for me. I know this implementation is somewhat buggy.Can anyone guide me throw this, when exactly Gridview is updated and possibly throw some light on my original question also?
Thanks!
Update: After further research, I found that databind() property is not needed to update the gridView. What is needed is a post-back. So if you add a button with no code inside it, it will update the gridView simply because the page has been reposted and (maybe)the textfield values have changed too.
I still dont get any result though. What I have done is entered "not data found" in empty template for the gridview. So now I get "Data not found" instead of nothing. Which does means that SP is run but returns 0 result.
Another thing that I found is, if you bound a text field to the SQLDataSource, by default, it changes the empty textbox value to NULL. This is kind of stupid, why will you need the NULL values for? This will result in 0 records since obviosly we dont store null int the database. (Null means nothing but Microsoft though the other way). So to fix that, go to the parameters list in SQL Datasource and select the parameter. Click on the link "Show advance properties" There change the property "ConvertEmptyStringToNull" from true to false. Although it did not fixed my problem yet (but fixed it somewhere else).
I am thinking I am using 4 parameters in sqlprocedure and one of them is causing problem? But the query does not ok when testing in SQLDatasource.
Is there a reason why you must use the GridView update event? Why not just add the code to bind the GridView during the Page_Load event or something? Have you at least tried to narrow down the causes for the erroneous behavior?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataSource1.DataBind();
}
}
After my research, I found GridView is automatically updated when the page loads. It is the same weather an SQL Statement or a Stored Procedure is used in SQLDataSource. Also SDQDatasource.Databind() is not needed, but may be needed in special scenarios.
What triggers GridView update is page load. If you create a button and add a click event, just clicking that button will automatically cause gridview to update but clickevent cause page reload.
Hope it helps
Related
if you have not encountered this problem and I have to upload my code in order to explain it for you or for you to be able to debug it, then this question is not for you.
I know I can solve this problem by using code-behind, but I don't want to do it. Eventually I will have to do it if nothing works.
I am not using any databound control (gridview,Formview,Detailview etc). Everything is a general form control: textbox and ListBox. I am using their Text and SelectedValue properties to supply values to Updatequery's ControlParameters. Everything should be working as expected. I have played with the ViewState property of the texbox control and the sqldatasource control itself, to no avail. The stored procedure used for the update command is logging the values supplied from the ASP.Net side and amazingly it shows the old values of textbox that were there when the form loads and not the changes I make.
Whats going on here?
Thanks!
Seems like you are missing something in the asp.net life cycle.
Remove any DataBind calls to your sqldatasource on page Load.
If you are using DataBind on Load to populate those text and select controls, a databind will just overwrite any values you entered.
Don't be so shy to show your code, it helps a lot in providing good answers.
So I have an ASP.NET page with two controls:
a GridView which displays rows from a SqlDataSource, and in which the user can select a row;
a DetailsView in which the user can see and edit the values of the selected row.
The DetailsView can update the underlying data, but even though the page reloads, the GridView still displays the old data until I manually reload the page in the browser.
How do I ensure that the GridView displays the correct data after the update in the DetailsView?
P.S. It may be important to note that due to reasons outlined in this other question, I had to use a custom OnItemUpdating event with the DetailsView. This event performs the SQL update command (successfully), sets the DetailsView back to ReadOnly mode (out of Edit mode) and then cancels the event (e.Cancel = true). If this event canceling also somehow cancels the GridView updating, how do I manually tell it to update itself?
P.P.S.: I discovered this similar question, but the answer doesn’t work: it resets the entire page back to pristine state, which means the GridView loses its selected row and the DetailsView disappears. I don’t want that.
on page load:
YourGridViewID.DataBind()
If your OnItemUpdating event generates postback
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
GridView.DataBind();//rebinding it with considering changes DetailView
}
}
If it doesn't work let me know.
1) If we first set DropDownList.SelectedIndex to some value and then rebind control to data source, it’s SelectedIndex property will be reset to default. So why doesn’t something similar happen with GridView.SelectedIndex and GridView.EditIndex? It seems that unlike DropDownList, GridView doesn’t reset these two properties ( to their default values ) after a re-bind.
2)
If you set the GridView.EditIndex property after a postback or in handlers for events that are raised later than the Load event, the GridView control might not enter edit mode for the specified row. If you read the value of this property in other event handlers, the index is not guaranteed to reflect the row that is being edited.
Why could setting GridView.EditIndex after postback or in handlers raised after Load event cause GridView not to enter into Edit mode?
thank you
In order for a GridView row to be in edit mode, the grid must be databound. So resetting EditIndex wouldn't make much sense. As for SelectedIndex... I imagine that is just simply a design decision.
As for number two... I think this text is misleading. Does it come from MSDN? I think what it is trying to say is "don't do this or allow it to happen:"
some EventHandler {
grid.EditIndex = X;
grid.DataBind();
grid.EditIndex = Y;
}
You see? What row are we editing now?
[Edit]
This has everything to do with Question 2. You see, with the above code we are actually still editing row X. (but most likely, you will get a state error when you go to postback). Setting EditIndex itself does not put the GridView into edit mode... so your question is somewhat moot to begin with. What actually puts a row in edit mode is having this property set when the grid is databound. EditIndex really just tells the Render method which template to use: ItemTemplate or EditTemplate.
Like I said before, I believe the text you quoted is misleading. The important thing is knowing when the data binding happens. The text you quoted seems to assume that that is happening during Load.
If I bind GridView (via DataSourceID attribute) to SqlDataSource and set SelectCommand and UpdateCommand attributes, then everything works perfectly.
But if we manually call GridView.DataBind inside Page_Load(), then SqlDataSource doesn’t perform any updates, even though SqlDataSource.Updating and SqlDataSource.Updated events do fire when GridView’s Update button is clicked. I think this is due to the fact that GridView resets to what it was before the user edits:
a) Why does GridView reset its values if we manually call DataBind() inside Page_Load()?
b) Since Update operation doesn’t work when manually calling DataBind, I would then assume that Delete operation also wouldn’t work, but it does. Why?
cheers
I believe Page_Load runs before your changes take place, thus you bind the old data before you run the updates
Wrap the bind in a If Not IsPostBack when under Page_Load, i believe that will fix your problem.
You will need to assign the GridView with source and also databind it. Something along the lines of the following code:
Page_Load
if(!Page.IsPostBack)
{
gv1.DataSource = GetData();
gv1.DataBind();
}
Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want