When using DropdownLists and Listboxes, or any control which uses ‘selecteditem.text’ to get the selected item, the major reason for not getting the correct item is as follows. If you are binding the control at Page_load, you MUST surround the binding code with a page.ispostback/if/then statement inside the Page Load event.
Something like this (VB.Net):?
if not Page.IsPostBack then
‘ Do your stuff here
end if
or in C#:
if(!IsPostBack)
{
// Do your stuff here
}
Inside Page_load, you need to make sure that, when it posts back, the actual item selected is maintained…. The reasoning is as follows: If you have as specific item being selected during page_load – it will always be selected upon every subsequent page_load – the page_load sub is exactly that – it tells the page what to do when the page is loading. If you post back, based on a selection, you naturally, would not want this to happen – you only want it to happen the first time the page loads – not on a postback… SO — to get around this, you need to qualify whether or not the page is loading for the first time, or it’s posting back.
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF
Leave a reply
You must be logged in to post a comment.