Search

Showing posts with label Update Panel. Show all posts
Showing posts with label Update Panel. Show all posts

Feb 19, 2009

File Upload not working with update panel

I was having problem while using File upload control which is inside the update panel and which was placed in user control. While doing post-back the value of PostedFile was blank, although page was properly valid.

Then I checked the Form property of Request object, the file upload control was missing in the AllKeys collection

We all know that if you want file uploads the form encode must be multipart/form-data. This is the reason why File Upload is not working properly inside Update Panel, lets understand this with following example.

Problem:

File upload is not working with update panel + asp.net

You are having two controls which needs to get display after one-another or post-back, first control does not have File upload and Second control does. Things to notice here is form tag will be reside either in ASPX page or Master page not in UserControl; and our File Upload is inside user control. Now when we load first control which does not have File Upload so the form will not encoded with multipart/form-data, now when you load second control which has the File Upload hence fort encode must be multipart/form-data, which is unfortunately not changing due to Update Panel.

Solution:

In problem definition we come to know that form must be encoded with multipart/form-data if we want to upload the file; solution is very simple, we just need to change encoded attribute of form with following single line!!!

Page.Form.Attributes.Add("enctype", "multipart/form-data");

Sep 30, 2008

Error: Validation of viewstate MAC failed

This is common error generally working with UpdatePanel.

On your screen you see message "Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."

You can fix it in two ways, setting in web.config or in page it self.

In web.config:

<pages enableEventValidation="false" viewStateEncryptionMode ="Never" />



Or in page you write following line in @Page directive.




<%@ Page EnableEventValidation="false" ViewStateEncryptionMode="never" ...

Sep 16, 2008

Reducing page load times with UpdatePanels and timers

Hi all,

Some time we have requirement like, on single page we are having 5-6 controls, which at the same tiem only single is visible and by navigation we can view all other controls.

This is very specific to Ajax Tab Implementation. Where we are having 4-5 tabs, in which we are having server controls, so on Page load all the controls get loaded and page become heavy also slow.

There is one technique to optimize this. As we all know we have to show single tab once page is get loaded, put all the controls into Asp:Panel and except one which we have to show on default; make other to visible false. The enable of all visible panel will be done using timer control. Time control will make enable the Asp:Panel one by one. As all the Asp:Panel will be in UpdatePanel will page will not get refreshed.

I found the same code in one of the post of Glavs Blog.

He provides the code along with video. Have a look at them and make optimize your page. I used this and found very good, even we can now use this on same page.

Jul 24, 2008

User Control and Javascript in UpdatePanel

Hi All,

There is general practices where we require some javascript function while useing UpdatePanel; along with the User Control; and the more problamatic is when the User Contorl is dynamic.

Problem:
When we use UserControl which change the state of Ajax postback; sometimes we require some javascript which needs to be embaded after loading the UserControl and with this said I am using UpdatePanel. Means my code is in the UpdatePanel.

Solution:
Generally we are using following code to run any javascript in code behind

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "alert('hi');", true);

Now if you are using UpdatePanel then replace you above code with following code.

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key", "alert('hi');", true);

Now you can see the alert saying 'hi' to you!!