updatepanel updating whole page instead of just the contenttemplate - c#

<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" ClientIDMode="AutoID" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">
<%
foreach (var item in AllSales)
{
//Here i have just set a breakpoint to see if it loops the AllSales list when I press the update button
}
%>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The script manager code is in the masterpage :
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Problem here is that everytime i click the Update button it loads the page again and loops the "AllSales" list , i want to only update a section and not have to do unnecessary loops.
Here is the fun part : If i remove the masterpage, it works ! But with the masterpage, it dont , why?!

You need to use a server control to initiate the update rather than a normal html input button. Try using:
<asp:Button ID="ActivitySubmit" Text="Submit" runat="server" />

The reason for such behavior is that you use plain html submit button instead of ASP.NET server button. Thus page submitted to server without involving ASP.NET Ajax functionality. Replace ActivitySubmit button with asp:Button control

Set the UpdateMode mode property to Conditional. The default value for UpdateMode is Always, If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page, reference
Edit: You are using input type="submit" which probably causing the post back replace it with asp:Button to get the ajax call to work.

I have tested the same code with only change in the button type, and it refreshes only the update panel content. The problem is your site is targeting .NET 3.0 but you need to target at least to 3.5. I have tested on 3.0, it does not work but on 3.5 and 4.0, it works fine. So the easier and safer solution is to target 3.5 onwards. But If you want to use .NET 3.0, I will try to find a workaround. Please let me know.

I tried to recreate your problem but it is working fine in my case. Try creating a new .aspx file and paste the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Outside UpdatePanel</p>
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="SubmitButton" Text="Go" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
This was tested in VS2010 using .NET 4.0 in an Empty Web Application. Does this work for you as well? I tried the same example using a Master page with a content placeholder. This yields the same (properly working) results. From this I gather there is something else going on on your page that we're missing. Is there?
[Edit]
I made another simple example, this time with a Master page and a Content page.
Master page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<p>Master Page: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Again this works without a problem in the same environment as the other example. It looks like you'll have to tell me about the .NET framework you're targeting and what else you've got set up in your master page and aspx page, because:
In .Net 3.5 and up this will work. In lower versions however, it won't. I'm afraid I've been unable to figure out how to fix it in lower versions though. :(

In your Page tag just add
ClientIDMode="AutoID"

I think you can try and put the form inside the updatepanel, or you can try and change children as triggers property of the updatepanel like this :
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="False">
and then add the button as trigger like this
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(SubmitButton);

try addind this part to your UpdatePanel:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Submit" />
</Triggers>

Related

Asp.net Webform Validation for working

Validation is not working in asp.net webform project for some reason event on a simple test page validation doesnt work, It always returns true and submits the form without validating.
Below is the actual test page i am trying to make it work to know the reason why it is not working.
Could issue be related to some reference issue or wrong library
Same file works in other project but not in this project, did i miss any file or reference.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="en_Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rf1" ControlToValidate="TextBox1" ValidationGroup="vgForm" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" ClientIDMode="Static" runat="server" Text="Button" OnClientClick="return ValidateForm();" ValidationGroup="vgForm" />
</div>
</form>
</body>
<script>
function ValidateForm() {
Page_ClientValidate("vgForm");
if (Page_IsValid) {
alert('it is valid');
return true;
}
else {
alert('No valid');
return false;
}
}
</script>
</html>

UpdatePanel working only in debug mode

The update panel control is not working in normal mode i.e. when i am browsing it without debugging while in debug mode it is working absolutely fine.
My code is as follows-
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Timer runat="server" id="Timer1" Interval="5000"></asp:Timer>
<%-- web content here --%>
</ContentTemplate>
</asp:UpdatePanel>
Please suggest for working of the above in normal mode without debugging.
Thanks in advance.
`<head runat="server">
<title></title>
<meta http-equiv="refresh" content="10" />
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<%-- web content here --%>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
in .cs page i used
protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel1.Update();
}`
i used simple meta tag in under the tag in desing page, its working fine for refresh page
The META tag will refresh the entire page, before calling the Page_Load method.
I also noticed that updatepanels stopped working for me too, after .NET4.5. Maybe its a bug in the framework, or maybe something is blocking it. Its even worse when you are using bootstrap with your project. As UpdatePanels are script driven, some hidden bootstrap script may be interfering with it.
So, if its not too much trouble, you could wire up all ur events using jQuery, and where you need to make server calls, you can use ajax to call a project-hosted webservice. This way, your page doesn't load at all, unless u want it to.

Asp.net FileUpload problem "Arithmetic operation resulted in an overflow." ContentLength is always -2

Hi all I have an update panel that works lovely in my test solution but when I put it into the main project it does not work correctly.
I've made it very simple, but still no joy, it consists of:
the file upload control
a link button
the link button has an onclick method that takes the file and creates a byte array. For some reason the contentLength is -2 every time. It does not matter what type of file I am using. Every time!
This is very frustrating considering it works fine in my test solution.
Is there anything I am missing or should be looking at?
Thanks :)
EDIT:
I am using VS2008
CODE:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:LinkButton ID="btnUpload" runat="server" ValidationGroup="uploadform" CssClass="uploadbutton" OnClick="btnUpload_Click">Upload</asp:LinkButton>
</form>
</body>
</html>
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
var intDoccumentLength = FileUpload1.PostedFile.ContentLength;
// will crash here as content length is -2 for some reason~???
byte[] newDocument = new byte[intDoccumentLength];
}
Your test code must be doing complete postback which is not the case for Updatepanel part as part of main project. With updatepanel, file may not have been uploaded at point of time when you are checking its contentlenght; this is not true for complete postback where file always gets uploaded first. In this case, it will always through you an error. This can only be done using an ActiveX control of some kind.
This article may give you nice hint and direction: asp.net FileUpload event after choice is made and before submit for upload
The file up loader is not working well in the update panel so if you are using the update panel then you have to use the triggers in the update panel and you have to give the name of the control or button on which you are clicking to upload the file
<%# Page Language="C#" MasterPageFile="~/FullViewMasterPage.master" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" Title="Untitled Page" %>
<%# Register TagPrefix="yaf" Namespace="YAF" Assembly="YAF" %>
<%# Register TagPrefix="yc" Namespace="YAF.Controls" Assembly="YAF" %>
<asp:Content ID="Content1" ContentPlaceHolderID="FullViewContentPlaceHolder" Runat="Server">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload id="fileUpload" runat="server" ></asp:FileUpload>
<asp:Button ID="Upload" runat="server" OnClick="Upload_Click" Text="Upload The Image" /><br />
</ContentTemplate>
</asp:UpdatePanel>
<br />
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Upload" EventName="Upload_Click" />
</Triggers>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime3" runat="server" /><br />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</asp:Content>
<code>
As FileUpload.PostedFile.InputStream.Length returns the same as FileUpload.PostedFile.ContentLength, did you check that your file size is lower than maxRequestLength (4MB by default) or specified in web.config :
<system.web>
<httpRuntime maxRequestLength="8192"/>
</system.web>
See : http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx

Encountering Error when doing post-back using __doPostBack()

I am getting an error on javascript when doing post back. The code is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function DoPostBack()
{
__doPostBack('Button2','My Argument');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="Button2" value="Press me" onclick="DoPostBack()" />
</form>
</body>
</html>
I am getting the following error:
Line: 13
Error: Object expected
I can't understand why this error is coming. Kindly help...
you can use a hidden button to do this task
Button1.Attributes.CssAttributes.Add("Display","None");
after hiding the button
you can call its click function from javascript
document.getElementById('<%=Button1.ClientID%>').click();
this will call Button1_Click on server
** remember to set UseSubmitBehaviour=false to make this work on non-IE browsers
hope that helps :)
__doPostBack is not created by default. If the page does not have a control that causes a postback then ASP.NET does not create/generate this method.
In your case you can force ASP.NET to generate __doPostBack by adding the following line in you Page_Load event:
ClientScript.GetPostBackEventReference(this, string.Empty);
This line will force the creation of this method.
_doPostBack isn't created by default. It appears when you are adding control with autoPostBack=true or adding some grid with buttons in it.
So there is no _doPostBack javascript generated in your code.
If you add
<asp:DropDownList ID="list" runat="server" AutoPostBack="true">
<asp:ListItem Text="first"></asp:ListItem>
<asp:ListItem Text="second"></asp:ListItem>
</asp:DropDownList>
for instance your code will work.
Don't know if it is really useful code :) however.

Button in update panel is doing a full postback?

I'm following a simple example of how to use the update panel from here (http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx). Outside the update panel i've another html input control which calls a javascript function which displays an count to the user in an alert box. simple stuff. My problem is that the page behaves differently when running on IIS and on the inbuilt asp.net web server (cassini). Under IIS clicking the button within the update panel causes a full postback and so the count displayed to the user in the js function gets reset after that eachtime. under the inbuilt web server hitting the button inside the update panel behaves how i would expect it to and how i want it to in that it refreshes the label only so that the counter on the client side isn't reset.
.net 3.5 is the target framework and I'm running IIS 5.1.
I've seen posts elsewhere describing the same problem (http://forums.asp.net/t/1169282.aspx)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var count=0;
function incrementCounter()
{
count ++;
alert(count);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<input type="button" id="Button2" value="JS Clicker" onclick="incrementCounter();" />
</form>
</body>
</html>
Update:
Thanks Crossbrowser for your answer. My reply will take up to much room in the Add Comment window. Ok, so following this simple example here (http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx) you can see that the update mode is not set to conditional so I've reflected those changes. However my problem still persists. That is that the page when running on IIS causes a full postback. i.e. the progress bar in you browser loads, the screen flickers, client side count that i was maintaining is lost. Running the code on the inbuilt asp.net webserver does not. That is the crux of my problem. I've come across this problem by others (http://forums.asp.net/t/1169282.aspx).
So my question is what is different when running on IIS compared to the inbuilt asp.net one?
Updated Code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var count=0;
function incrementCounter()
{
count ++;
alert(count);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<input type="button" id="Button2" value="JS Clicker" onclick="incrementCounter();" />
</form>
</body>
</html>
Since you are using the .NET Framework 3.5 I will assume you are using Visual Studio 2008, and you say you are targeting IIS 5.1 for the production platform.
The local web server that is part of Visual Studio 2008 is based off of IIS 6/7 architecture, not IIS 5. So, to answer your question of what is different with IIS compared to the local web server... unfortunately, in this case, you are mixing apples and oranges.
Are you restricted to IIS 5.1?... ie client mandate or some other reason. If you are not, and you are developing with Visual Studio 2008 (.NET Framework 3.5) you really should be using IIS7 (or at least 6) as you would most likely not have this problem.
Again, IIS7 may not be an option for you.
Have you tried using a trigger? e.g.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Panel Refresh" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Label1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Since your UpdatePanel's UpdateMode is set to conditional you have to specify a trigger.
Alternatively, you could define the property ChildrenAsTriggers to true.
UpdatePanel.UpdateMode reference
In IIS Manager, check the "Pages and Controls" settings for your site.
Specifically, the View State and Settings sections. Those look like they could affect how your page interacts with the server and when.
Did you try using the <asp:Button runat="server"> element instead of the HTML <input> element?

Categories

Resources