GridView.EditIndex property - asp.net

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.

Related

DropdownList and Textbox not callling SelectedIndex/Text Changed on first postback in IE and FF

I know this is a problem that's been asked a few times before, but having had a look around I can't seem to find an answer that works for me.
I have a VB.NET page with 2 textboxes (set to show dates using JQuery), a dropdownlist and a datagrid.
Both textboxes and the dropdownlist are set to fire events, if they're changed, to update the datagrid.
Generally this works fine with one exception; when I change the value of either the textboxes or the dropdownlist after the initial page loading it often doesn't fire the event, the postback is set to false and the dropdownlist value is cleared; this doesn't happen in Chrome and is less frequent in Firefox than in IE.
I've tried the following:
I've set autopostback=true on all items
Tried adding an extra item, which isn't enabled, to the dropdownlist
Moved the boxes/List outside the update panel
Set ViewStateMode=Enabled
Tried adding a hidden field which is set to Request.Form(ddlDropdownList.UniqueNumber) and comparing it to Request.Form(ddlDropdownList.UniqueNumber) in the page load as a way to fire the event (the suggestion says to use Request.Forms, but this isn't coming up as an option on intellisense?)
All of these solutions either don't work, cause other issues, or I'm not doing them right (Which could certainly be the case)
Thanks in advance for any help you can give.
I don't know if this will help, it's been a while since I've done web forms, but I do recall there being a problem with listboxes where I had to check if the page was not on a postback when populating it.
So basically in your page loading method, check to see if the page is not a postback before initalising it. I found that even though I selected a new item, it was resetting the list back to it's default state.
This may or may not help you, but I thought it was worth at least mentioning it.

when gridView update is fired?

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

DropDownList always does full-page postback, the first time it fires

I have an ASP.NET page using the AJAX library. There is a dropdownlist inside an UpdatePanel, which on index changing is supposed to update a different UpdatePanel to modify a grid control.
But after the page first comes up, and you adjust the ddl, the entire page postbacks, followed by a (correct) partial postback. After this one time, every other usage of the ddl performs correctly, trigger partial postbacks.
Both the panel and the ddl are being added from code. The UP is Conditional updates and ChildrenAsTriggers = true. The dropdownlist is AutoPostBack true and has an event set up on SelectedIndexChanged.
So what is going on? I've tried adjusting every setting I can think of and still the page completely refreshes once, then works fine after that.
I believe I've solved my current woes in regards to this problem, though I'm left feeling a bit dumb by the outcome. When programmatically adding the dropdownlist, I didn't give it an ID, assuming ASP.NET would just assign it some sequential name-container mangled ID anyway. It seems this was the cause. I'm assuming that the initial postback was due to ASP.NET trying to deal with the lack of a proper ID, assigning one, and then using that afterwards for the partial postbacks.
Or maybe it's something totally different at root, but the simple fix was: add an ID to the control.

ASP.NET GridView problem

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

When does a DropDownList retain the value from postback at the SelectedIndexChanged Event Handler

To Clarify to all this problem absolutely does not stem from rebinding of the controls and the value does not remain the initial value after binding.
I have a DropDownList on an aspx page which is being used in multiple projects.
Along the life cycle of the page the SelectedValue is changed prior to the handling of the SelectedIndexChanged event.
In one project when the code reaches the event handler the SelectedValue is back to what was posted from the client, while in the other the new SelectedValue is present.
Viewstate is on in both cases, the control is not being rebound and follows the exact same flow from all that I can gather.
The control is not being initialized again, I checked this thoroughly and does not retain the initial value but rather the value set in the code.
I actually need the posted value at the point of the event handler like is happening in the first project but do not understand why it would be changing back to the posted value and how to replicate this behavior in the second project.
I will be happy to clarify further if any of this is unclear.
Maybe some initialization is performed twice in the on_load event because you dont check the page is posted back using IsPostBack in a test ?
It exactly happens to one of my colleague ten minutes ago :-)
Many people wrongly believe that DropDownLists must have ViewState on to be able retain selected value. I almost never have EnableViewState set to true on DropDownLists because they work fine in a form with the posted values. (And setting it to true on a DataBinding control will cause a long viewstate)
The posted value (selected item in the dropdown) is loaded from the post-parameters after OnInit in the page so if you bind the data to the dropdown in OnInit it will work fine.
If you bind in OnLoad, the selected Value will be overridden.
Perhaps you are databinding in the wrong event so that the selected value gets overridden by a DataBind-call on the dropdown?
I usually see this behavior when I've forgotten to check IsPostback somewhere. The page will load all of the original data before going to the SelectedIndexChanged event handler.

Resources