I have a Listbox getting populated when the page loads itself and also in postbacks. Some items are already selected in that listbox even when the page loads or postbacks. I have to select one or more items in that textbox in addition to the selected one.
Till this point, it works perfectly, but the problem is that when i click an "update" button to save the newly added items, then also the page postbacks and so the list box get populated again. This will loose the newly selected items and none of the newly selected items will be saved.
So what should i do in order not to loose the newly selected items when i click the "update" button.
Note:- I need to populate the listbox on postbacks also. So the population of that listbox on postbacks cannot be discarded.
Kindly help me. I am new to ASP.net.
Thanks in Advance
You shouldn't need to re-populate the list if you have your view-state enabled.
To populate you should be doing something similar to this:
if(!IsPostBack){
// populate your list from database or whatever
}
This will only populate the list if the page is not a post-back, therefore (if viewstate is enabled) your selections will remain after you click update.
If you want to disable the viewstate, it is important to populate the DropDownList during the "Init" event of the Page LifeCycle not the "Load" event:
protected void Page_Init( object sender, EventArgs e ) {
DropDownList1.DataSource = ...;
DropDownList1.DataBind();
}
More information can you find in this excellent article:
Truly understanding viewstate.
I hope this helps a bit.
Related
this is my application link please download
i had 2 different user controls in one .aspx page.
first user control was dropdownlist binded with database.
second user control was gridview.
if i select a company int the dropdownlist products of that company should be displayed in the second user control.
my problem was, when i am selecting the companies in the dropdownlist gridview user control is loading first and 2nd dropdownlist selected event is firing..
please help me...
You should probably check the IsPostBack property and only populate the controls if it isn't a postback.
void Page_Load()
{
if (!IsPostBack)
{
// Populate the user controls.
}
}
But since you didn't show any code I'm just guessing.
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.
I have an Asp.net gridview on a page, when a buttonfield is clicked (instead of select) it opens a popup form (my popup not asp's) which contains the detail of the gridview row displayed in an editable DetailsView. (This approach has been used because the grid contains 20 wideish columns and it is easier to edit / update in the detailsView format) The DetailsView takes any amendmends and writes them back to the table, fine, but the underlying Gridview is never visually updated unless of course the page is completely reloaded (I tried to use the Windows.reload function but I get the annoying winddows is trying to reopen etc, error, so that is useless). I am trying to discover the best way to get the gridview to refresh it's data. I have placed via a Me.ClientScript.RegisterStartupScript(Me.GetType() eyc, an alert box to communicate when the 'update steps' of the detailsview fires so I could interject a gridview.ReBind() but because the detailsview is held within the update panel the, for example, Protected Sub DetailsView2_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView2.ItemUpdated functions, and the roqwupdatind and so one, do not appear to fire. (they must do of course else the table would not be updating. MY problem is how do I 'communicate with update events within the panel contained detailsviews so that I can cause a reBind to occur on the main gridview. Any Ideas would be appreciated. Thank you
Detail view have an event called
protected void dvEbook_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
Response.Redirect(Request.RawUrl,false);
}
where dvEbook is my detail view.
It will call a same page.
I hope it will work for you.
Thanks
I'm Using a GridView in my Application.
My GridView has a checkbox column and when the checkbox is clicked, the page is redirected to an other form.
When I return to previous from, nothing comes and GridView data is lost.
I used session and it's working fine, but I want that checkbox which was clicked to also maintain there state.
Is there better method to do this?
Upon redirection to the next page, Iterate your Gridview to update your Datatable with a checked checkbox column and then set that datatable to a session variable. I am assuming that you are already storing your source Datatable to a session.
Sorry, but no other magic work around !!!
Hii,
I need to develop an online quiz website that would be having MCQs. I would want to have one question appearing per page with a Numeric Pager so that the user can go back and forth.
I tried using the FormView for displaying the questions and RadioButtons. I created a class QANS that would hold the answer selected by the user for the questions that he did answer so that I can later sum up the total Score.
Now, the problem I'm facing is as below:
Let the user select a RadioButton, say R, on a PageIndex, say I, and then go to some other page, say K. When the user returns back to page I, none of the RadioButtons is selected.
I tried adding code for setting the Checked property of the RadioButton true in the Page_Load(), but it didn't help. I also tried the same with the PageIndexChanged and PageIndexChanging event, but the RadioButton doesn't get checked.
The RadioButton that was selected for a particular question has been stored and I am able to print which one it was using Response.Write() but I'm not able to keep the RadioButton Checked.
How shall this be done?
You should be able to make the radio button checked in the
protected override void OnPreRender(EventArgs e)
event of the page lifecycle
Have you considered using the .Net wizard control for this?
The ASP.Net 2.0 Wizard Control
You can save the state of the selected controls in session and repopulate them after the round trip.
It's odd that it's losing state after the postback. Is there any crazy code in you on_load or pageIndexChanging or anything?
From what I understand, you have to persist the checked state of the radiobutton across the pageindexes. You can use ViewState for that as that can preserve data across postbacks for a page.
something like this:-
// Define a list of question ids attempted
List questionIdsAttempted;
//Add the List to the ViewState
if(ViewState["QuestionIdsAttempted"]!=null)
{
questionIdsAttempted= new List()
ViewState["QuestionIdsAttempted"] = questionIdsAttempted
}
// Add the question id to the list when when the user attempts a question
questionIdsAttempted.Add(qId);
// Add the list back to the viewstate
ViewState["QuestionIdsAttempted"] = questionIdsAttempted
And in the page load in checked attribute of the radiobuttons , you can call a function with the current question id and the function will get the list from the viewstate and returns true or false based on if the id exists in the list.
Ofcourse, you need to check the nullability of the viewstate in various places based on the flow of the program.
Let me know