Issue with page life cycle (asp.net)
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 deletio开发者_运维问答n 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.
精彩评论