When a postback occurs on my page, changes are made in my database and I want those changes to be reflected in my ListBox. It has a datasource that is set up in the .aspx side of things (DataSource = "myDataSource"). Right now, on a postback, these changes are not visible. How can I "Rebind" my ListBox?
try this :
In your event handler that is causing the postback try adding this at the end (say Button1_Click if it a postback caused by a button)
ListBox1.DataBind();
it should cause the datasource to refresh.
Related
I have a dropdownlist control and a text box control in asp.net. dropdownlist is getting data from database. suppose some data are x-ray,USG,MRI and their value are 100,200,300. So if i select x-ray from the dropdown i want it's value 100 binds at the textbox without page refresh. How can i do this??? Please Help.
Using .net, to have something happen on the server side when something is selected from DropDownList, having property AutoPostBack=true is required. This means when you change the SelectedIndex, the page refreshes (postback), then the SelectedIndexChanged event is fired.
Theres a few approaches. In your page_load event, put everything you want to happen on the first page load in If Not IsPostBack Then ... End If block. Then when a PostBack happens, like DropDownList_SelectedIndexChanged, the code in If Not IsPostBack Then won't happen, because it IS a postback.
The above approach still has a page reload, but you can choose what happens when its this type of PostBack page reload. You may have javascript thats re-firing, though...
The second approach would be to have no postback event happen from the dropdownlist, and handle the Binding of the selected item in DropDownList to the TextBox with javascript.
I have two drop down lists (ddl1 and ddl2) and a button.
I select a value from ddl1 which fires the ddl1_selectedIndexChanged event.
When that happens, the value selected by ddl1 goes to a database in a SELECT statement, in which a WHERE Clause is used to fetch rows of required data.
This new data table now populates ddl2.
After selecting a value from dd12,
I press a button.
When the button is pressed, I want to use the ddl2_selected_value in the button_click event.
The problem is when I press the button, ddl1_selectedIndexChanged event fires unexpectedly and binds the ddl2 again, losing the ddl2_selectedValue.
ddl1 - EnableViewState True, PostBack = True
ddl2 - EnableViewState True, PostBack = False
Do you have anything on Page Load that is populating the ddl1.
In that case you need to check for PostBack in the page load.
if(!Page.IsPostBack)
{
//Call method to populate ddl1.
//ddl1 will load only once.
}
I resolved the issue by going through a couple of more blogs over net.
What I was doing was I wrote these entire set of controls in asp Panel.
The viewState property of this panel was set to False.
As soon as I changed it to true, the unwanted selectedindexchanged event got suppressed.
This got me through. :)
But, it was a hunch.
I did not yet got the reason why this happened ?
Explanations are most welcome.
#Arun: Thanks fot the concern.
I have the control dropdownlist which has been loaded inside the template column of RadGrid.
While loading I have set AutoPostBack='True' for dropdownlist and also created the event SelectedIndexChanged.
DropDownList ddlConditions = new DropDownList();
ddlConditions.ID = "ddl" + name;
ddlConditions.AutoPostBack = true;
ddlConditions.SelectedIndexChanged += new EventHandler(ddlConditions_SelectedIndexChanged);
My question is while i change the selected index of dropdownlist the event SelectedIndexChanged is not getting triggered.
Can anyone help me to solve this problem?
Thanks in advance.
Usually this caused by a page life cycle problem. When your index changed event of Dropdownlist fires the control doesn't exist to bind it on the postback.
Example:
-> MyEvent fires.
-> Drop-down created.
-> Event Handler specified.
-> Index Changed event triggered. Page reloads. Drop-down not found, cannot fire.
So you have to ensure that the drop-down is created before .NET attempts to handle the event.
Please refer this answer for more information regarding this type of issue and life cycle.
I can suggest you to check the place where you have created DropDownList. Dynamic controls should be added on OnInit or at least on OnLoad. After OnLoad finishes executing ASP.NET starts processing control's events and values.
My question is while i change the selected index of dropdownlist the
event SelectedIndexChanged is not getting triggered.
Answer: because you have created DropDownList after the events have been processed.
Is the page posting back? If so, you'll need to make sure that the control is recreated on the page on every postback.
If it's inside the usual if(!IsPostBack) block, then put it outside - Usually, it's prudent to create controls in page_init as well, but that can depend on your specific setup.
Hi I'm having a really strange issue here. I have an ImageButton and Gridview, both of which I create on runtime. The Image button is a seperate control on the webform and is not linked in anyway to the Gridview. What I am trying to achieve is when the user completes the editing of a line in the GridView and clicks on the update row button, I perform a set of calculations on the input entered. If an error is found with the data entered, I then make visible the ImageButton which when clicked displays the error message. The function I am using to make visible or hide the Image button through
picCross.Style.Value = "display: none;"
and
picCross.Style.Value = "display: block;"
gets called on the Page.PreRender function. What I have discovered is that I am able to make changes to the ImageButton (changing the button's tooltip, setting its Style's value ) during a postback not fired by the GridView, such as when the user clicks on a button else where on the web form. If however I try making changes to the Imagebutton during the post back event of the GridView, such as when the user clicks on the edit row or update row button/link. The changes are not saved. What is even more strange is I do not have such an issue with labels. I can change the text of labels regardless on whether the change was made during the postback fired off by the Gridview control.
I have tried setting the ImageButton's EnableViewState to true and false but neither makes a difference. I have tried the same approach with panels and the same thing occurs, I can't change its properties during a postback caused by the gridview. My GridView has EnableViewState = True
P.S I'm binding the GridView on runtime too. Only my GridView's RowUpdating event fires. The RowUpdated event does not fire. I was hoping to try changing the ImageButton on RowUpdated, not that I think it would have made much of a difference anyway.
You should create the dynamic controls (Gridview and ImageButtons) in the PreInit event. Init event if you have a Master Page.
I forgot to mention this asp.net 2.0.
The user control has a unique id and it is loaded in the PageLoad event.
The user control is loaded into a panel and the panel is inside of a webpart.
The dropdown has autopostback set to true.
EnableViewState = true on the dropdown.
The ListItems are created in the dropdowns pre-render event method.
This is why I don't understand why it is not firing, the dropdown is the only thing that causes postback on this user control.
The event methods for the dropdown should occur since the user control is loaded in the page load method on postback again right?
Make sure there is no OnLoad or PageLoad event that is rebinding the datasource of the dropdown list. Rebinding the data with a new set of data may cause the clickhandler to not ever get executed.
make sure you have if (!Page.IsPostBack) around dropdownlist.datasource = and dropdownlist.databind()
I am not sure if this is your problem, but it is the most common.
Try with EnableViewState set to
true for the DropDownList
If the ViewState is set to false, on post back the selected Index gets back to default which is normally the first Item. First item, if selected, does not cause SelectedIndexChange event to fire