Refresh content-page asynchronously? - asp.net

I have a master page "PartialUpdate.Master" and 2 simple content pages "WebForm1.aspx" and "WebForm2.aspx"
WebForm1.aspx/WebForm2.aspx: the content-pages just display their filename
The master page "PartialUpdate.Master" has 2 buttons and a ContentPlaceHolder
in an UpdatePanel. When clicking on a button I want to see the corresponding content-page
Here's what it looks like...
PartialUpdate.Master :
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Master Page
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional"
runat="server">
<ContentTemplate>
<asp:Button ID="btnForm1" CommandArgument="WebForm1.aspx"
runat="server" Text="Form1" OnClick="ChangeForm_Click" />
<asp:Button ID="btnForm2" CommandArgument="WebForm2.aspx"
runat="server" Text="Form2"
OnClick="ChangeForm_Click" />
<hr />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnForm1"
EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnForm2"
EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
The code behind of PartialUpdate.Master.cs
protected void ChangeForm_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
Response.Redirect(btn.CommandArgument);
}
Problem here is that Response.Redirect() triggers a full-page postback.
I just want the Master to refresh the content located in the 'ContentPlaceHolder' asynchronously (that's why I added the AJAX UpdatePanel)
In other words, I'd like to dynamically change the content-page that the Master is displaying whitout causing a full-page postback.
Is this possible? If so how?
thank you
Chris

You can call with WebRequest in ajax call and write the content you recieve to the page...
But you can`t "knowing" the browser that you want to redirect to another page without post back.

Related

change text on button and enabled status after click button(asp.net)

I use ASP.NET to develop.
Here, I want the thing is that once I click button A (the text on the button A is "Start"), then the text on the button A will change to "processing...please wait" and button A will become not clickable. Then execute the stored procedure(the duration of stored procedure is 1 minute). After the stored procedure finishes, the text on the button A will change to "Start" and button A will become clickable.
I tried that before stored procedure, I added these codes:
ButtonA.Text = "processing...please wait";
ButtonA.Enabled = false;
And after stored procedure, I added these codes:
ButtonA.Text = "Start";
ButtonA.Enabled = true;
But the outcome is the text on the button A is not changed and button A is still clickable during the stored procedure is executing.
Can anyone tell me how to achieve what I want? Thanks.
Then I edit my aspx file to be below:
<asp:ScriptManager ID="MainScriptManager" runat="server"/>
<asp:UpdatePanel ID="pnlHelloWorld" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="ButtonA" runat="server" OnClick="ButtonA_Click" Text="Start" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonA" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
But the text of button still remains the same and the button is still clickable.
You can achieve your requirement by using Ajax. Using Ajax you can do partial page refresh.
Put ButtonA inside the UpdatePanel, and it should work as expected.
You can refer below sample code for reference.
webpage.aspx code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Hello, world!</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlHelloWorld" runat="server">
<ContentTemplate>
<asp:Button runat="server" ID="ButtonA" OnClick="btnHelloWorld_Click" Text="Update label!" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
c# code
protected void btnHelloWorld_Click(object sender, EventArgs e)
{
//Before Procedure call
ButtonA.Text = "processing...please wait";
ButtonA.Enabled = false;
//Call Procedure
procedure ....
//After Prodedure call
ButtonA.Text = "Start";
ButtonA.Enabled = true;
}
Hope that helps, thanks.

Partial update doesn't work with master pages

I am working on a C# asp.net web forms project. It has two master pages. I
have an user control which reads data from database,
creates an un-ordered list in html string and populates a placeholder with
it. This user control has to be automatically
refreshed every 2 minutes. I have included this usercontrol on the parent
master page. I have the following code to refresh the
user control, which I have obtained through another stackoverflow answer.
The problem is that the entire master page refreshes, and I am not sure why.
Is there a way to make that only the user control which has the UpdatePanel
refreshes?
Outer Master Page:
<body>
<form id="frmMain" role="form" method="post" runat="server">
<div>
<uc2:PendingOrders runat="server" ID="PendingOrders" />
</div>
<asp:ContentPlaceHolder ID="MainBodyContent" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
User Control:
<asp:ScriptManager ID="myScriptManager" runat="server"
EnablePartialRendering="True"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode = "Conditional"
runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="20000"
OnTick="Timer1_Tick"></asp:Timer>
<asp:PlaceHolder runat="server" id="lblMyOrders"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
User Control Code behind:
protected void Timer1_Tick(object sender, EventArgs e)
{
//This method creates the html string with data as an unordered list
and
//populates asp:PlaceHolder inte updatepanel
GetData();
}
UpdatePanel1's Content will be update by Timer1_Tick.
"entire master page refreshes" means master page's Page_Load will be call while Timer1 been trigger?
UpdatePanel always post entire page back server and only render UpdatePanel1's Content.
I had the same problem, but in our project the EnablePartialRendering was set to false on the Master Page, if set to true the whole project breaks.
To fix the problem, I defined another master page without ScriptManager and added the ScriptManager to the page in which I wanted to use UpdatePanels with the EnablePartialRendering attribute set to True.
This fixed the problem.
<asp:ScriptManager ID="myScriptManager" runat="server" EnablePartialRendering="True"></asp:ScriptManager>

ASP.NET Trigger UpdatePanel on HTML Button click

I have created a User Control in which I use a JQuery Plugin (DataTables).
I would like to refresh the User Control when I click on a button that I have generated inside the User Control like this :
_DT += "<button id=\"validConf\" runat=\"server\" type=\"button\" class=\"btn btn-success\" onclick=\"$('#modalConfig_" + ID + "').modal('hide');\">Valid</button>"
Now, in my Page I have the following code :
<asp:ScriptManager EnablePartialRendering="true" ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<UC:Grille runat="server" ID="Grille1"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="validConf" />
</Triggers>
</asp:UpdatePanel>
It returns this error : "Unable to find control in Update Panel for trigger"
Am I not doing it the good way or am I missing something ? Is it even possible ?

Script controls may not be registered before PreRender. when using timer control

I'm a newbie to ASP.net (vb), and i'm working with the timer controls. I've once again researched the answer to this and done as said (overloading prerender method). My problem is I have a master page which i've added a timer control, in order to update a count down field, on going to the sites index page i keep getting the error : "Script controls may not be registered before PreRender." The timer works well on other pages except the index page.
<form id="fMain" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer></div>
<div>
<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Literal ID="litTimer" runat="server"></asp:Literal>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
The Codebehind:
Sub timer1_tick() Handles timer1.Tick
ltr1.text = "test"
End Sub`
Thank you in advance.
This is because you are using Telerik controls and have overridden the OnPreRender method.
You need to add the following to this method:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

FileUpload control postback problem

I'm having a FileUpload control on a aspx page inside a UpdatePanel with a button on click of which I want to set a label with the filename of the selected file.
Here is the code I have:
ASPX PAGE:
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fuSimple"></asp:FileUpload>
<asp:Button runat="server" Text="Button" ID="btnPostback"
onclick="btnPostback_Click" />
<br />
<asp:Label ID="lblFileName" runat="server" Text="File Name: "></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Code Behind:
protected void btnPostback_Click(object sender, EventArgs e)
{
lblFileName.Text = "File Name: " + fuSimple.FileName;
}
Every time I hit the button, I'm getting an empty string. What am I missing?
Also, when I do this without UpdatePanel it works fine.
Comments/help appreciated.
The FileUpload control is not supported with ASP.NET AJAX and Asynchronous Postbacks. They require a full postback due to the nature of how a file upload works (multipart form data).
The following question should have useful answers: FileUpload control inside an UpdatePanel without refreshing the whole page?

Resources