I have a report that takes about 2 or 3 minutes to pull all the data
So I am trying to use ASP.net Asynch pages to prevent a timeout. But can't get it to work
Here's what I am doing :
private delegate List<PublishedPagesDataItem> AsyncReportDataDelegate();
private AsyncReportDataDelegate longRunningMethod;
private List<PublishedPagesDataItem> reportData;
public PublishedPagesReport() // this is the constructor
{
reportData = new List<PublishedPagesDataItem>();
longRunningMethod = GetReportData;
}
protected void Page_Load(object sender, EventArgs e)
{
this.PreRenderComplete +=
new EventHandler(Page_PreRenderComplete);
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsynchOperation),
new EndEventHandler(EndAsynchOperation)
);
}
private List<PublishedPagesDataItem> GetReportData()
{
// this is a long running method
}
private IAsyncResult BeginAsynchOperation(object sender, EventArgs e, AsyncCallback cb, object extradata)
{
return longRunningMethod.BeginInvoke(cb, extradata);
}
private void EndAsynchOperation(IAsyncResult ar)
{
reportData = longRunningMethod.EndInvoke(ar);
}
private void Page_PreRenderComplete(object sender, EventArgs e)
{
reportGridView.DataSource = reportData;
reportGridView.DataBind();
}
So I have a delegate representing the Long running method (GetReportData).
And I am trying to call it as per this article :
http://msdn.microsoft.com/en-us/magazine/cc163725.aspx
The long running method does complete in the debugger but breakpoints on the EndAsynchOperation and Page_PreRenderComplete methods never get hit
any idea what I am doing wrong?
the code below works. not sure what the difference is, except that there is a if (!IsPostBack)
anyway, now solved
private delegate List<PublishedPagesDataItem> AsyncReportDataDelegate();
private AsyncReportDataDelegate longRunningMethod;
private List<PublishedPagesDataItem> reportData;
public asynchtest()
{
reportData = new List<PublishedPagesDataItem>();
longRunningMethod = GetReportData;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Hook PreRenderComplete event for data binding
this.PreRenderComplete +=
new EventHandler(Page_PreRenderComplete);
// Register async methods
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);
}
}
IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
AsyncCallback cb, object state)
{
return longRunningMethod.BeginInvoke(cb, state);
}
void EndAsyncOperation(IAsyncResult ar)
{
reportData = longRunningMethod.EndInvoke(ar);
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Output.DataSource = reportData;
Output.DataBind();
}
Related
I have made a simple web form with some static HTML on the design view, and on the code behind I put on two methods: Page_Load and Page_PreRender as follows:
public partial class SamplePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ClearContent();
Response.ClearHeaders();
Response.StatusCode = (int)HttpStatusCode.NotFound;
Respons.Write("Not Found");
Context.ApplicationInstance.CompleteRequest();
}
protected void Page_PreRender(object sender, EventArgs e) // Why does this event get called? Should not the CompleteReqeust()
{ // cause the page to jump directly to the end of events pipeline?
throw new NotImplementedException();
}
}
Moreover, I have read so many Q&As about Response.End() being "ugly", "dangerous", etc, even from MSDN website. But it puzzles me a lot, why if so the Response.Redirect(string) still uses Response.End() internally?
Overriding IHttpHandler's ProcessRequest(HttpContext) in the Page is enough to do the trick.
public partial class SamplePage : System.Web.UI.Page
{
public override void ProcessRequest(System.Web.HttpContext context)
{
if (conditionTrue)
{
context.Response.StatusCode = 404;
context.ApplicationInstance.CompleteRequest();
}
else
{
base.ProcessRequest(context);
}
}
protected void Page_Load(object sender, EventArgs e) // This is not called also :P
{
}
protected void Page_PreRender(object sender, EventArgs e) // Not called now :)
{
throw new NotImplementedException();
}
}
I fill some controls with data from data base, by calling a select method
and I change the values to update it, but when I press update button, it takes the values that called at the page load, and ignore all changes.
how can I avoid this ?
thanks in advance : )
here is a simple code to present my problem
SelectBLL _selectbll;
string _title = string.Empty;
string _details = string.Empty;
#region Page Load
protected void Page_Load(object sender, EventArgs e)
{
GetUMedicineDetails();
}
#endregion
#region Get UMedicine Details
private void GetUMedicineDetails()
{
_selectbll = new SelectBLL();
_selectbll._GetUMedicineDetails(Request.QueryString[0].ToString(), ref _title, ref _details);
#region Bind Controls
txtdetails.Text = _details;
txttitle.Text = _title;
#endregion
}
#endregion
#region Update Button
protected void btnupdate_Click(object sender, EventArgs e)
{
_UpdateUMedicine();
}
#endregion
#region UpdateU Medicine
public void _UpdateUMedicine()
{
_updatebll = new UpdateBLL();
_updatebll._UpdateUMedicine(
Convert.ToInt32(Request.QueryString[0]), txtdetails.Text, txttitle.Text);
}
#endregion
Page.IsPostBack Property: Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetUMedicineDetails();
}
}
Also, Refer:
How to: Determine How ASP.NET Web Pages Were Invoked
DataBind only if !Page.IsPostaBack:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostaBack)
GetUMedicineDetails();
}
Can anyone tell me what I'm doing wrong?
//--- menuFac ---
public void UpdatePageById()
{
db.ModifyData("UPDATE tblsider SET colOverskrift=#1, colTekst=#2 WHERE colID=#3", _overskrift, _tekst, _id);
}
//--- where i'm trying to get some from db to edit and save the edited ---
menuFac objTekst = new menuFac();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
objTekst._id = int.Parse(Request.QueryString["colID"]);
DataRow value = objTekst.GetPageById();
txtOverskrift.Text = value["colOverskrift"].ToString();
txtTekst.Text = value["colTekst"].ToString();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
objTekst._id = int.Parse(Request.QueryString["colID"]);
objTekst._overskrift = txtOverskrift.Text;
objTekst._tekst = txtTekst.Text;
objTekst.UpdatePageById();
Response.Redirect("Protected.aspx");
}
Replace this call method
objTekst.UpdatePageById();
with
this.UpdatePageById();
UpdatePageById is method of your Page Class, not of your property objTekst
how to do that every time s_Sort not update SortDirection.Desc
private SortDirection s_Sort = SortDirection.Desc;
protected void Page_Load(object sender, EventArgs e)
{
lblSort.Text = S_Sort.ToString();//every time == SortDirection.Desc - this is bad!
if (!IsPostBack)
{
ShowTree();
Validate();
}
}
Need
public void btnSortUp_Click(object sender, EventArgs e)
{
S_Sort = SortDirection.Asc;
}
public void btnSortDown_Click(object sender, EventArgs e)
{
S_Sort = SortDirection.Desc;
}
but after SortDirection.Desc is bad
The is a problem of the ASP.NET lifecycle. Every time a postback happens (for example, when btnSortUp or btnSortDown is clicked), a new instance of your page is created, i.e., S_Sort is reinitialized to Desc. If you want to persist the value between postbacks, you can store it in the viewstate, for example, by encapsulating it in a private property:
private SortDirection S_Sort {
get { return (SortDirection)(ViewState["S_Sort"] ?? SortDirection.Desc); }
set { ViewState["S_Sort"] = value; }
}
I want to preserve the dynamically created control when postback occurs .
protected void Page_Load(object sender, EventArgs e)
{
}
private void CreateTable()
{
HtmlTableRow objHtmlTblRow = new HtmlTableRow();
HtmlTableCell objHtmlTableCell = new HtmlTableCell();
objHtmlTableCell.Controls.Add(new TextBox());
objHtmlTblRow.Cells.Add(objHtmlTableCell);
mytable.Rows.Add(objHtmlTblRow);
this.SaveControlState();
// this.Controls.Add(mytable);
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateTable();
}
It can be achieved by calling CreateTable() in Page_Load. Is there any alternative way to preserve the control
Thanks
You can add them to a List when you create them and save your List to Session. On postback (Page_Load) load them from your Session to your Page.
the below code should work
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateTable()
}
public static Control GetPostBackControl(Page thisPage)
{
Control ctrlPostedback = null;
string ctrlName = thisPage.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
{
ctrlPostedback = thisPage.FindControl(ctrlName);
}
else
{
foreach (string Item in thisPage.Request.Form)
{
Control c = thisPage.FindControl(Item);
if (((c) is System.Web.UI.WebControls.Button))
{
ctrlPostedback = c;
}
}
}
return ctrlPostedback;
}
The code works from UpdatePanel
Reference:http://www.asp.net/ajax/videos/how-to-dynamically-add-controls-to-a-web-page