How does asp.net parse the GridView? Suppose I have defined both EditItemTemplate and ItemTemplate. It seems that I can’t do any data bound at the loading time to the controls like dropdownlist in the EditItemTemplate.
And is it true that the data will be bound to the controls in the EditItemTemplate when the Edit mode is activated? If not, how to bind all controls at the loading time in C#?
Thanks, this is actually driving me crazy. I can’t find anything about how asp.net execute or evaluate the GridView online.
You need to bind at when it is put into edit mode. Remember the edit controls do not exist until the it is set to edit mode for a specific row. At that time the controls are available to be bound.
Normally if you have common lookup type data you want to bind at edit mode, I load it into
Cache,SessionorViewState(depending on content and situation) the first time it is needed and then bind it from this ‘cached’ location to save DB calls. I normally implement theOnDataBindingmethod for each control within theEditItemTemplatethat need special binding like aDropDownList.In your aspx:
Then in your codebehind:
I prefer to bind this way as it will localizes the code to specific controls and your don’t have to go looking for them using
FindControlon rows etc…The
yourDropDownList_DataBindingwill only fire for the row in edit mode. On your initial bind where nothing is in edit mode, the databinding will not fire but each time you put a row into edit mode it will execute, that is why I say to somehow cache the data you want to bind to aDropDownListthe first time you get it.