When using UpdatePanel, the AsyncPostBackTrigger is not firing on the server side. Originally, I thought it was because I'm using a MasterPage and the ScriptManager was not loading correctly so I created a test template but it still won't fire. However, the UpdateProgress does appear. This button is being used to export a report from Crystal Reports. Below is my following code:
Client Side:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<%# Register Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script src="cr/crystalreportviewers13/js/crviewer/crv.js" type="text/javascript">
</script>
<script>
</script>
<form id="form1" runat="server">
<div>
<p>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
</p>
</div>
<div>
<asp:Label ID="test" runat="server"></asp:Label>
<asp:RadioButtonList ID="rblFormat" runat="server" RepeatDirection="Horizontal" Width="130px" >
<asp:ListItem Text=" Excel" Value="Excel" />
<asp:ListItem Text=" PDF" Value="PDF" />
</asp:RadioButtonList>
<asp:ScriptManager runat="server" ></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Button ID="btnExport" CssClass="btn btn-primary" Text="Export" runat="server" CausesValidation="false"
OnClick="btnExport_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnExport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div>
<font color="red"> Generating Download</font>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</form>
</body>
</html>
Server-Side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using static System.Net.Mime.MediaTypeNames;
namespace WebApplication1
{
public partial class Test : System.Web.UI.Page
{
ReportDocument crystalReport;
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.CrystalReportViewer1);
scriptManager.RegisterPostBackControl(this.btnExport);
crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("cr/missing_dob.rpt"));
crystalReport.SetDatabaseLogon("test", "test");
CrystalReportViewer1.ToolPanelView = ToolPanelViewType.None;
CrystalReportViewer1.ReportSource = crystalReport;
}
protected void btnExport_Click(object sender, EventArgs e)
{
ExportFormatType formatType = ExportFormatType.NoFormat;
switch (rblFormat.SelectedItem.Value)
{
case "PDF":
formatType = ExportFormatType.PortableDocFormat;
break;
case "Excel":
formatType = ExportFormatType.Excel;
break;
}
crystalReport.ExportToHttpResponse(formatType, Response, true, "Report");
}
}
}
PostBackTrigger works but the UpdateProcess does not appear. I can't seem to get both working. I appreciate any help!
Related
I have a very simple asp.net webform page with a listview, I can not make the listView refresh on postback, only if I make a new request. I guess the listView gets its content from the viewstate or something. I databind in the Page_Load event, and i have also tried the Page_Init event. No difference.
I have probably been through the first 50 Google search results, and nothing seems to work.
How do i force the listview to refresh on postback?
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RSS_Reader.Default" %>
<%# Import namespace="RSS_Reader.Extensions" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RSS Reader</title>
<link rel="shortcut icon" href="favicon.ico" />
<link href="css/main.css" rel="stylesheet" />
<script src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="js/App.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="horizontal-menu">
<ul id="horizontal-menu-list">
<li id="add-feed">Add Feed
<ul>
<li>
<input type="text" placeholder="Feed URL" /><button class="button green">Add</button>
</li>
</ul>
</li>
<li>Add Group</li>
<li>Settings</li>
</ul>
<div id="global-search">
<input type="text" id="search" placeholder="Search" />
</div>
<div style="clear: both"></div>
</div>
<div id="feeds">
<div id="feed-filter">
<asp:TextBox ID="feedUrl" runat="server" placeholder="Feed URL"></asp:TextBox>
<asp:Button ID="ThisIsAUniqueNameForAButton" runat="server" CssClass="button green" Text="Subscribe" OnClick="subscribeToFeed" />
</div>
<div id="feed-wrapper">
<asp:ListView ID="FeedCollection" runat="server">
<ItemTemplate>
<div><div class="feed-collection-icon"></div><asp:Label runat="server" ID="listFeedTitle" Text='<%# Bind("Title") %>'></asp:Label></div>
</ItemTemplate>
</asp:ListView>
</div>
</div>
<div id="content">
<div id="feed-title">Version2 - IT For Professionelle</div>
<div id="nodes">
<asp:GridView ID="nodesGridview" OnSorting="nodesGridview_Sorting" runat="server" OnSortCommand="SortGrid" AllowSorting="True" AutoGenerateColumns="false">
<EmptyDataTemplate>
No feed.
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Title" SortExpression="title">
<ItemTemplate>
<div class="node-icon"></div> <%# XPath("title") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="description">
<ItemTemplate>
<%# XPath("description").ToString().Truncate(50) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="pubDate">
<ItemTemplate>
<%# XPath("pubDate").ToString().toDate() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div id="reading-pane">
<asp:Label ID="readingPaneTitle" runat="server" Text="Label"></asp:Label>
</div>
</div>
<div id="footer">
Copyright © 2014 - Mads O. Nielsen
</div>
</div>
</form>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Diagnostics;
using System.Data.Entity;
using System.Data;
using System.IO;
using RSS_Reader.FeedCollection;
using RSS_Reader.FeedItem;
namespace RSS_Reader
{
public partial class Default : System.Web.UI.Page
{
private XmlDataSource feedItems;
private FeedReader feedReader;
protected void Page_Init(object sender, EventArgs e)
{
Debug.WriteLine("Page_Init");
if (!IsPostBack)
{
}
}
protected void Page_Load(object sender, EventArgs e)
{
RSS_Reader.Model db = new RSS_Reader.Model();
//var feedQuery = from emp in db.Feeds select emp;
//List<Feeds> empList = feedQuery.ToList();
FeedCollection.DataSource = db.Feeds.ToList();
feedReader = new FeedReader();
feedReader.load("http://www.version2.dk/it-nyheder/rss");
feedItems = new XmlDataSource();
feedItems.EnableCaching = false;
feedItems.ID = "feedItems";
feedItems.Data = feedReader.feed.OuterXml;
feedItems.XPath = "rss/channel/item";
nodesGridview.Width = new Unit(100.00, UnitType.Percentage);
nodesGridview.DataSource = feedItems;
FeedCollection.DataBind();
nodesGridview.DataBind();
}
protected void subscribeToFeed(object sender, EventArgs e)
{
FeedManager feedManager = new FeedManager();
feedManager.subscribe(feedUrl.Text);
}
protected void nodesGridview_Sorting(object sender, GridViewSortEventArgs e)
{
Debug.WriteLine("Sorting");
}
}
}
if it cant refresh the ListView because of save it into ViewState you can set EnableViewState property to False then server refresh it in all postback
I am writting a program (web site) which uploads images on the server,displays them and saves them in a folder. I want to add a progress bar which will be connected to the image's file size while it is being filled.Here is my Default.aspx file
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<br />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<output id="images"></output>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gvPhotos" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField ="Text" HeaderText="filename" />
<asp:ImageField DataImageUrlField="Value" ControlStyle-Height="250" ControlStyle-Width="250" HeaderText="Photo" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Loading....<br />
<br />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
<p>
<asp:Label ID="lblSuccess" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
This is Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List<ListItem> sia = new List<ListItem>();
if(FileUpload1.HasFiles)
{
foreach(var file in FileUpload1.PostedFiles)
{ string[] fileName = Directory.GetFiles(Server.MapPath("~/Images/"));
file.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), file.FileName));
Response.Write(file.FileName + " - " + file.ContentLength + " Bytes. <br />");
sia.Add(new ListItem(file.FileName, "~/Images/" + file.FileName));
}
lblSuccess.Text = string.Format("{0} files have been uploaded successfully.", FileUpload1.PostedFiles.Count);
}
gvPhotos.DataSource = sia;
gvPhotos.DataBind();
}
}
Please help me in inserting a jquery bar in my Default.aspx. Thank you in advance
I'm trying to update controls outisde the update panel with the ref. of below link . but its not working. i want to know what i'm missing.in my application can't use these labels inside the update panel.
http://msdn.microsoft.com/en-us/library/bb301423(v=vs.110).aspx
HTML
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Updatepaneltest._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
function PageLoadingHandler(sender, args) {
var dataItems = args.get_dataItems();
if ($get('Label1') !== null)
$get('Label1').innerHTML = dataItems['Label1'];
if ($get('Label2') !== null)
$get('Label2').innerHTML = dataItems['Label2'];
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
UpdatePanel content.
<asp:Button ID="Button1" Text="Submit" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:Label ID="Label1" Text="hiii" runat="server" /> <br />
<asp:Label ID="Label2" runat="server" />
</div>
</asp:Content>
code behind
protected void Page_Load(object sender, EventArgs e)
{
// ScriptManager ScriptManager1 = ScriptManager.GetCurrent(this.Page);
if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
{
ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString());
ScriptManager1.RegisterDataItem(Label2, DateTime.Now.Year.ToString());
}
}
Control updates inside of updatepanel,
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" Text="Submit" runat="server" />
<asp:Label ID="Label1" Text="hiii" runat="server" /> <br />
<asp:Label ID="Label2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Control updates outside of updatepanel,
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:Button ID="Button1" Text="Submit" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label1" Text="hiii" runat="server" /> <br />
<asp:Label ID="Label2" runat="server" />
Later, write below lines in client side.
var pageInstance = Sys.WebForms.PageRequestManager.getInstance();
pageInstance.add_pageLoaded(UpdateLabelHandler);
function UpdateLabelHandler(sender, args)
{
var ControldataItems = args.get_dataItems();
if ($get('Label1') !== null)
$get('Label1').innerHTML = ControldataItems ['Label1'];
if ($get('Label2') !== null)
$get('Label2').innerHTML = ControldataItems ['Label2'];
}
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterDataItem(Label1, DateTime.Now.ToString());
ScriptManager1.RegisterDataItem(Label2, DateTime.Now.Year.ToString());
}
Hope this may help you.
I have this problem:
Add in UpdatePanel a TextBox with RequiredFieldValidator.
Then at the Postback add values to DB and redirect to an other Page.
My problem is why when I add one MyControl and click GO button, ValidationSummary don't stop the postback, and why at the second click the ValidationSummary fire?
The second problem is why VisualStudio don't find MyControl class(Underline it with red) but when I compile result is no error?
This is the code:
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Validators</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="upAdd" runat="server">
<ContentTemplate>
<asp:Button ID="cmdAdd" runat="server" Text="Add" onclick="cmdAdd_Click" CausesValidation="false" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:UpdatePanel ID="upValidators" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="phValidators" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmdAdd" />
</Triggers>
</asp:UpdatePanel>
<hr />
<asp:Button ID="cmdGo" runat="server" Text="Go" onclick="cmdGo_Click" />
</div>
<asp:ValidationSummary ID="valSum" HeaderText="Errors:" ShowSummary="False" ShowMessageBox="True" EnableClientScript="True" DisplayMode="BulletList" runat="server"></asp:ValidationSummary>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
List<int> m_oItems = new List<int>();
if (ViewState["ItemsAdded"] != null)
{
m_oItems = (List<int>)ViewState["ItemsAdded"];
}
ViewState["ItemsAdded"] = m_oItems;
foreach (int m_oItem in m_oItems)
{
MyControl m_oMyControl = (MyControl)LoadControl("MyControl.ascx");
phValidators.Controls.Add(m_oMyControl);
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
List<int> m_oItems = new List<int>();
if (ViewState["ItemsAdded"] != null)
{
m_oItems = (List<int>)ViewState["ItemsAdded"];
}
m_oItems.Add(m_oItems.Count);
ViewState["ItemsAdded"] = m_oItems;
MyControl m_oMyControl = (MyControl)LoadControl("MyControl.ascx");
phValidators.Controls.Add(m_oMyControl);
}
protected void cmdGo_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
//Add To DB
Response.Redirect("http://www.google.it/");
}
}
MyControl.ascx
<asp:TextBox ID="txtLanguage" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rqrLanguage" runat="server" ErrorMessage="Language not set" ControlToValidate="txtLanguage"></asp:RequiredFieldValidator>
<br />
Thank you very much
Damiano
Here's my situation. I have a web forms page, and it's getting annoying when the entire page scrolls to the top every time you click a control, so I'm trying to apply update panels to my page.
The submit button may or may not be click-able depending on whether or not the contents of individual update panels are in a proper state.
When the submit button is clicked, It can potentially affect any of the controls on the page.
I understand that I can do some of this with the <triggers> tag element of the update panel, but I don't want to have to put everything on the page into an update panel with it's own trigger, I'd rather that the submit button just reload the entire page.
For the sake of simplicity I've put together a sample project to represent my page. It has a "reset" button, to represent the "submit" button.
The Master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" />
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ Log In ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
The default page:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label runat=server Text="This lable represents things not in update panels" ID="label1"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel1" runat=server>
<ContentTemplate>
<table>
<tr>
<td><asp:Button ID="button1" runat="server" OnClick="Button1_click" Text="Button 1" /></td>
<td><asp:TextBox ID="textBox1" runat="server" Text="StartText" Enabled="false" /></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat=server>
<ContentTemplate>
<table>
<tr>
<td><asp:Button ID="button2" runat="server" OnClick="Button2_click" Text="Button 2" /></td>
<td><asp:TextBox ID="textBox2" runat="server" Text="StartText" Enabled="false" /></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanelReset" runat=server>
<ContentTemplate>
<asp:Button ID="reset" runat="server" OnClick="Reset_click" Text="reset" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="button1" EventName="click" />
<asp:AsyncPostBackTrigger ControlID="button2" EventName="click" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
The code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_click(object sender, EventArgs e)
{
textBox1.Enabled = !textBox1.Enabled;
reset.Enabled = !textBox1.Enabled && !textBox2.Enabled;
}
protected void Button2_click(object sender, EventArgs e)
{
textBox2.Enabled = !textBox2.Enabled;
reset.Enabled = !textBox1.Enabled && !textBox2.Enabled;
}
protected void Reset_click(object sender, EventArgs e)
{
textBox1.Text = "StartText";
textBox2.Text = "StartText";
label1.Text = "reset button clicked";
}
}
}
Just add a PostBackTrigger to the last UpdatePanel.
<asp:PostBackTrigger ControlID="reset" />