UpdatePanel with dynamically added buttons  | Newbie | | Join Date: Aug 2007 Location: Belgium
Posts: 20
| | |
Here's the problem,
We're still using .NET 2.0. I have a custom grid view whichs includes a context menu. Each item in the menu is a Button.
One button action, the export to excel, wants to do some Response.Write calls, but this does not work as it's giving me the "Sys.WebForms.PageRequestManager" exception whichs tries to parse whetever I write to Response as some kind of Ajax I guess (don't know the full details). Which I can fully understand, because when I press the update, only the update panel does a post-back and not the entire page, so I can not change the Response.
I would have to set a PostBackTrigger on the Export to excel button. The problem is that I can not seem to do this before the PreRender event because then I will get an exception at trigger initialization saying it cannot find the control with the ID of my button. However, if I do this in PreRender phase, it WILL find the control, but nothing changes and still a partial update is performed (I guess because the triggers are already initialised at that point?).
Does anyone know this problem and a solution? I can put the postback trigger on my grid, but that would have performance issues I would like to avoid when it's not needed. I can not use the AddPostBackControl method of the ScriptManager - which is the best way to do it - because we're in .NET 2.0.
Or should I take a completely different approach?
Thanks guys
| |
best answer - posted by Pittaman |
Hey, thanks for the explanation...
I actually ended up solving the issue in a very different way.
I add my context menu buttons to a panel and add that panel to the Controls collection of my server control before the CreateChildControls is called (so in OnInit). At that point, I also attach my event handlers to each of the buttons. This ensures me that the handlers will be called. Although the menu is shown without adding the panel to the controls collection, this is vital because if it's not in there CreateChildControls will not be called and my event handlers will not be triggered.
So now my button clicks raise server side events and allow me to run server side logic. For the export to excel I had the problem that I could not immediatly modify the Response.Write. I made it easy for myself by using the following technique:
1) In my Server Control there's a class ExcelViewer, which inherits from Page (so it is a page).
2) That class contains a shared field (shared, to make it easy for myself) which contains the data for the excel.
3) In the event handler I generate the actual excel file as a string and then assign it to the shared field of my ExcelViewer class. After this, I raise an ExcelExported event.
4) In the Page_Load of the ExcelViewer I do my Respone.Write manipulations and write the excel data to it (I change the content-type etc...)
5) In the website that is using my grid all I have to do is create an ASPX page which inherits from my ExcelViewer page and then handle the "ExcelExported" event of my grid. In that handler I simply do a Response.Redirect() to my ASPX page that inherits from the viewer.
The Response.Redirect should work even if the grid is inside updatePanels.
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: UpdatePanel with dynamically added buttons
I'm going to suggest moving the function that exports the data into its own ASPX page. Place an iframe on the page and set the src of the iframe to the ASPX page that exports the excel sheet whenever the user clicks the "export" button. This way the ASPX page can use response.write to export the page into the iframe instead of the main page.
-Frinny
|  | Newbie | | Join Date: Aug 2007 Location: Belgium
Posts: 20
| | | re: UpdatePanel with dynamically added buttons
I see what you mean, but the problem is that the Grid is actually a server control. And I do not think it is possible to embed that other page inside the control, which is a requirement.
This was the first approach we took but I think it's not possible, but maybe I'm mistaking?
Thx
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: UpdatePanel with dynamically added buttons
Ok, well if that doesn't fit your requirements I am going to recommend that you put the GridView in an UpdatePanel of it's own and set the GridView as a full page postback trigger for the UpdatePanel.
I tried in the past to set individual, dynamic buttons/links within a GridView as triggers for an UpdatePanel but found out that it's impossible. I tried everything I could think of but could not get this to go.
The easiest way is to set the entire GridView as a full page postback trigger.
|  | Newbie | | Join Date: Aug 2007 Location: Belgium
Posts: 20
| | | re: UpdatePanel with dynamically added buttons
I know, that is what I might be doing, but it would kind of defeat the purpose of using an update panel, as most postbacks will happen in the grids.
Thanks for the link by the way! Then I know some stuff to exclude. I will keep you updated, because we'll have another look at it.
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: UpdatePanel with dynamically added buttons
I'm going to be removing the link shortly because it's actually against forum rules to post links to other forums. I figured it would help you to see the problems that I ran into when I was attempting the same thing as you. Let me know when you're done with it so I can remove it.
I just re-read your question and it for some reason I missed the fact that you're using a server control.
We actually might be able to solve your problem by converting your server control into an Ajax-enabled server control. We'd implement a JavaScript Object that could solve your problems. Designing this JavaScript Object will be interesting but...could be fun :)
|  | Newbie | | Join Date: Aug 2007 Location: Belgium
Posts: 20
| | | re: UpdatePanel with dynamically added buttons
I'm not very familiar with AJAX programming, could you explain to me what the functionalities would be of the "javascript object" and how it would help solve the problem? Would it pop-up a new screen in which we can freely change the content with Response.Write? Or is it another approach?
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: UpdatePanel with dynamically added buttons
Well, you could do the pop-up thing but I was thinking about something a little different. I would think that the JavaScript Object would do 3 things.
First of all, it would dynamically create an iFrame element that you could use as I my original suggestion.
Secondly, it would provide a method that would set the src of the iFrame to the ASPX page that will generate the excel file.
Lastly, it would hijack the onclick events for the dynamic links (buttons?) in your GridView to make them execute the JavaScript method that sets the src for the iFrame.
What do you think?
-Frinny
|  | Newbie | | Join Date: Aug 2007 Location: Belgium
Posts: 20
| | | re: UpdatePanel with dynamically added buttons
Hey, thanks for the explanation...
I actually ended up solving the issue in a very different way.
I add my context menu buttons to a panel and add that panel to the Controls collection of my server control before the CreateChildControls is called (so in OnInit). At that point, I also attach my event handlers to each of the buttons. This ensures me that the handlers will be called. Although the menu is shown without adding the panel to the controls collection, this is vital because if it's not in there CreateChildControls will not be called and my event handlers will not be triggered.
So now my button clicks raise server side events and allow me to run server side logic. For the export to excel I had the problem that I could not immediatly modify the Response.Write. I made it easy for myself by using the following technique:
1) In my Server Control there's a class ExcelViewer, which inherits from Page (so it is a page).
2) That class contains a shared field (shared, to make it easy for myself) which contains the data for the excel.
3) In the event handler I generate the actual excel file as a string and then assign it to the shared field of my ExcelViewer class. After this, I raise an ExcelExported event.
4) In the Page_Load of the ExcelViewer I do my Respone.Write manipulations and write the excel data to it (I change the content-type etc...)
5) In the website that is using my grid all I have to do is create an ASPX page which inherits from my ExcelViewer page and then handle the "ExcelExported" event of my grid. In that handler I simply do a Response.Redirect() to my ASPX page that inherits from the viewer.
The Response.Redirect should work even if the grid is inside updatePanels.
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: UpdatePanel with dynamically added buttons
I'm glad you solved your problem!
You're right it's a completely different approach. I would never have thought of making a server control that inherits from Page. I'm impressed that you got it to work this way!
Congrats!
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|