I have got this event in my page aspx.cs:
public void deleteBtn_Click(object sender, CommandEventArgs e)
{
UsefulStaticMethods.DeleteComment(int.Parse(e.CommandName));
}
I am trying to delete a comment from the page. The deletion is successful. However, the website interface doesnt update itself after that event happens.
My Page Load is responsible to drawing all the comments on the page with a dynamic button (delete comment).
I know when the delete button fires, the page Load fires before.. and thats a bit is a problem..cause the page load recreates the page interface, while the deleteBtn_click deletes the comment, and I want to update the interface straight away... "Refresh" the page without the comment that was deleted..
If i execute a function to draw the whole table again, it will draw another comment list along with the comment list drawn at the page Load event.
I cant not refuse not to draw the comment list at page_load, cause i need everything recreated at postback time (including the dynamically created button).-cant use !Ispostback
The question is how can i achieve this/overcome the issue?
Typically, if your using data-binding then you can just re-bind the control in question. Perhaps, you should modify your function that draws the comment list to clear the existing list (possible by removing rows from table control or clearing control collection from container panel or placeholder (you can introduce a placeholder control just for clearing purpose)).
Yet another hack to refresh the page is to restart the page-life cycle by doing Server.Transfer to the same page. Generally, I wouldn't recommend this approach unless page code structure is very complicated and refreshing the data would take many lines of code.
You need to rebind control. Suppose your button is in Grid than you need to Rebind() grid. If not than there is one more way. Put Contents in Update panel and set Update panel Trigger with Delete button. SO when Delete button is clicked that update panel cause Update.
Use data list or repeater server control to display comments and than bind the server control again after deletion. Use !Ispostback on Page_Load.
Related
I have an aspx page with the following code:
<asp:Table ID="JsonContent" runat="server">
</asp:Table>
In my behind code at Page_Load i add Items to Table.
Table consist two rows:
Row num 0 is Cell that contain TextBox
Row num 1 is Cell that Contain TextBox
Additionally i have a button save, when the user click the button i need to go over all table row and write the data at Text Prop to File.
The problem is that when i click the Button Save the event page_load raise and all text box control is not already exist so i can no write the edited data to file.
I read about similar post with this issue, but could not get a solution.
Thank!
First of all, you should learn about asp.net pages web cycle : https://msdn.microsoft.com/en-us/library/ms178472.aspx
And i would say that #TimSchmelter has the perfect solution for you in the comments :
"You should (re-)create all dynamically added controls in Page_Init in every postback because all objects are disposed when html is sent to the client. But even if you recreate them there they will persist the user-input if you assign the same ID as before"
This means you have to use the function page_init (wich is similar to page_load but not automaticly generated, you have to write it manualy)
I did this once too, and I confime that Tim's solution will work.
Just make sure that your page_init() fucntion is reached.
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 want to make use of "complex" usercontrols with more than one control element within. It's the same control I will reuse in the list, and I have a PlaceHolder control for it already.
I can add the control with LoadControl(path to .ascx) - no problem.
I can through my custom properties get/set access the embedded Labels, too, so I can initialize each control perfectly.
But when adding LinkButtons, I get into trouble/problems.
When I click the button, I do get a "submit" of the page rendering the controls; but the control's own button event does not seem to fire (or at least PageLoad on the parent page seems to fire first?) - I can't figure out where my event goes or where to look for a name/ID or parameter for this button.
How come or what am I doing wrong here?
I've made a "fake button" now by using a label more with a "hardcoded A HREF" with an ID in the URL, but I would like to learn what event I need to catch and where or how to init the button, because I want to be able to use "default ASP.NET" controls for these usercontrols (hopefully without too much patchwork-coding)...
The only reason that events get "lost" is because your controls are not being recreated in such a manner that ASP.Net can associate the event with the control after the postback. It does so through the use of the ID property.
In other words, you're doing one of three things wrong:
1) You're assigning the ID's of your linkbuttons differently during the creating phase in Init after the postback
2) You're creating your linkbuttons dynamically using code, but you're doing it after the Init phase of the page lifecycle, so that your controls do not participate in ViewState.
3) You're re-binding the datasource of the parent control containing the linkbuttons on every postback. Use if (!IsPostBack) to prevent rebinding it every time.
Without seeing your code I can't give anything more specific than that unfortunately.
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.
My problem is that all the textbox's my formview are getting cleared when I hit the submit button.
I currently have a page with a small section that has an update panel around it. This small section adds an address to my databse. To the left of this form there is a gridview that is tied into the formview. So if i click on an item in the gridview its contents fill the address section(formview) with the correct data.
When I hit add the data gets validated in the c# code behind, and if all the info is correct the address is inserted. If there is an error the entire form is cleared and the error message(label) is displayed.
I have already done this many times in other pages, but none have had the gridview tied to the formview, and they have all worked. I tried removing the gridview and the form still erases itself.
Is there some reason that .net thinks it should be clearing the form? When in other cases it decides it won't? If so what are these cases, or what general tips should I try to solve this?
in the page_load are you using if(!Page.IsPostback) { ... } so if it's a postback nothing gets re-bound?
is the ViewState enabled?
Yup many hours later I found that a single panel that was wrapped around the section had a EnableViewState="false" added to it. Sad part is that I know I didn't add that because I didn't even know what it was until craig here mentioned it. Visual Studio must have added it sometime.