Currently I need to call a method when the user changes the page number of a datapager and moves from one page to another.Can any one one tell me is there any datapager page change event or any work around for this? Thanks in advance.
You might have to subscribe to the page change event of the control the datapager is doing the paging for. An instance would be if you are using a gridview then you can subscribe to the gridview PageIndexChanged event and put your method call there.
Related
Well I've got some textboxes dinamically created in a .cs file and I want to save/store their values when I click on Update button in a detailsview's edit mode, so I've got seted button's onclick event to a function to do this.
The problem is that the postback event occurs before the "OnClick" event, and then the textboxes' values are lost.
How can I save/store their values first?.
Please, help me with this.
Thanks in advance!
You can detect the page's first loading or post back(OnClick, SelectedItemIndexChanged like..) with the Page.IsPostBack property.
You should check this property value in the Page_Laoad event handler.
It helps to understand the ASP.NET page life cycle.(See here for more info.)
Basic events are as follows:
PreInit
Init
PreLoad
Load (controls are filled with postback values here)
Control events (OnClick happens here)
PreRender
Render
Unload
Usually during the "Init" event you want to create all of the controls, including any dynamic controls for the user.
During the Load event asp.net copies all of the post date back from the client to the controls in the page. But in order for this to work on the postback the same controls that were created on the initial render must exist in the same order with the same IDs.
Then in PreRender you can change the page state including adding and removing controls for the next post.
So to clarify here's how I would imagine your page flow:
OnInit - Construct your controls and populate with the initial values.
OnLoad - During postback your control will be populated with the users updated values.
OnClick - Use the event of the user's button click to then save the updated values.
I have a page that that contains a User Control and a submit Button. The User Control contains a Drop Down List.
I want to make the Button on the page visible if the user selects an item in the User Control's Drop Down List.
How can I make the page recognise the Drop Down List OnSelectedIndexChanged event?
Expose a custom public event in your user control - MySelectedIndexChanged. Subscribe to this event in your page class. Effectively delegates and events.
Read more on MSDN
Add own event to your UserControl and subscribe on it in page's code-behind. In SelectedIndexChanged event handler fire your event if somebody subscribe on it. Detailse explanation with code you may find here: Easily Raise Events From ASP.NET ASCX User Controls
I have a fairly standard DataGrid. It contains a few BoundColumns.
I'm overloading one of these columns to contain either text or a LinkButton depending on some characteristics of the bound item.
So.. in the ItemDataBound event for the Grid, I check a few things and add either a Label Control, or LinkButton Control to the proper Cell.
If I click on one of these LinkButtons, a postback occurs, but it does not call the method in the event handler of the button.
I have seen some people say that events will not work if you create controls AFTER the PreInit page event. Is that true? If so, there must be a way I can manually wire these up?
I have tried creating all of the buttons in PreInit and only adding them in ItemDataBound which unsurprisingly did not work.
I have heard others say the buttons and each parent control must have a unique ID. Is that true?
Rather than blindly swing away at this, I'd like to understand exactly why I can't do this.
Thanks
As far as I understand it, if you put a link button inside a DataGrid you need to use the RowCommand, i.e.
<asp:LinkButton ID="btnUpdate" CommandName="Something" CommandArgument='<%# Bind("something") %>' runat="server">Update</asp:LinkButton>
Then you need to bind the OnRowCommand event to a function in your DataGrid. This will pass the Command Argument that was given to link button and the name of the command in an Event Argument. You can use this to re-act appropriately to the event.
I am having a DataGrid in one usercontrol which is getting filled with some data when I press some button(ouside it). This Datagrid is filled by one linkbutton also. When I click this LinkButton then OnItemCommand or SelectedIndexChanged should fire, but both the events are not firng. While the control's PageLoad event is firing.
Please let me know where I am doing the mistake.
Thanks
Is your control added dynamically, using something like Page.LoadControl()? If so, events won't fire unless the control is dynamically added for each request. That's the only way the control tree can be properly built again. I usually do this in Init().
https://web.archive.org/web/20211029043701/https://www.4guysfromrolla.com/articles/042402-1.aspx
For those that see this post, looking for an answer. Most likely, you're rebinding the data to the datagrid on every call. Use a conditional and check !this.IsPostBack.
I have button Add and Remove on my page.
Add button adds one checkbox, two textboxes and one dropdownlist to a new line on my page.
Remove button removes them.
I have this running nicely by following Joe Stagner's example.
Problem:
The controls that are created dynamically all need to fire the same event when checked (for checkboxes), also for selected index changes (for dropdownlists).
I have tried to add event handler when I create an object but it doesn't seem to fire?
you need to persist the dynamically created controls in some way [session, viewstate, etc.] for each page load. Recreate the dynamic controls and re-bind the events using delegates on each page load in preInit function.
I think you're probably running into the fact that your page, upon each page post, is being completely recreated - essentially the page has to duplicate what controls were on your page before it can attempt to feed postback (and events) to them. I think what you probably need to do is add code to your page_load which will re-create the dynamically created controls, with the same ids as they had, and register the event handler.
Sounds like you have a page life cycle issue.
For a dynamically created controls to fire events you should create them in the PreInit event of the page.
Here's a link to a cheat sheet for Asp.net page life cycle.
yeah,
like what all said, it is Life cycle issue.
when you load user controls dynamically you should always do the following.
Assign a unique ID for each User Control.
Reload the user controls on Page_Load or Page_Init Events.
and to make it all easier i suggest to abstract the loading to a function that you will call from Page_Load and Page_Init as mentioned before, this function will check if hte target user control was loaded and will load it again for you, to do that, you store the loaded user controls IDs in Session or viewstate.
hope this helps.
If you want to do it without auto post back you can remove the auto post back and throw and ASP button on there. Any runat server should fire off your dynamic event handlers.