Postback Problems by using UpdatePanel - c#

I have Used Asp UpdatePanel On Master Page To stop postback. But now it's Create problem on other pages, such as data fetching in gridview on button click.
<asp:UpdatePanel runat="server" ID="updatePanle1">
<ContentTemplate>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>

try this code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridviewBind();
}
}
<asp:UpdatePanel ID="updatepnl" runat="server">
<ContentTemplate>
---Put Your Grid View Code---
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>

You could try adding Triggers in your UpdatePanel desgin code here to stop the other events causing postback including button clicks and others. Try something like this :
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="clickEvent" />
</Triggers>
You can also try using PostBackTrigger instead of AsyncPostBackTrigger here incase if it doesnt work out as expected.
<Triggers>
<asp:PostBackTrigger ControlID="Button1" EventName="clickEvent" />
</Triggers>
Hope this helps.

We don't normally use the update panel just to prevent the whole asp.net site from re-loading page / posting back. Client controls (jquery) would still be the best fit. But if you're really curious you might want to grind time learning the lessons in this MSDN link https://msdn.microsoft.com/en-us/library/bb398864%28v=vs.140%29.aspx

Related

Unable to upload photo asynchronously with FileUpload control using UpdatePanel

In my ASP.NET Web forms application, I am adding a feature to upload files but I don't want to do a full postback. So to make it asynchronous I thought of using UpdatePanel.
This UpdatePanel contains a FileUpload control and a button to upload the selected photo. When I was debuggin my code to detect if file is actually selected or not, I found FileUpload's HasFile property to be false.
It works when I remove UpdatePanel but I don't understand why.
To help you understand, here is the code:
ASPX markups:
<asp:UpdatePanel ID="UPProf" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUDP" runat="server" />
<asp:Button ID="BUDP" runat="server" OnClick="BUDP_Click" Text="Upload your photo" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BUDP" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Here's its CS code:
protected void BUDP_Click(object sender, EventArgs e)
{
try
{
if (FUDP.HasFile) // code doesn't pass this if condition
{
FUDP.SaveAs("D:/Pictures/" + count + ".jpeg"); //it doesn't work since there is no file here
}
else
{
//Set some message for user
}
}
catch (Exception ex)
{
//log the error
}
}
Upload requires full page request. This is a limitation of the XmlHttpRequest component used in all AJAX frameworks for asynchronous calls to the application.
Remove the UpdatePanel or make BUDP button to perform postbacks.
<asp:UpdatePanel ID="UPProf" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUDP" runat="server" />
<asp:Button ID="BUDP" runat="server" OnClick="BUDP_Click"
Text="Upload your photo" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="BUDP" />
</Triggers>
</asp:UpdatePanel>

Stopping auto page refresh

I use the below code to auto refresh the page every 60 seconds via the AJAX tools in VS2010. Works perfectly.
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled" UpdateMode="Conditional">
<ContentTemplate>
ASP.NET/HTML Code
<p>
<asp:Button ID="Button2" runat="server" Text="Click here" OnClick="Button2_Click" /> to disable the pages automatic refresh.</p>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="60000">
</asp:Timer>
</asp:View>
<asp:View ID="View2" runat="server">
etc.
</asp:MultiView>
I want to include a button on the asp.net page to cancel the auto refresh.
I tried to include the below but when I clicked the button, it didn't work. The below is the Code Behind for an OnClick event for a Button. The asp.net code is in the above code.
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Interval = 0;
}
Where am I going wrong? Is this even a way to do this or do I need to go another route in order to allow the user to cancel the auto page refresh?
Thanks to PeterJ I have found the solution. I modified the code and since I clicked it the page has not refreshed. The issue was with my code behind for the button OnClick event. I had:
Timer1.Interval = 0;
When I should have had:
Timer1.Enabled = false;

Asyncpostback using standard button

Using an update panel I am attempting to perform an Async postback with a standard html button.
I have tried this:
<asp:UpdatePanel runat="server" ID="MyUpdatePanel">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnMyButton" EventName="Click" />
</Triggers>
<ContentTemplate>
<button runat="server" id="btnMyButton">ASyncPostBack</button>
</ContentTemplate>
</asp:UpdatePanel>
AND
<asp:UpdatePanel runat="server" ID="MyUpdatePanel">
<Triggers>
</Triggers>
<ContentTemplate>
<button runat="server" id="btnMyButton">ASyncPostBack</button>
</ContentTemplate>
</asp:UpdatePanel>
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = MyControl.ID;
trigger.EventName = "Click";
MyUpdatePanel.Triggers.Add(trigger);
Judging by my scriptmanager IsInAsyncPostBack value nether of these solutions seem to work.
I am not using a standard ASP.NET button because of issues that jQuery has with it and I am aware that I could put a hidden ASP.NET button inside the page and trigger that but am hoping for a better solution.
Event name should be EventName="onserverclick" instead EventName="Click"
Just to update others, its now works as EventName="serverclick"

Nested UpdatePanel: Why does ChildrenAsTriggers have no effect?

Given a simple example with two nested Update-Panels. They are nested, and each one has a label which is filled with the current time in the code behind. I don't understand why the ChildrenAsTriggers="true" - Property on the outer UpdatePanel has no effect? When I hit the 'Update Nested Panel' - button, the time in the parent UpdatePanel is not updated. But as far as I understand the property, it should be:
<asp:ScriptManager ID="ScriptManager1" runat="server"
onasyncpostbackerror="ScriptManager1_AsyncPostBackError">
</asp:ScriptManager>
<asp:Button ID="ButtonUpdate" runat="server" Text="Update Panel 1"
style="margin-top: 15px" onclick="ButtonUpdate_Click" />
<asp:Button ID="ButtonUpdateNestedPanel" runat="server" Text="Update Nested Panel"
style="margin-top: 15px" onclick="ButtonUpdateNestedPanel_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonUpdate" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" />
<asp:UpdatePanel ID="UpdatePanelNested" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonUpdateNestedPanel" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Label ID="LabelNested" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
Thx for any tipps!
sl3dg3
p.s.: code behind:
protected void ButtonUpdate_Click(object sender, EventArgs e)
{
LabelNested.Text = DateTime.Now.ToString();
Label1.Text = DateTime.Now.ToString();
}
protected void ButtonUpdateNestedPanel_Click(object sender, EventArgs e)
{
LabelNested.Text = DateTime.Now.ToString();
Label1.Text = DateTime.Now.ToString();
}
When the ChildrenAsTriggers property is set to true and any child control of the UpdatePanel control causes a postback. Child controls of nested UpdatePanel controls do not cause an update to the outer UpdatePanel control unless they are explicitly defined as triggers for the parent panel.
http://forums.asp.net/t/1422425.aspx/1
and this should be like...
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonUpdate" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonUpdateNestedPanel" EventName="Click" />
</Triggers>
......
......
I believe that, in order to see the desired effect in action, you should actually add your buttons as children in the content template. The "ChildrenAsTriggers" property only applies to direct children of the "ContentTemplate" block.
ChildrenAsTriggers="true"
Set the ChildrenAsTriggers property to true if you want postbacks from immediate child controls of the UpdatePanel control to cause an update of the panel's content. Child controls of nested UpdatePanel controls will not cause an update of the parent UpdatePanel control's content unless you call the Update method explicitly or you define the child controls as triggers.

asp.net timer to load page content

asp.net
c#
Our webpage currently contains a rather large web app which causes a lengthy delay when attemping to navigate to it. I'm currently implementing a WCF web service to utilize ajax but the delay is a concern to my employer so he wanted a quick and dirty fix in the mean time.
I would like to have the empty page load and then use a timer to load the content. This will cut down on perceived page load time but i'm unsure how to accomplish it.
Any help would be much appreciated
Shawn
Some code to get you started:
In the asp.net page:
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="1">
</asp:Timer>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
.... your stuff here
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="100">
<ProgressTemplate>
Please wait...
</ProgressTemplate>
</asp:UpdateProgress>
In the code behind:
protected void Timer1_Tick(object sender, EventArgs e)
{
this.Timer1.Enabled = false;
StartLongRunningTask();
}
Instead of a timer, you could flush the Response with Response.Flush().

Categories