I search for results, click one, it opens in a new window, then edit it, and close the popup.
Then I hit 'search' to refresh my gridview, but the changes do not reflect unless I hit F5. It's caching it and I need to stop it, but I don't know how. IdeaS?
Are you rebinding the grid when the user clicks 'search'? You need to be sure that somewhere in the 'search' method control flow, you're doing this:
dataGrid.DataSource = updatedDataSource;
dataGrid.DataBind();
I'm guessing you're binding your results to the grid during PageLoad. Because the event handler for your search button gets fired after PageLoad, the new search results don't get bound to the grid until the next page request. If this is the problem you'll need to rebind the grid when handling the search button event.
Related
I have a page that if IsPostBack is true, calls a javascript function which gets the size of the screen, passes the width and height to hidden fields and clicks a button to cause a PostBack. So I can retrieve the size of the user's screen and then load the page with default data with tables the right size etc.
Invariably, when the page (having posted back once) displays the data, a gridview is populated. Each row has a 'delete' Link Button in it. Each Link Button is set OnClientClick to call a javascript function to confirm you want to delete.
Above the grid is a row of text boxes / buttons / dropdownlists which allow you to search for, or filter the data showing. The Gridview is in an update panel. When the page first loads its data, and shows the list of projects, the delete Link Buttons all work okay. In every row, no problem. The Confirm box is displayed and, if Okay is selected, the asynchronous postback occurs.
If, after the initial data is displayed (which is already after one postback) you then use the search box and button, or use a dropdownlist to filter the data (the Search button and filter DropDownList are async triggers for the UpdatePanel) the data refreshes okay (always) but, then, sometimes, the 'delete' LinkButton in each row of the GridView does nothing. It doesn't even fire the ClientSide function. It's as if it is dead.
I had a similar problem a while ago with ImageButtons and the received wisdom seems to be 'change them to LinkButtons'. But I have dozens of grids with 'delete' LinkButtons that work okay, but this one has decided to stop working - sometimes.
I have to say, since moving to Framework 4.0, I'm thinking of giving up on UpdatePanels. Seem to have nothing but problems with sites that worked reliably for years in Framework 2.0
Any ideas please? Is this anything to do with the order in which controls are loaded on the second postbacks?
Edit: There is a twist to this. When this situation occurs, i.e. clicking on the Delete link does not even trigger the Client Side event - if you click on an area of the page outside the Gridview (i.e. outside the UpdatePanel) - and then click back on the Delete link - it works!
Regarding ImageButtons: There is a bug with IE10: ASP.NET fails to detect IE10 causing _doPostBack is undefined JavaScript error or maintain FF5 scrollbar position ( http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx ). Just upgrade to .NET 4.5.
Regarding LinkButtons: Do you have "ID" property? I remember I had a similar issue because my linkButton didn't have an ID.
Could you post some code? It would help.
I have the following scenario:
UserControlA contains a <asp:Button id="bSomeid" onClick="AddItem" /> with some code to an item to a shopping basket in AddItem.
UserControlB contains some LinkButton's that dynamically add a selection of UserControlA to the page in the OnClick event.
This is all done in an UpdatePanel. It is a little more complicated but I have pruned the information to what I believe is causing the problem, I will add more information if necessary.
The problem I have is that it takes 2 clicks for the AddItem event to trigger after I have added the items to the page after clicking the LinkButton.
I understand why this is happening - it is to late in the page cycle to register events for the next post back in the onclick - but can anyone think of a way around this? Can I force an event to be triggered on the next postback? I have tried to think of a way to run my code in page_load but I requuire access to the sender in the onClick.
Using .NET 4.0.
EDIT
I managed to find a way to get the link button sending the request in the Page_Load (using Request.Form["__EVENTTARGET"];) so I moved my code to the Page_load event. It still requires 2 clicks so I am assuming it isn't something to do with the onClick being registered to late.
Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?
If your suspicion about being late in page life cycle is true then you can try using ScriptManager.RegisterAsyncPostBackControl method to register dynamically added controls in the link button click - considering that your button is within user control, you need to add public method into UserControlA that would actually register the button bSomeid1 and link button click from UserControlB would actually call the A control's method.
EDIT :
Another cause for button click not happening can be that button being dynamic control is not added in the page hierarchy when post-back happens (or it gets added very late in the page life cycle when the post back data is already processed). A really full-proof solution should add dynamic controls back to the page hierarchy in page_load it-self (and strictly maintaining same controls ids within hierarchy). If that's not possible then you can sniff the request (Request.Form) to detect the post-back.
In your case, you should ascertain if the button is indeed causing the post-back on each click. If yes, what is the POST data (Request.Form) for the first request - what is the __EVENTTARGET value on the first click (and post-back)? That should start your trouble-shooting.
On the other hand, a simple work-around could be to use html anchor element (you can still use link button) and have a javascript handler in the click event that would set some hidden variable and then submit the form (you can simulate the click on hidden button to trigger ASP.NET client side submit pipeline) . Now the hidden variable value can be used on the post-back to determine which link button has been clicked.
"Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?"
Does it require two clicks on the control, or does it take accept a single click elsewhere on the screen, and then fire first time with a single click on the control?
I have my own (similar) issue with the Updatepanel where the first (expected) trigger does not fire and it seems that a single click elsewhere, and then the subsequent triggers fires first time (which totals 2 clicks)
[edit] Since you are working on this ATM, it may help me as well. Do you have a textbox with a trigger event on it? I do, and if I leave this blank (so that it does not fire) then there is no need for a second click.
I have a button, which updates a value in the database. This value is used to determine what to draw on the page. Because of the page lifecycle though, the page redraws before the button click method is executed, meaning that any changes are not reflected until the page is reloaded again.
What's the best solution for this?
To clarify:
Page has a piece of text, that says "I like cats" if the database value is 1
button 'I hate cats' is pressed, which sets the database value to 0
the page reloads, but still says "I like cats"
the button click event is handled, and database value becomes 0
If the page is refreshed/reloaded, it now correctly says "I hate cats"
It should update when the button is clicked though.
you can use the page prerender event. this fire after the control event in the page lifecycle.
You can solve your problem by below code, use this end of button_click block
for ASPX:(C#)
Response.Redirect(HttpContext.Current.Request.Path);
Where are you querying the database? One easy option would just be to use Response.Redirect back to the page.
I think you could just get the result from the button method and just update a label/literal.
One possible solution is to execute a javascript function that updates the database (using ajax) when the OnClientClick event for the button is raised. In this case, when the page reaches the Page_Load event you will be able to render the appropriate content, as the postback happens after the script is executed. It should work fine if the database update takes a relatively small amount of time.
One curiosity, are using any anything like Page.IsPostBack { do something }.. If so, could you check if your update UI code is outside this check.
On details view, I would like to hide the new button.
On page load I have successfully done so by the following code:
dtvwMyProfile.Rows[5].Cells[0].Controls[2].Visible = false;
But how do I hide the New button when I hit cancel or update button after I am done editing.
The New button keeps showing up. How do I hide it completely from the screen.
In some event, the visible property keeps changing to true and how do i find out that event?
I want to be able to do it at run time instead of design time.
Dynamically changing the properties of child controls created by the DetailsView is not recommended.
If the button is being created by the DetailsView itself then all you need to do is set AutoGenerateInsertButton to false and you can do that in Page_Load.
I do not recommend randomly selecting a page event and handling it. If you do that then chances are it will just break again when you change something else.
Maybe try to do that in the ModeChanged event handler, which fires after the mode changes... But can you ensure new is always at position 2? You may want to verify the button by its text or command name.
My understanding of the order of page events is this:
Page : Load
Control : DataBind (for a GridView or whatever)
Control : Load
Control : Clicked (for a Button)
Page: PreRender
Control : PreRender
(There are lots of others - but these are the ones I'm interested in)
The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.
This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?
The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.
Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.
The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).